Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Store Procedure Declaration

Status
Not open for further replies.

leira

Programmer
Jun 26, 2008
1
Hi @ all,

what's my mistake?
I want to build up a table with relevant row-count information of specific tables in the database.
Therefor in the first step I create a temp-table with all relevant tables from dbc and the sql-queries to select the data.
After then I loop with a cursor around every entry in this table and generate a dynamic sql to insert the table and row-count
information in the target table.
But I get different failure messages and don't know why.
I had a look at the examples in the SQl-Reference,too. There is no END Definition before the DECLARE Statements and the
declarations are the same like mine.

error messages:

SPL1027:E(L14), Missing/Invalid SQL statement'E(3707):Syntax error, expected something like an 'END' keyword between ';' and the 'DECLARE' keyword.'.
SPL1027:E(L15), Missing/Invalid SQL statement'E(3707):Syntax error, expected something like an 'END' keyword between ';' and the 'DECLARE' keyword.'.
SPL1027:E(L18), Missing/Invalid SQL statement'E(3707):Syntax error, expected something like an 'END' keyword between ';' and the 'DECLARE' keyword.'.
SPL1030:E(L19), Referring to undefined cursor 'tblcursor'.
SPL1027:E(L21), Missing/Invalid SQL statement'E(3707):Syntax error, expected something like a 'DO' keyword between ')' and the 'FETCH' keyword.'.
SPL2001:E(L24), Undefined symbol 'ctbl'.
SPL2001:E(L24), Undefined symbol 'sql_str'.

I work on a Teradata RELEASE V2R.06.02.02.30
Thanks for every help.

Leira

Procedure

CREATE PROCEDURE table_facts
(IN DBase VARCHAR(30))
BEGIN

CREATE TABLE tables
AS ( SELECT
CAST('SELECT country, cdate, ccode, COUNT(*) AS rowcnt FROM ' || TRIM(tt.Tablename) || ';' AS VARCHAR(1000)) AS sql_str
, CAST(TRIM(tt.Tablename) AS VARCHAR(30)) AS ctbl
FROM ( SELECT TableName FROM dbc.tables
WHERE TableKind = 'T'
AND TRIM(DatabaseName) = '' || :DBase || ''
AND TRIM(TableName) LIKE '%_c') tt
) WITH DATA ;

DECLARE sql_str VARCHAR(1000) ;
DECLARE ctbl VARCHAR(30) ;
DECLARE tblcursor CURSOR FOR
SELECT sql_str, ctbl FROM tables
ORDER BY 2 ;

OPEN tblcursor ;

WHILE (SQLCODE = 0)
FETCH tblcursor INTO sql_str, ctbl ;
DELETE FROM tables_info ;
CALL DBC.SysExecSQL (
'INSERT INTO tables_info (table_name, country, cdate, ccode, rowcnt) SELECT ''' || :ctbl || ''',a1.* FROM (' || :sql_str || 'GROUP BY 1,2,3) a1' ) ;
END WHILE ;

CLOSE tblcursor ;

END ;
 
I am not sure why you want this SP, but anyways the WHILE needs a DO as well as an END WHILE. As in

WHILE x > 0
DO
something
END WHILE;
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top