在T-SQL中如何显示给定表名的列?
我试图列出我选择的 Adventureworks 表中的所有列。我可以执行什么 T-SQL 语句或存储过程来查看所有列的列表?我想使用我的 C# Web 应用程序输入一个输入参数 = table_name,然后获取所有列名称的列表作为输出。现在我正在尝试执行有效的 sp_columns 存储过程,但我无法仅获得包含 select 和 exec 组合的一列。有人知道该怎么做吗?
感谢大家的回复,但你们的答案都没有达到我的需要。让我为您详细解释一下我的问题。下面的查询返回我需要的内容。但我的问题是将这个逻辑合并到一个带有输入参数的 SP 中。所以这里是成功的 SQL 语句:
select col.name from sysobjects obj inner join syscolumns col
on obj.id = col.id
where obj.name = 'AddressType'
order by obj.name
目前我的 SP 看起来像:
CREATE PROCEDURE [dbo].[getColumnNames]
@TableName VarChar(50)
AS
BEGIN
SET NOCOUNT ON;
SET @TableName = RTRIM(@TableName)
DECLARE @cmd AS NVARCHAR(max)
SET @cmd = N'SELECT col.name from sysobjects obj ' +
'inner join syscolumns col on obj.id = col.id ' +
'where obj.name = ' + @TableName
EXEC sp_executesql @cmd
END
但我运行上面的命令,
exec getColumnNames 'AddressType'
它给了我错误:
Invalid column name 'AddressType'
我该如何完成这个?
I am trying to list all of the columns from whichever Adventureworks table I choose. What T-sQL statement or stored proc can I execute to see this list of all columns? I want to use my C# web app to input one input parameter = table_name and then get a list of all the column_names as output. Right now I am trying to execute the sp_columns stored proc which works, but I can't get just the one column with select and exec combined. Does anybody know how to do this?
Thanks everyone for all your replies, but none of your answers do what I need. Let me explain my problem more for you. The query below returns what I need. But my problem is incorporating this logic into an SP that takes an input parameter. So here is the successful SQL statement:
select col.name from sysobjects obj inner join syscolumns col
on obj.id = col.id
where obj.name = 'AddressType'
order by obj.name
And currently my SP looks like:
CREATE PROCEDURE [dbo].[getColumnNames]
@TableName VarChar(50)
AS
BEGIN
SET NOCOUNT ON;
SET @TableName = RTRIM(@TableName)
DECLARE @cmd AS NVARCHAR(max)
SET @cmd = N'SELECT col.name from sysobjects obj ' +
'inner join syscolumns col on obj.id = col.id ' +
'where obj.name = ' + @TableName
EXEC sp_executesql @cmd
END
But I run the above as
exec getColumnNames 'AddressType'
and it gives me error:
Invalid column name 'AddressType'
How do I accomplish this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
您不需要将语句创建为字符串 - 事实上,这就是让您陷入困境的原因。试试这个:
You don't need to create the statement as a string -- in fact, that's what's messing you up. Try this:
要显示
database.dbo.yourtable
的信息,请尝试以下操作:编辑基于OP的编辑
您不需要动态sql,只需使用给定的参数:
如果您检查返回值并获取 1,则找不到与给定 @TableName 参数匹配的表。您可以使用它在应用程序中给出错误消息。
to display info for
database.dbo.yourtable
try this:EDIT based on OP's edit
You don't need dynamic sql, just use the given parameter:
if you check the return value and get a 1 then no table was found matching the given @TableName parameter. You can use this to give a error message in the application.
使用 Remus Rusanu 答案或使用 C# 代码中的
SqlConnection.GetSchema()
函数Use Remus Rusanu answer or use
SqlConnection.GetSchema()
function from your c# code回复您的编辑问题:
您的脚本不起作用,因为您正在构建动态字符串,并且在该动态字符串中,您必须向放置在 where 子句中的字符串添加引号。修改后的代码将是:
但是,为什么要构建动态字符串?这会做同样的事情,并且不需要动态 SQL 的开销:
如果您担心 SQL 注入,那么这里不是问题 - obj.name =(无论它们传入的是什么),或者不是。
A reply to your edited question:
Your script does not work because you're building a dynamic string, and within that dynamic string you must add quotes to the string you are placing in your where clause. The revised code would be:
However, why are you buliding a dynamic string? This would do the same thing and not require the overhead of dynamic SQL:
If you are worried about SQL injection, is not a problem here -- either obj.name = (whatever they passed in), or it doesn't.