开发者

Getting column names from a table

开发者 https://www.devze.com 2023-04-12 00:32 出处:网络
I am trying to get column names for a Given table. So I wrote a query like this: SELECTsc.Name FROMAsdim.dbo.sysobjects so

I am trying to get column names for a Given table. So I wrote a query like this:

  SELECT   sc.Name
   FROM     Asdim.dbo.sysobjects so
            INNER JOIN Asdim.dbo.syscolumns sc ON so.id = sc.id
            INNER JOIN Asdim.dbo.systypes st ON sc.xtype = st.xusertype
   WHERE    so.Name = 'Admin'

The problem is that I have two tables with name 'Admin' but they have different schemas. So when I run this query:

SELECT * FROM Asdim.dbo.sysobjects
WHERE name LIKE 'Admin'

I get two records since the table names are same. Is there a way that I caould filter out based on the schema name too开发者_开发知识库?


Using the INFORMATION_SCHEMA tables will be easier and more portable:

SELECT c.column_name
  FROM information_schema.columns c
 WHERE c.table_name = 'Admin' and c.table_schema = 'SCHEMA'

More info on INFORMATION_SCHEMA.COLUMNS.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号