Hello again, I had some problems using this, it didn't run, but it helped me with showing how to do it, my current approach looks like this:
for /f %%a in (sp_list.txt) do sqlcmd -E -S SERVER -d DB -h-1 -Q "DECLARE @I INT, @SP1 NVARCHAR(4000), @SP2 NVARCHAR(4000), @SP3 NVARCHAR(4000) SET @I = 0 SET @SP1 = '' SET @SP2 = '' SET @SP3 = '' SELECT @I = count(number)from dbo.syscomments WHERE id = OBJECT_ID('%%a') WHILE @I > 0 BEGIN SELECT @SP1 = CASE WHEN @I = 1 THEN text ELSE @SP1 END, @SP2 = CASE WHEN @I = 2 THEN text ELSE @SP2 END, @SP3 = CASE WHEN @I = 3 THEN text ELSE @SP3 END from dbo.syscomments WHERE (id = OBJECT_ID('%%a')) AND (colid = @I) SET @I = @I-1 END SELECT @SP1,@SP2,@SP3" -o "%%a.sql"
indeed the SELECT @SP1,@SP2,@SP3 is not correct since it produces a space between the variables. But if I use + just the first varialbe will be returned (or just the first 4000 chars).
In SQL Management Studio the statement
SELECT CAST(@SP1 AS varchar(max)) + CAST(@SP2 AS varchar(max)) + CAST(@SP3 AS varchar(max))
works fine, but carried over to the sqlcmd statement mysterious amount of 259 or 260 chars will be returned only.
any suggestions? Thanks in advance :)