I have this dynamic SQL in a stored procedure:
Declare @template nvarchar(max)
Declare @publishdetails nvarchar(max)
set @publishdetails= ',B.[PublishedBy]=suser_name(),
B.[PublishedDate]=GETDATE() '
set @template='if NOT EXISTS(select * from ' +@DestinationDB+ '.[CLs] where id='+ str(@slid)+')
insert into ' +@DestinationDB+ '.CLs (id,slid,slversion) VALUES ( '+ str(@id)+','+ str(@slid)+','+str(@slversion)+')
update B set
B.[Clientid]=A.clientid,
--.........
B.[CreatedDate] = A.CreatedDate,
B.[ModifiedDate] = A.ModifiedDate,
B.[CreatedBy] = A.CreatedBy,
B.[ModifiedBy] = A.ModifiedBy '+@publishdetails+ --Added publishdetails
'FROM ' + @SourceDB + '.[CLs] as A, '+ @DestinationDB+ '.[CLs] as B
where A.slversion = '+ str(@slversion)+' and A.id='+str(@slid) + 'B.slversion = '+ str(@slversion)+' and B.id='+str(@slid)
print 'template is: ' + @template
exec sp_Executesql @template
When exec sp_Executesql @template
is executing, it fails. Because @template
is > 4000 chars and is truncated. How can I split it in chunks and execute it the correct way?