开发者

In T-SQL how to display columns for given table name?

开发者 https://www.devze.com 2022-12-25 22:38 出处:网络
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 inp

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 ob开发者_运维问答j 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?


select * from sys.columns where object_id = object_id(@tablename);


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


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.


SELECT *
FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_NAME = 'TableName' AND TABLE_CATALOG = 'DatabaseName'


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


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.

0

精彩评论

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