从表中获取列名
我正在尝试获取给定表的列名称。所以我写了一个这样的查询:
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'
问题是我有两个名为“Admin”的表,但它们有不同的架构。因此,当我运行此查询时:
SELECT * FROM Asdim.dbo.sysobjects
WHERE name LIKE 'Admin'
我得到两条记录,因为表名相同。有没有一种方法可以根据架构名称进行过滤?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
使用
INFORMATION_SCHEMA
表会更容易更便携:更多信息
INFORMATION_SCHEMA.COLUMNS
。Using the
INFORMATION_SCHEMA
tables will be easier and more portable:More info on
INFORMATION_SCHEMA.COLUMNS
.