I am trying to list all the columns from the Adventureworks table that I select. What T-sQL statement or stored procedure can I execute to list all columns? I want to use my C # web application to input a single input parameter = table_name, and then get a list of all column columns as output. Right now I'm trying to execute the sp_columns executable, which works, but I can’t get only one column with the select and exec command. Does anyone know how to do this?
Thank you all for all your answers, but none of your answers do what I need. Let me explain my problem more to you. The following query returns what I need. But my problem is to include this logic in the SP, which takes an input parameter. So here is a 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 is as follows:
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 this gives me an error:
Invalid column name 'AddressType'
How to do it?
source
share