jancas08 (2/25/2013)
Hi, I need help with this dynamic query to execute the sql statements but it gives the following error when i run it:UPDATE [janinetestdata].[dbo].[EDTE] set ED_THREE = CORRECTED VALUE WHERE zBarcode = 3636 and PERSON_NUMBER = 3
Msg 102, Level 15, State 1, Line 1
Incorrect syntax near 'VALUE'.
It is hard to help with this without table structure and some sample data but here is a quick takeaway from what you posted.
in your updates statement quoted above you have the words "CORRECTED VALUE". that is where the incorrect syntax is coming from. If "CORRECTED VALUE" is the string that you are trying to update ED_THREE to then possible changing you query to this might help. note the extra quotes around + CAST(@CorrectedValue AS nVarchar(15)) +
USE [janinetestdata]
GO
/*Purpose of the sql query is to make corrections to all the errors detected
which in essences pertains to all of the tables concerning the LFS Survey .
This query will run insertions, updates and deletion commands*/
--Variables declaration section
DECLARE @ErrorMessage varchar(500), @BatchNo varchar(15), @zBarcode varchar(15),
@IncorrectValue varchar(100), @ColumnsToFix varchar(25), @Person_Number varchar(7),
@CorrectedValue varchar(100), @Description varchar(500), @Command varchar(10),
@ED_Three varchar(10), @ED_Four varchar(10), @ED_Five varchar(10), @ED_Six varchar(10),
@CORRECTION_STATEMENT nvarchar(900)
DECLARE ERRORCursor CURSOR FOR
SELECT ERRORMESSAGE, BATCH_NO, zBarcode, Incorrect_Value , Columns_To_Fix ,
Person_No , Corrected_Value , Err_Description ,Command , ED_THREE , ED_FOUR
,ED_FIVE, ED_SIX
FROM [janinetestdata].[dbo].[TEST66]
OPEN ERRORCursor
BEGIN
--Fetch first household
FETCH NEXT FROM ERRORCursor into
@ErrorMessage, @BatchNo , @zBarcode,@IncorrectValue,
@ColumnsToFix ,@Person_Number, @CorrectedValue ,
@Description, @Command, @ED_Three , @ED_Four ,
@ED_Five , @ED_Six
WHILE @@FETCH_STATUS = 0
BEGIN
/*Beginning of Education Commands*/
if @ErrorMessage LIKE 'ED%'
BEGIN/*Beginning of Update Command for Education Table*/
if @Command = 'UPDATE' and @Person_Number is not null and @ColumnsToFix is not null
and @zBarcode is not null
BEGIN
set @CORRECTION_STATEMENT = 'UPDATE [janinetestdata].[dbo].[EDTE] set ' + CAST(@ColumnsToFix AS nVarchar(50))+ ' = '''
+ CAST(@CorrectedValue AS nVarchar(15)) + ''' WHERE zBarcode = ' + CAST(@zBarcode AS nVarchar(15))+ ' and '
+ 'PERSON_NUMBER = ' + CAST(@Person_Number AS nVarchar(7))
print @CORRECTION_STATEMENT
EXECUTE (@CORRECTION_STATEMENT)
END/*End of Update Command for Education Table*/
END/*end of Fourteen and Over Commands*/
--execute sp_executesql @CORRECTION_STATEMENT
FETCH NEXT FROM ERRORCursor into
@ErrorMessage, @BatchNo , @zBarcode,@IncorrectValue , @ColumnsToFix ,
@Person_Number,@CorrectedValue , @Description, @Command,
@ED_Three , @ED_Four , @ED_Five , @ED_Six
END/*End of begin*/
END
CLOSE ERRORCursor
DEALLOCATE ERRORCursor
GO