SQL Server 2008 R2 - Dynamic SQL Update - Set

Hi Experts,

I have this bit of code involving SQL Dynamic Query:
SELECT @sql =
           '	IF OBJECT_ID (''dbo.' + @TBLNM + ''') IS NULL
         		UPDATE	DATABASE_TABLES
        		SET	TBL_STAT = ''DELETED''
        		WHERE	TBL_NM = ''' + @TBLNM + '''
        		AND	TBL_STAT = ''PURGE_ELIG''
            '
     exec sp_executesql @sql;
                  

Open in new window


This works fine,

But when I try to put more into it:
DECLARE 
   @rc int,
   @Update DateTime,
   @sql nvarchar(4000) 
      
 
      PRINT 'STARTING UPDATE OF CLAIM RECORDS IN DATABASE_TABLES: ' + @TBLNM
      SET @rc = 1
      set @Update = GETDATE()

      WHILE @rc > 0
      BEGIN
    
     SELECT @sql =
           '	IF OBJECT_ID (''dbo.' + @TBLNM + ''') IS NULL
         		UPDATE	DATABASE_TABLES
        		SET	TBL_STAT = ''DELETED''
        		SET REC_STAT_CD = ''I''
        		SET UPD_TS = ''' + @Update + '''
        		WHERE	TBL_NM = ''' + @TBLNM + '''
        		AND	TBL_STAT = ''PURGE_ELIG''
            '
     exec sp_executesql @sql;
                  
          
                  SET @rc = @@ROWCOUNT
      END;

Open in new window


I get error(s):
The first error I get involving the a time error:
SSIS package "PurgeClaim.dtsx" starting.
Error: 0xC002F210 at Update Claim Record In DBT, Execute SQL Task: Executing the query "Exec Update_DB_Tables ?" failed with the following error: "Conversion failed when converting date and/or time from character string.". Possible failure reasons: Problems with the query, "ResultSet" property not set correctly, parameters not set correctly, or connection not established correctly.
Task failed: Update Claim Record In DBT
Warning: 0x80019002 at Loop all Claim Tables: SSIS Warning Code DTS_W_MAXIMUMERRORCOUNTREACHED.  The Execution method succeeded, but the number of errors raised (1) reached the maximum allowed (1); resulting in failure. This occurs when the number of errors reaches the number specified in MaximumErrorCount. Change the MaximumErrorCount or fix the errors.
Warning: 0x80019002 at PurgeClaim: SSIS Warning Code DTS_W_MAXIMUMERRORCOUNTREACHED.  The Execution method succeeded, but the number of errors raised (1) reached the maximum allowed (1); resulting in failure. This occurs when the number of errors reaches the number specified in MaximumErrorCount. Change the MaximumErrorCount or fix the errors.
SSIS package "PurgeClaim.dtsx" finished: Failure.


When I take out all that is in the time I get another error involving the Set command:
DECLARE 
   @rc int,
   --@Update DateTime,
   @sql nvarchar(4000) 
      
 
      PRINT 'STARTING UPDATE OF CLAIM RECORDS IN DATABASE_TABLES: ' + @TBLNM
      SET @rc = 1
      --set @Update = GETDATE()

      WHILE @rc > 0
      BEGIN
    
     SELECT @sql =
           '	IF OBJECT_ID (''dbo.' + @TBLNM + ''') IS NULL
         		UPDATE	DATABASE_TABLES
        		SET	TBL_STAT = ''DELETED''
        		SET REC_STAT_CD = ''I''
        		WHERE	TBL_NM = ''' + @TBLNM + '''
        		AND	TBL_STAT = ''PURGE_ELIG''
            '
     exec sp_executesql @sql;
                  
          
                  SET @rc = @@ROWCOUNT
      END;
      PRINT 'ENDING UPDATE OF CLAIM RECORDS IN DATABASE_TABLES: ' + @TBLNM
      CHECKPOINT;

Open in new window


Error:
SSIS package "PurgeClaim.dtsx" starting.
Error: 0xC002F210 at Update Claim Record In DBT, Execute SQL Task: Executing the query "Exec Update_DB_Tables ?" failed with the following error: "Incorrect syntax near '='.". Possible failure reasons: Problems with the query, "ResultSet" property not set correctly, parameters not set correctly, or connection not established correctly.
Task failed: Update Claim Record In DBT
Warning: 0x80019002 at Loop all Claim Tables: SSIS Warning Code DTS_W_MAXIMUMERRORCOUNTREACHED.  The Execution method succeeded, but the number of errors raised (1) reached the maximum allowed (1); resulting in failure. This occurs when the number of errors reaches the number specified in MaximumErrorCount. Change the MaximumErrorCount or fix the errors.
Warning: 0x80019002 at PurgeClaim: SSIS Warning Code DTS_W_MAXIMUMERRORCOUNTREACHED.  The Execution method succeeded, but the number of errors raised (1) reached the maximum allowed (1); resulting in failure. This occurs when the number of errors reaches the number specified in MaximumErrorCount. Change the MaximumErrorCount or fix the errors.
SSIS package "PurgeClaim.dtsx" finished: Failure.


So there are two errors I get

Please help and thanks
Amour22015Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

ste5anSenior DeveloperCommented:
Why, on earth? Your dynamic SQL is equivalent to

IF OBJECT_ID('dbo.' + @TBLNM) IS NULL
    UPDATE  DATABASE_TABLES
    SET     TBL_STAT = 'DELETED'
    WHERE   TBL_NM = @TBLNM
            AND TBL_STAT = 'PURGE_ELIG';

Open in new window


So why do you need dynamic SQL here at all?
0
Vitor MontalvãoMSSQL Senior EngineerCommented:
You only need a SET instruction:

	IF OBJECT_ID (''dbo.' + @TBLNM + ''') IS NULL
         		UPDATE	DATABASE_TABLES
        		SET	TBL_STAT = ''DELETED'',
        		        REC_STAT_CD = ''I''
        		WHERE	TBL_NM = ''' + @TBLNM + '''
        		AND	TBL_STAT = ''PURGE_ELIG''

