Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 834
  • Last Modified:

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
0
Amour22015
Asked:
Amour22015
  • 2
  • 2
  • 2
  • +1
2 Solutions
 
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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
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
 
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

Featured Post

NFR key for Veeam Agent for Linux

Veeam is happy to provide a free NFR license for one year.  It allows for the non‑production use and valid for five workstations and two servers. Veeam Agent for Linux is a simple backup tool for your Linux installations, both on‑premises and in the public cloud.

  • 2
  • 2
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now