Returning Table Field Names

So, today one of my friends asked me if I knew how to return the names of all the fields in a table using a stored procedure so that he could build the names into a generic string collection. This is how you do it:

[sql]

CREATE PROCEDURE [dbo].[GetColumnNames] @tableName VARCHAR(50)
AS
SELECT  name
FROM    syscolumns
WHERE   id = OBJECT_ID(@tableName)

[/sql]

Easy as pie! :)

Share

Leave a Reply

Your email address will not be published. Required fields are marked *