Open in new window

0
nemws1Database AdministratorCommented:
The "SET" directive in an UPDATE command is only used once.  If you have multiple things to set as a part of your update, you comma separate them much like you would fields in a SELECT.

From your second code chunk:
     SELECT @sql =
           '	IF OBJECT_ID (''dbo.' + @TBLNM + ''') IS NULL
         		UPDATE	DATABASE_TABLES
        		SET	TBL_STAT = ''DELETED''
        		    , REC_STAT_CD = ''I''
        		    , UPD_TS = ''' + @Update + '''
        		WHERE	TBL_NM = ''' + @TBLNM + '''
        		AND	TBL_STAT = ''PURGE_ELIG''
            '

Open in new window

0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

Amour22015Author Commented:
Ok,

I tryed: ste5an
IF OBJECT_ID('dbo.' + @TBLNM) IS NULL
		UPDATE  DATABASE_TABLES
		SET     TBL_STAT = 'DELETED'
		SET REC_STAT_CD = I
		SET UPD_TS = @Update
		WHERE   TBL_NM = @TBLNM
            AND TBL_STAT = 'PURGE_ELIG';

Open in new window

But I keep getting the error:
"Invalid syntax near '='."


I tryed: nemws1
 SELECT @sql =
           '	IF OBJECT_ID (''dbo.' + @TBLNM + ''') IS NULL
         		UPDATE	DATABASE_TABLES
        		SET	TBL_STAT = ''DELETED''
        		    , REC_STAT_CD = ''I''
        		    , UPD_TS = ''' + @Update + '''
        		WHERE	TBL_NM = ''' + @TBLNM + '''
        		AND	TBL_STAT = ''PURGE_ELIG''
        	'

Open in new window

But I keep getting the error about the date:
SSIS package "PurgeClaim.dtsx" starting.
Error: 0xC002F210 at Update Claim Record In DBT, Execute SQL Task: Executing the query "Exec Update_DB_Tables ?" failed with the following error: "Conversion failed when converting date and/or time from character string.". Possible failure reasons: Problems with the query, "ResultSet" property not set correctly, parameters not set correctly, or connection not established correctly.
Task failed: Update Claim Record In DBT
Warning: 0x80019002 at Loop all Claim Tables: SSIS Warning Code DTS_W_MAXIMUMERRORCOUNTREACHED.  The Execution method succeeded, but the number of errors raised (1) reached the maximum allowed (1); resulting in failure. This occurs when the number of errors reaches the number specified in MaximumErrorCount. Change the MaximumErrorCount or fix the errors.
Warning: 0x80019002 at PurgeClaim: SSIS Warning Code DTS_W_MAXIMUMERRORCOUNTREACHED.  The Execution method succeeded, but the number of errors raised (1) reached the maximum allowed (1); resulting in failure. This occurs when the number of errors reaches the number specified in MaximumErrorCount. Change the MaximumErrorCount or fix the errors.
SSIS package "PurgeClaim.dtsx" finished: Failure.


Please help and thanks
0
nemws1Database AdministratorCommented:
You need to cast your @Update var.  It is a DATETIME value and you are trying to concatenate in your dynamic query (ie - treat it as text).  Try this:

SELECT @sql =
           '	IF OBJECT_ID (''dbo.' + @TBLNM + ''') IS NULL
         		UPDATE	DATABASE_TABLES
        		SET	TBL_STAT = ''DELETED''
        		    , REC_STAT_CD = ''I''
        		    , UPD_TS = ''' + CAST(@Update AS VARCHAR(40)) + '''
        		WHERE	TBL_NM = ''' + @TBLNM + '''
        		AND	TBL_STAT = ''PURGE_ELIG''
        	'

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Amour22015Author Commented:
Thanks for all help
0
ste5anSenior DeveloperCommented:
Just a reminder: No need for dynamic SQL..

IF OBJECT_ID('dbo.' + @TBLNM + '') IS NULL
    UPDATE  DATABASE_TABLES
    SET     TBL_STAT = 'DELETED' ,
            REC_STAT_CD = 'I' ,
            UPD_TS = @Update
    WHERE   TBL_NM = @TBLNM
            AND TBL_STAT = 'PURGE_ELIG';

Open in new window

0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft SQL Server 2008

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.