在T-SQL中如何显示给定表名的列?

发布于 2024-08-28 08:31:07 字数 1057 浏览 9 评论 0原文

我试图列出我选择的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

GRAY°灰色天空 2024-09-04 08:31:07
select * from sys.columns where object_id = object_id(@tablename);
select * from sys.columns where object_id = object_id(@tablename);
痴情 2024-09-04 08:31:07

您不需要将语句创建为字符串 - 事实上,这就是让您陷入困境的原因。试试这个:

CREATE PROCEDURE [dbo].[getColumnNames] @TableName VarChar(50) AS

BEGIN
SET NOCOUNT ON;
SELECT col.name FROM sysobjects obj 
INNER JOIN syscolumns col ON obj.id = col.id 
WHERE obj.name = @TableName
END

You don't need to create the statement as a string -- in fact, that's what's messing you up. Try this:

CREATE PROCEDURE [dbo].[getColumnNames] @TableName VarChar(50) AS

BEGIN
SET NOCOUNT ON;
SELECT col.name FROM sysobjects obj 
INNER JOIN syscolumns col ON obj.id = col.id 
WHERE obj.name = @TableName
END
仅冇旳回忆 2024-09-04 08:31:07

要显示database.dbo.yourtable的信息,请尝试以下操作:

SELECT
    *
    FROM INFORMATION_SCHEMA.COLUMNS
    WHERE 
        TABLE_CATALOG   ='database'
        AND TABLE_SCHEMA='dbo'
        AND TABLE_NAME  ='yourtable'
    ORDER BY ORDINAL_POSITION

编辑基于OP的编辑

您不需要动态sql,只需使用给定的参数:

CREATE PROCEDURE [dbo].[getColumnNames]
@TableName VarChar(50)
AS
BEGIN
    SET NOCOUNT ON;
    DECLARE @Rows         int
           ,@ReturnValue  int
    SET @ReturnValue=0
    SELECT
        col.name 
        FROM sysobjects            obj 
            inner join syscolumns  col on obj.id = col.id 
        WHERE obj.name = @TableName
        ORDER BY obj.name
    SELECT @Rows=@@ROWCOUNT
    IF @Rows=0
    BEGIN
        SET @ReturnValue= 1  --table not found!!
    END
    RETURN @ReturnValue 

END

如果您检查返回值并获取 1,则找不到与给定 @TableName 参数匹配的表。您可以使用它在应用程序中给出错误消息。

to display info for database.dbo.yourtable try this:

SELECT
    *
    FROM INFORMATION_SCHEMA.COLUMNS
    WHERE 
        TABLE_CATALOG   ='database'
        AND TABLE_SCHEMA='dbo'
        AND TABLE_NAME  ='yourtable'
    ORDER BY ORDINAL_POSITION

EDIT based on OP's edit

You don't need dynamic sql, just use the given parameter:

CREATE PROCEDURE [dbo].[getColumnNames]
@TableName VarChar(50)
AS
BEGIN
    SET NOCOUNT ON;
    DECLARE @Rows         int
           ,@ReturnValue  int
    SET @ReturnValue=0
    SELECT
        col.name 
        FROM sysobjects            obj 
            inner join syscolumns  col on obj.id = col.id 
        WHERE obj.name = @TableName
        ORDER BY obj.name
    SELECT @Rows=@@ROWCOUNT
    IF @Rows=0
    BEGIN
        SET @ReturnValue= 1  --table not found!!
    END
    RETURN @ReturnValue 

END

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.

柠北森屋 2024-09-04 08:31:07
SELECT *
FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_NAME = 'TableName' AND TABLE_CATALOG = 'DatabaseName'
SELECT *
FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_NAME = 'TableName' AND TABLE_CATALOG = 'DatabaseName'
柠檬色的秋千 2024-09-04 08:31:07

使用 Remus Rusanu 答案或使用 C# 代码中的 SqlConnection.GetSchema() 函数

Use Remus Rusanu answer or use SqlConnection.GetSchema() function from your c# code

死开点丶别碍眼 2024-09-04 08:31:07

回复您的编辑问题:

您的脚本不起作用,因为您正在构建动态字符串,并且在该动态字符串中,您必须向放置在 where 子句中的字符串添加引号。修改后的代码将是:

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 

但是,为什么要构建动态字符串?这会做同样的事情,并且不需要动态 SQL 的开销:

BEGIN 
SET NOCOUNT ON; 
SET @TableName = RTRIM(@TableName) 
SELECT col.name from sysobjects obj
inner join syscolumns col on obj.id = col.id
where obj.name = @TableName 
END 

如果您担心 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:

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 

However, why are you buliding a dynamic string? This would do the same thing and not require the overhead of dynamic SQL:

BEGIN 
SET NOCOUNT ON; 
SET @TableName = RTRIM(@TableName) 
SELECT col.name from sysobjects obj
inner join syscolumns col on obj.id = col.id
where obj.name = @TableName 
END 

If you are worried about SQL injection, is not a problem here -- either obj.name = (whatever they passed in), or it doesn't.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文