Hi,
I don't know Datastage Basic for develop my own routine. I need help about us.
I would like to call a stored procedure (SQL Server) in my own routine. How ?
Thx
How to call a stored procedure in a routine
Moderators: chulett, rschirm, roy
-
- Participant
- Posts: 4
- Joined: Mon Apr 30, 2007 8:35 am
-
- Participant
- Posts: 4
- Joined: Mon Apr 30, 2007 8:35 am
I need a custom routine because i would like to call it in each job Master (before/after job) for log. All log will be in table SQL and alimentate by my stored procedure.DSguru2B wrote:If your not well versed in writing routines then I would suggest a workaround. Call the stord proc. in a before/after sql tab of DRS stage or from STP stage. You dont need to write a custom routine to do this.
-
- Participant
- Posts: 4
- Joined: Mon Apr 30, 2007 8:35 am
thx you. I found response in searching on the Internet and it work :
***************************************************
** FUNCTION ExecSQLProc(dataSource,userName,password)
==> RETURN (Ans)
***************************************************
$INCLUDE UNIVERSE.INCLUDE ODBC.H
** Initialise parameters ODBC
Ans = -1
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, dataSource, userName , password )
If status = SQL.ERROR
Then
Call DSLogInfo("Connection KO " , "ExecSQLProc")
Ans = -1
Goto Finally
End Else
Call DSLogInfo("Connection OK " , "ExecSQLProc")
End
** Execution request (here, my stored procedure )
status = SQLAllocStmt(hConn, hStmt)
Stmt= "exec MyStoredProc ;"
Call DSLogInfo("request SQL: " : Stmt, "ExecSQLProc")
status = SQLExecDirect(hStmt, Stmt)
If status<>SQL.SUCCESS AND status<>SQL.SUCCESS.WITH.INFO
Then
Call DSLogInfo("Request KO ", "ExecSQLProc")
Ans = -1
Goto Finally
End Else
Call DSLogInfo("Request OK ", "ExecSQLProc")
End
** If my stored proc return a result , i put it in 'res' (=> Ans)
status = SQLBindCol(hStmt, 1, SQL.B.DEFAULT, res)
status = SQLFetch(hStmt)
If status<>SQL.SUCCESS AND status<>SQL.SUCCESS.WITH.INFO
Then
Call DSLogInfo("Result KO", "ExecSQLProc")
Ans = -1
Goto Finally
End Else
Call DSLogInfo("Result OK ", "ExecSQLProc")
Call DSLogInfo("The result is " : res, "ExecSQLProc")
Ans = 0
End
** Free allocated ressources
Finally:
status = SQLFreeStmt(hStmt, SQL.DROP)
status = SQLDisconnect(hConn)
status = SQLFreeConnect(hConn)
status = SQLFreeEnv(hEnv)
***************************************************
** FUNCTION ExecSQLProc(dataSource,userName,password)
==> RETURN (Ans)
***************************************************
$INCLUDE UNIVERSE.INCLUDE ODBC.H
** Initialise parameters ODBC
Ans = -1
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, dataSource, userName , password )
If status = SQL.ERROR
Then
Call DSLogInfo("Connection KO " , "ExecSQLProc")
Ans = -1
Goto Finally
End Else
Call DSLogInfo("Connection OK " , "ExecSQLProc")
End
** Execution request (here, my stored procedure )
status = SQLAllocStmt(hConn, hStmt)
Stmt= "exec MyStoredProc ;"
Call DSLogInfo("request SQL: " : Stmt, "ExecSQLProc")
status = SQLExecDirect(hStmt, Stmt)
If status<>SQL.SUCCESS AND status<>SQL.SUCCESS.WITH.INFO
Then
Call DSLogInfo("Request KO ", "ExecSQLProc")
Ans = -1
Goto Finally
End Else
Call DSLogInfo("Request OK ", "ExecSQLProc")
End
** If my stored proc return a result , i put it in 'res' (=> Ans)
status = SQLBindCol(hStmt, 1, SQL.B.DEFAULT, res)
status = SQLFetch(hStmt)
If status<>SQL.SUCCESS AND status<>SQL.SUCCESS.WITH.INFO
Then
Call DSLogInfo("Result KO", "ExecSQLProc")
Ans = -1
Goto Finally
End Else
Call DSLogInfo("Result OK ", "ExecSQLProc")
Call DSLogInfo("The result is " : res, "ExecSQLProc")
Ans = 0
End
** Free allocated ressources
Finally:
status = SQLFreeStmt(hStmt, SQL.DROP)
status = SQLDisconnect(hConn)
status = SQLFreeConnect(hConn)
status = SQLFreeEnv(hEnv)
-
- Participant
- Posts: 54607
- Joined: Wed Oct 23, 2002 10:52 pm
- Location: Sydney, Australia
- Contact:
-
- Participant
- Posts: 4
- Joined: Mon Apr 30, 2007 8:35 am
Exactly ! if I have errors, I see it only in Datastage logs. Perhaps you can interpreted the routine's answers ....ray.wurlod wrote:... except that the error handling is abysmal, so you had better hope that you never get any errors. ...
But I wanted just to show how to create a connexion SQL in Datastage Basic.