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?