sql select with column name like

前端 未结 8 1135
盖世英雄少女心
盖世英雄少女心 2020-12-02 15:46

I have a table with column names a1,a2...,b1.b2....

How can I select all those with column names like a%?

相关标签:
8条回答
  • 2020-12-02 16:23

    You cannot with standard SQL. Column names are not treated like data in SQL.

    If you use a SQL engine that has, say, meta-data tables storing column names, types, etc. you may select on that table instead.

    0 讨论(0)
  • 2020-12-02 16:24

    Here is a nice way to display the information that you want:

    SELECT B.table_catalog as 'Database_Name',
             B.table_name as 'Table_Name',
            stuff((select ', ' + A.column_name
                  from INFORMATION_SCHEMA.COLUMNS A
                  where A.Table_name = B.Table_Name
                  FOR XML PATH(''),TYPE).value('(./text())[1]','NVARCHAR(MAX)')
                  , 1, 2, '') as 'Columns'
      FROM INFORMATION_SCHEMA.COLUMNS B
      WHERE B.TABLE_NAME like '%%'
            AND B.COLUMN_NAME like '%%'
      GROUP BY B.Table_Catalog, B.Table_Name
      Order by 1 asc
    

    Add anything between either '%%' in the main select to narrow down what tables and/or column names you want.

    0 讨论(0)
提交回复
热议问题