sql statement execution from Routine.
Moderators: chulett, rschirm, roy
sql statement execution from Routine.
Hi all,
acording to business logic ,if its a full refresh load then i need to truncate the Table Before loading.Otherwise i need to upsert the data into the table.So for truncating the table we wrote a routine. which is as given below. Before it was working fine, but now its giving execution failure.
all connections to DB like DSN,user,Passwd,happens good,but statement execution fails.
so plz let me know where i went wrong.
$INCLUDE DSINCLUDE JOBCONTROL.H
$INCLUDE UNIVERSE.INCLUDE ODBC.H
* Database Environment Settings
henv = SQL.NULL.HENV
hConn = SQL.NULL.HDBC
hStmt = SQL.NULL.HSTMT
** Connect to DataSource
status = SQLAllocEnv(hEnv)
status = SQLAllocConnect(hEnv, hConn)
status = SQLConnect(hConn, DbName, DbUName ,DbPassword )
If status = SQL.ERROR
Then
Call DSLogInfo("Connection KO " , "ExecSql")
ErrorCode = -1
Goto Finally
End Else
Call DSLogInfo("Connection OK " , "ExecSql")
ErrorCode = 1
End
** Execution Statement
status = SQLAllocStmt(hConn, hStmt)
Call DSLogInfo("request SQL:":Stmt,"ExecSql")
status = SQLExecDirect(hStmt, Stmt)
ErrorCode=SQLError(hEnv,hConn,hStmt,SQLstate,DBMSCode,ErrText)
Call DSLogInfo("status of SQL":SQLError(hEnv,hConn,hStmt,SQLstate,DBMSCode,ErrText),SQLstatus)
If status<>SQL.SUCCESS AND status<>SQL.SUCCESS.WITH.INFO
Then
Call DSLogInfo("Request KO ", "ExecSql")
ErrorCode = -1
Goto Finally
End Else
Call DSLogInfo("Request OK ", "ExecSql")
ErrorCode = 1
End
** Free Allocated Resources
Ans = ErrorCode
Finally:
status = SQLFreeStmt(hStmt, SQL.CLOSE)
status = SQLFreeStmt(hStmt, SQL.DROP)
status = SQLDisconnect(hConn)
status = SQLFreeConnect(hConn)
status = SQLFreeEnv(hEnv)
Ans = ErrorCode
acording to business logic ,if its a full refresh load then i need to truncate the Table Before loading.Otherwise i need to upsert the data into the table.So for truncating the table we wrote a routine. which is as given below. Before it was working fine, but now its giving execution failure.
all connections to DB like DSN,user,Passwd,happens good,but statement execution fails.
so plz let me know where i went wrong.
$INCLUDE DSINCLUDE JOBCONTROL.H
$INCLUDE UNIVERSE.INCLUDE ODBC.H
* Database Environment Settings
henv = SQL.NULL.HENV
hConn = SQL.NULL.HDBC
hStmt = SQL.NULL.HSTMT
** Connect to DataSource
status = SQLAllocEnv(hEnv)
status = SQLAllocConnect(hEnv, hConn)
status = SQLConnect(hConn, DbName, DbUName ,DbPassword )
If status = SQL.ERROR
Then
Call DSLogInfo("Connection KO " , "ExecSql")
ErrorCode = -1
Goto Finally
End Else
Call DSLogInfo("Connection OK " , "ExecSql")
ErrorCode = 1
End
** Execution Statement
status = SQLAllocStmt(hConn, hStmt)
Call DSLogInfo("request SQL:":Stmt,"ExecSql")
status = SQLExecDirect(hStmt, Stmt)
ErrorCode=SQLError(hEnv,hConn,hStmt,SQLstate,DBMSCode,ErrText)
Call DSLogInfo("status of SQL":SQLError(hEnv,hConn,hStmt,SQLstate,DBMSCode,ErrText),SQLstatus)
If status<>SQL.SUCCESS AND status<>SQL.SUCCESS.WITH.INFO
Then
Call DSLogInfo("Request KO ", "ExecSql")
ErrorCode = -1
Goto Finally
End Else
Call DSLogInfo("Request OK ", "ExecSql")
ErrorCode = 1
End
** Free Allocated Resources
Ans = ErrorCode
Finally:
status = SQLFreeStmt(hStmt, SQL.CLOSE)
status = SQLFreeStmt(hStmt, SQL.DROP)
status = SQLDisconnect(hConn)
status = SQLFreeConnect(hConn)
status = SQLFreeEnv(hEnv)
Ans = ErrorCode
Aquilis
-
- Participant
- Posts: 54607
- Joined: Wed Oct 23, 2002 10:52 pm
- Location: Sydney, Australia
- Contact:
What's the error message?
Where do you initialize the variable containing the SQL statement (Stmt)?
You really need to strip ALL error information from the handle - loop executing SQLError() until it returns SQL.NO.DATA.FOUND.
Where do you initialize the variable containing the SQL statement (Stmt)?
You really need to strip ALL error information from the handle - loop executing SQLError() until it returns SQL.NO.DATA.FOUND.
IBM Software Services Group
Any contribution to this forum is my own opinion and does not necessarily reflect any position that IBM may hold.
Any contribution to this forum is my own opinion and does not necessarily reflect any position that IBM may hold.
actually we are executing 3 statements truncate,delete and update.
As a runtime 'Stmt' will have the sql statements(It might be Truncate or update or delete).This Routine is invoked in the job control code,which processes the different jobs in sequence.
1. If its job1 fails .. then delete whole data from table.
2. after job1 success... job2 is invoked ,so which is a truncate and load.
As a runtime 'Stmt' will have the sql statements(It might be Truncate or update or delete).This Routine is invoked in the job control code,which processes the different jobs in sequence.
1. If its job1 fails .. then delete whole data from table.
2. after job1 success... job2 is invoked ,so which is a truncate and load.
Aquilis
-
- Participant
- Posts: 54607
- Joined: Wed Oct 23, 2002 10:52 pm
- Location: Sydney, Australia
- Contact:
-
- Participant
- Posts: 54607
- Joined: Wed Oct 23, 2002 10:52 pm
- Location: Sydney, Australia
- Contact:
-
- Participant
- Posts: 54607
- Joined: Wed Oct 23, 2002 10:52 pm
- Location: Sydney, Australia
- Contact:
Good so far. What precisely was the SQL statement - that is, the value of the Stmt variable - when this error occurred? And to what kind of data base is the connection being made (e.g. UniVerse, Oracle, Informix...)?
JoshGeorge, the expired license will give a different error message.
JoshGeorge, the expired license will give a different error message.
IBM Software Services Group
Any contribution to this forum is my own opinion and does not necessarily reflect any position that IBM may hold.
Any contribution to this forum is my own opinion and does not necessarily reflect any position that IBM may hold.
-
- Participant
- Posts: 54607
- Joined: Wed Oct 23, 2002 10:52 pm
- Location: Sydney, Australia
- Contact:
You still have not reported what the value of the Stmt variable was. Also, what was logged as the "status of SQL" message? Note that you need to call SQLError() to remove information from the handle even if the status is SQL.SUCCESS.WITH.INFO - that is, you still have a "dirty handle" even if it's only informational message attached to it. That is, you need to change the AND status <> SQL.SUCCESS.WITH.INFO logic.
Code: Select all
Begin Case
Case status = SQL.SUCCESS
Call DSLogInfo("Successfully called SQLExecDirect", "Reporting")
Case status = SQL.SUCCESS.WITH.INFO
Msg = "Successfully called SQLExecDirect with information."
Temp = SQLError(hEnv,hConn,hStmt,SQLState,DBMSCode,ErrText)
Msg<-1> = "SQL State = " : SQLState : ", DBMS code = " : DBMSCode
Msg<-1> = ErrText
Call DSLogInfo(Msg, "Reporting")
Case @TRUE
Msg = "Error calling SQLExecDirect."
Temp = SQLError(hEnv,hConn,hStmt,SQLState,DBMSCode,ErrText)
Loop
While Temp <> SQL.NO.DATA.FOUND And Temp <> SQL.ERROR
Msg<-1> = "SQL State = " : SQLState : ", DBMS code = " : DBMSCode
Msg<-1> = ErrText
Repeat
Call DSLogWarn(Msg, "Reporting")
End Case
IBM Software Services Group
Any contribution to this forum is my own opinion and does not necessarily reflect any position that IBM may hold.
Any contribution to this forum is my own opinion and does not necessarily reflect any position that IBM may hold.
DB Used: Oracle
Status Sql: SQL100
Error message when tested:
Arg1 = XYZ
Arg2 = DFLD
Arg3 = abc
Arg4 = select count(*) from DFLD.DFREJECT;
Test completed.
DSLogInfo called from : ExecSql
Message to be logged is...
> Connection OK
DSLogInfo called from : ExecSql
Message to be logged is...
> request SQL:select count(*) from DFLD.DFREJECT;
Program "DSLogInfo": Line 46, Variable "CallerName" previously undefined. Empty string used.
DSLogInfo called from :
Message to be logged is...
> status of SQL100
DSLogInfo called from : ExecSql
Message to be logged is...
> Request KO
Result = -1
Status Sql: SQL100
Error message when tested:
Arg1 = XYZ
Arg2 = DFLD
Arg3 = abc
Arg4 = select count(*) from DFLD.DFREJECT;
Test completed.
DSLogInfo called from : ExecSql
Message to be logged is...
> Connection OK
DSLogInfo called from : ExecSql
Message to be logged is...
> request SQL:select count(*) from DFLD.DFREJECT;
Program "DSLogInfo": Line 46, Variable "CallerName" previously undefined. Empty string used.
DSLogInfo called from :
Message to be logged is...
> status of SQL100
DSLogInfo called from : ExecSql
Message to be logged is...
> Request KO
Result = -1
Aquilis