Note that there are some explanatory texts on larger screens.

plurals
  1. POIn T-SQL how to display columns for given table name?
    primarykey
    data
    text
    <p>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?</p> <p>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:</p> <pre><code>select col.name from sysobjects obj inner join syscolumns col on obj.id = col.id where obj.name = 'AddressType' order by obj.name </code></pre> <p>And currently my SP looks like:</p> <pre><code>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 </code></pre> <p>But I run the above as</p> <pre><code>exec getColumnNames 'AddressType' </code></pre> <p>and it gives me error:</p> <pre><code>Invalid column name 'AddressType' </code></pre> <p>How do I accomplish this?</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload