0

I am newbie to IBM db2.Need to convert the below mentioned SP to db2 syntax. But i am stuck with many equivalents used or available in Db2. Even google research doesn't show how exactly we can compare object id of tables in db2 as I am doing in SQL Server stored procedure. Could anyone suggest me with right way to proceed?

EDIT: I have updated with equivalent DB2 syntax, but facing below error while deploying at the particular line, Can anyone identify and help me understand what is wrong with this syntax or the problem lies anywhere else in the procedure.

line no 25 : DECLARE v_sqlstate CHAR(5);

BACKUPTABLE: 25: An unexpected token "<variable declaration> was found following "". Expected tokens may include: "".. SQLCODE=-104, SQLSTATE=42601, DRIVER=4.18.60 An unexpected token variable declaration was found following "". Expected tokens may include: "".. SQLCODE=-104, SQLSTATE=42601, DRIVER=4.18.60

SQL Server Stored procedure syntax:

CREATE PROCEDURE [dbo].[BackUpTable] 
        @TableName sysname
    AS
    BEGIN

        SET nocount ON 

        DECLARE @sql VARCHAR(500) 

        IF EXISTS (SELECT * 
               FROM   sys.objects 
               WHERE  object_id = Object_id(N'[dbo].[' + @TableName+'_EST' + ']') 
                      AND TYPE IN ( N'U' )) 

            BEGIN 
              SET @sql = 'declare @Done bit
                          set @Done = 0
                          while @Done = 0
                          begin
                            delete top (100000)
                            from ' + @TableName + '_Bak' + 
                            ' if @@rowcount = 0     
                                set @Done = 1  
                          end;' 
              SET @sql = @sql + 'insert into ' + @TableName + '_Bak select * from ' + 
                         @TableName +'_EST'

              EXEC(@sql) 
            END 
        ELSE 
            BEGIN 
              DECLARE @err_message VARCHAR(300) 

              SELECT @err_message = 'The table "' + Isnull(@TableName, 'null') + 
                                '" does not exist' 

              RAISERROR (@err_message, 16, 1) 

            END  

    END

DB2 SYNTAX CREATED SO FAR:

 CREATE OR REPLACE PROCEDURE BackUpTable (IN TableName VARCHAR(128))
    DYNAMIC RESULT SETS 1

BEGIN

    DECLARE dynamicSql  VARCHAR(500); 

    IF(EXISTS( 
        SELECT * FROM SYSIBM.SYSTABLES
             WHERE NAME =  TableName||'_EST'
            ) 
    )

    THEN 

           SET dynamicSql  = 'DELETE FROM '||TableName ||'_BAK';

           SET dynamicSql  = dynamicSql  ||'insert into ' || TableName || '_BAK select * from ' || 
                     TableName || '_EST';

          EXECUTE IMMEDIATE dynamicSql; 

    ELSE

    DECLARE v_sqlstate CHAR(5);    
    DECLARE v_sqlcode INT;

    DECLARE SQLSTATE CHAR(5) DEFAULT '00000';   
    DECLARE SQLCODE INT DEFAULT 0;

    DECLARE EXIT HANDLER FOR SQLEXCEPTION  

    BEGIN

    SELECT SQLSTATE, SQLCODE
    INTO v_sqlstate, v_sqlcode
    FROM sysibm.sysdummy1;    

    SET O_Error_Msg = 'TABLE IS NOT AVAILABLE:: SQLState : '||v_sqlstate||' SQLCode : '||v_sqlcode ;   

    END;

    END IF;

END
RVR
  • 97
  • 3
  • 13

1 Answers1

0

on z/os you can do it:

IF( EXISTS( SELECT 1 FROM QSYS2.SYSTABLES WHERE TABLE_SCHEMA = 'YOURLIB' AND TABLE_NAME = 'YOURTABLENAME')) THEN
DROP TABLE YOURLIB.YOURTABLENAME;
END IF;
Esperento57
  • 16,521
  • 3
  • 39
  • 45
  • Thanks for the response. I am trying something like this : IF(EXISTS( SELECT * FROM SYSIBM.SYSTABLES WHERE NAME = TableName||'_EST' ) ) – RVR Jan 12 '17 at 07:06