You can concatenate all column values in sql pass variable by many ways
as examples: XMLPATH, STUFF or COALESCE, with some manipulation with string.
but still getting an error
The Main Issue for This task is Go
Go
is Not-Valid T-SQL
so if you tried execute dynamic sql contains Go
, the next error will be raised:-
Msg 102, Level 15, State 1, Line 4 Incorrect syntax near 'go'.
After surfing the stackoverflow , I get the resolved here:-
Execute Dynamic Query with go in sql
so Get the next demo (after applying the above link with my trials):-
Demo:-
-- Try to create 4 procedures proc_1, proc_2 , proc_3 and proc_4
Create database Demo
go
use Demo
go
Create table MyTable (procName varchar (200))
go
insert into MyTable values ('proc_1')
go
insert into MyTable values ('proc_2')
go
insert into MyTable values ('proc_3')
go
insert into MyTable values ('proc_4')
go
declare @Query nvarchar(max)
SELECT @Query = isnull(@Query,'') + 'create proc ['+[ProcName]+'] as
print '''+[ProcName]+''''+ char (10) + '
Go
'
FROM MyTable
--print @Query
SET @Query = 'EXEC (''' + REPLACE(REPLACE(@Query, '''', ''''''), 'GO', '''); EXEC(''') + ''');'
EXEC (@Query)
Result:-

