Hi Experts,
I have writen the below stored procedure which creates SQL statements by taking the input (Table name,Column name , Data type from the table). I am getting an internal error when executing the SP. Could you please help on this.
Error:
Could not execute 'CALL SREELATR.TRANSPORTS7('SREELATR')' in 455 ms 514 µs .
[129]: transaction rolled back by an internal error: [129] "SREELATR"."TRANSPORTS7": line 27 col 7 (at pos 939): [129] (range 3): transaction rolled back by an internal error: sql syntax error: incorrect syntax near "SELECT": line 1 col 39 (at pos 39)
==================================================
CREATE COLUMN TABLE "SREELATR"."CREATEDATA" ( "TABLENAME1" NVARCHAR(100) CS_STRING,
"COLUMN1" NVARCHAR(100) CS_STRING,
"DATATYPE1" NVARCHAR(100) CS_STRING )
drop table "SREELATR"."CREATEDATA"
INSERT INTO "SREELATR"."CREATEDATA" VALUES ('A7','B2','INTEGER')
=============================
DROP PROCEDURE SREELATR.TRANSPORTS7;
CREATE PROCEDURE SREELATR.TRANSPORTS7 (IN ZSCHEMA NVARCHAR(50))
LANGUAGE SQLSCRIPT AS
BEGIN
DECLARE ZTABL NVARCHAR(30);
DECLARE ZCOLUMN NVARCHAR(100);
DECLARE ZDATATYPE NVARCHAR(100);
DECLARE ROWCOUNT1 NVARCHAR(1000);
DECLARE DYNAMICSQL NVARCHAR(10000);
DECLARE COLUMCNT INTEGER;
DECLARE WORKAREA NVARCHAR(10000);
DECLARE ZCOUNTER INTEGER DEFAULT 0;
DECLARE TABLE_VAR NVARCHAR(30);
DECLARE CURSOR CURSORFORCREATESCRIPT FOR
SELECT * FROM "SREELATR"."CREATEDATA";
OPEN CURSORFORCREATESCRIPT;
FOR CUR_ROW AS CURSORFORCREATESCRIPT DO
ZTABL := CUR_ROW.TABLENAME1;
ZCOLUMN := CUR_ROW.COLUMN1;
ZDATATYPE := CUR_ROW.DATATYPE1;
CREATE COLUMN TABLE SREELATR.TEMPTABL (TEMPCOUNT INTEGER);
ROWCOUNT1 := 'SELECT COUNT(*) FROM SREELATR.CREATEDATA WHERE TABLENAME = '''||:ZTABL||'''' ;
DYNAMICSQL := 'INSERT INTO SREELATR.TEMPTABL VALUES (' ||:ROWCOUNT1 ||')';
EXEC DYNAMICSQL ;
--SELECT ::ROWCOUNT INTO COLUMCNT FROM DUMMY;
DROP TABLE SREELATR.TEMPTABL;
IF TABLE_VAR = ZTABL THEN
ZCOUNTER := ZCOUNTER + 1;
ELSE
ZCOUNTER := 1;
END IF;
IF ZCOUNTER = 1 THEN
WORKAREA := :ZCOLUMN|| ' ' ||:ZDATATYPE ;
ELSE
WORKAREA := :WORKAREA ||','||:ZCOLUMN||' '||:ZDATATYPE ;
END IF;
TABLE_VAR := ZTABL;
IF ZCOUNTER = COLUMCNT THEN
DYNAMICSQL := 'CREATE COLUMN TABLE '||:ZSCHEMA ||'.' ||:ZTABL ||'('|| :WORKAREA|| ')';
EXEC DYNAMICSQL;
WORKAREA := ' ' ;
END IF;
END FOR;
END;
CALL SREELATR.TRANSPORTS7('SREELATR')