Home > Software design >  Can i use a wild card in select statement.e.g All Column names that start with Job_Title,Job_Status,
Can i use a wild card in select statement.e.g All Column names that start with Job_Title,Job_Status,

Time:04-28

Can i use a wild card in select statement.e.g All Column names that start with Job_Title,Job_Status,Job_Description?

For Example: Select 'Job%' from StaffTable;

I want to get all Table Headers that starts with 'Job'

and also display their values.

CodePudding user response:

No - the only way of achieving this would be to dynamically construct, and then execute, this query within a stored procedure.

CodePudding user response:

No, you only have SELECT * and SELECT mytable.* which means all columns or all columns from a specific table. Aside from those two cases, you must spell out all the columns you want to use. Columns must be fixed in the query at the time the query is parsed.

You can query the set of columns from metadata:

SELECT COLUMN_NAME FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_NAME='StaffTable' AND COLUMN_NAME LIKE 'Job%';

Then based on the results, format a second query as a string, and execute that query.

You tagged several different brands of SQL database. The example I gave was for MySQL. Other brands may provide different tables for metadata, so you'll have to look up how to do the equivalent query in whatever brand you use.

  •  Tags:  
  • sql
  • Related