/* Script : sp_getcolumns Description : Utility for getting column/parameter list for given database object Author : Pawel Potasinski Date : 2008-04-09 */ USE master GO IF OBJECT_ID('dbo.sp_getcolumns','P') IS NOT NULL DROP PROC dbo.sp_getcolumns GO CREATE PROC [dbo].[sp_getcolumns] @object sysname, @horizontal tinyint = 0 AS SET NOCOUNT ON DECLARE @lines TABLE (line_id int identity(1,1), line nvarchar(max)) IF EXISTS (SELECT 1 FROM sys.columns WHERE [object_id] = OBJECT_ID(@object)) BEGIN IF @horizontal = 1 BEGIN DECLARE @line nvarchar(max) SET @line = N'' SELECT @line = @line + [name] + N', ' FROM sys.columns WITH (NOLOCK) WHERE [object_id] = OBJECT_ID(@object) ORDER BY column_id INSERT @lines (line) SELECT LEFT(@line,LEN(@line)-1) END ELSE BEGIN INSERT @lines (line) SELECT [name] + N',' FROM sys.columns WITH (NOLOCK) WHERE [object_id] = OBJECT_ID(@object) ORDER BY column_id UPDATE @lines SET line = LEFT(line,LEN(line)-1) WHERE line_id = @@IDENTITY END END SELECT line AS ' ' FROM @lines ORDER BY line_id GO -- test USE AdventureWorks GO EXEC dbo.sp_getcolumns 'Production.Product' EXEC dbo.sp_getcolumns 'HumanResources.uspUpdateEmployeeLogin',1 GO