I have this simple query :-
update [Schema1].ReportData
set Qry = 'declare @cols nvarchar(max) select @cols = ''Code,Name,DateOfJoining'''
where ViewName = 'Rpt1'
But here is the problem. I have more than 50 schemas, so I did the following (summarized SQL):
WHILE (@i <= (SELECT MAX(idx) FROM @schema_table))
BEGIN
-- get the next record primary key
SET @schema_names = (SELECT schema_names FROM @schema_table WHERE idx = @i)
BEGIN TRY
DECLARE @sSQL nvarchar(500);
SELECT @sSQL = N'update '+@schema_names+'.ReportData set Qry='declare @cols nvarchar(max) select @cols = ''Code,FName,DateOfJoining ,CategoryName''' where ViewName='Rpt1''
EXEC sp_executesql @sSQL
END TRY
BEGIN CATCH
SELECT ERROR_MESSAGE()+' '+@schema_names AS ErrorMessage;
END CATCH
-- increment counter for next record
SET @i = @i + 1
END
But is giving me syntax error on this line :-
SELECT @sSQL = N'update '+@schema_names+'.ReportData set Qry='declare @cols nvarchar(max) select @cols = ''Code,FName,DateOfJoining ,CategoryName''' where ViewName='Rpt1''
Actually i tried many things like using double quotes & escape characters. How to solve this?
@colsyou really should clarify that.Qryis the column in which i save the queries.