SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO -- ============================================= -- Author: <Author,,Name> -- Create date: <Create Date,,> -- Description: <Description,,> -- ============================================= CREATE PROCEDURE [Procedure_Name] @tableName VARCHAR(100) ='', @destination VARCHAR(100) ='', @seprator VARCHAR(100) ='' AS BEGIN SET NOCOUNT ON; DECLARE @name VARCHAR(100) select @name=isnull(@name+N',','')+name from tempdb.dbo.syscolumns where id=object_id('euseraccess') DECLARE @fields VARCHAR(100) DECLARE @sql VARCHAR(MAX) DECLARE FIELD_CUR CURSOR FOR select Name FROM SysColumns Where id=Object_Id(@tableName) FETCH NEXT FROM FIELD_CUR INTO @fields WHILE (@@FETCH_STATUS <> -1) BEGIN SELECT @fields =concat(@fields,@seprator,@fields); FETCH NEXT FROM FIELD_CUR INTO @fields END CLOSE FIELD_CUR set @sql = 'select concat(' + @fields + ')from ' +@tableName DECLARE result_CUR CURSOR FOR EXEC(@sql) FETCH NEXT FROM result_CUR into result WHILE (@@FETCH_STATUS <> -1) begin insert into table value result FETCH NEXT FROM result_CUR into result end close result_CUR END GO