Unabe to run DB2 Stored Procedure through VB script - stored-procedures

I'm breaking my head for the past three days in calling a DB2 stored procedure from VB Script. I can able to call a SP that is simple but not the one with more IN/OUT parameters.
I use the below code to add parameters for the SP.
oADO_CMD.CommandType = 4
oADO_CMD.CommandText = "schema.sp_name"
oADO_CMD.Parameters.Refresh
Const adVarChar = 200
Const adParamInput = 1
Const adParamOutput = 2
oADO_CMD.Parameters.append oADO_CMD.CreateParameter("test", adVarChar, adParamInput, 4, "1000")
oADO_CMD.Parameters.append oADO_CMD.CreateParameter("tester", adVarChar, adParamInput, 6, "")
when i run in front end i use the below query and it works fine.
call schema.sp_name('1000','')
Everything works fine in one SP but when i use the same method for another SP it just throws me error. But in front end when i give the same data, i can receive the records.
When i looked into the SP about the error im getting it is related to preparing cursor. Seriously i'm not sure what it is because im not very good in SP's. Is this anything about passing values from vbs to DB2 SP ?. Because there are some parameters where i need to pass empty values within double quotes.. or some other thing that causes error... ?
Here is some lines from SP.
CreateSQL.
------------,
MOVE SPACES TO lc_SQLStatement.
EXEC SQL
SET :lc_SQLStatement = TRIM(:lc_SelectClause) || " " ||
TRIM(:lc_FromClause) || " " ||
TRIM(:lc_WhereClause)
END-EXEC.
STRING lc_SelectClause DELIMITED BY SIZE
MOVE SPACES TO lc_SQLStatement.
EXEC SQL
SET :lc_SQLStatement = TRIM(:lc_SelectClause) || " " ||
TRIM(:lc_FromClause) || " " ||
TRIM(:lc_WhereClause)
END-EXEC.
STRING lc_SelectClause DELIMITED BY SIZE
lc_FromClause DELIMITED BY SIZE
lc_WhereClause DELIMITED BY SIZE
INTO lc_SQLStatement
END-STRING.
CreateDynamCursor.
-------------------
EXEC SQL
PREPARE cu_SearchCR FROM :lc_SQLStatement
END-EXEC.
EVALUATE SQLCODE
WHEN 0
CONTINUE
WHEN OTHER
MOVE lnc_04 TO pc_ReturnCode
MOVE lnc_PrepCurErr TO pc_RetCodeDescription
END-EVALUATE.
I'm getting the pc_ReturnCode and pc_RetCodeDescription values as "04" and "error while preparing cu_SearchCR".

Related

How to create an update query with Open Office Base?

I want to create basically an update query on Open Office Base (the same way with Ms ACCESS).
Base does not typically use update queries (but see below). Instead, the easiest way to do an update command is to go to Tools -> SQL. Enter something similar to the following, then press Execute:
UPDATE "Table1" SET "Value" = 'BBB' WHERE ID = 0
The other way is to run the command with a macro. Here is an example using Basic:
Sub UpdateSQL
REM Run an SQL command on a table in LibreOffice Base
Context = CreateUnoService("com.sun.star.sdb.DatabaseContext")
databaseURLOrRegisteredName = "file:///C:/Users/JimStandard/Desktop/New Database.odb"
Db = Context.getByName(databaseURLOrRegisteredName )
Conn = Db.getConnection("","") 'username & password pair - HSQL default blank
Stmt = Conn.createStatement()
'strSQL = "INSERT INTO ""Table1"" (ID,""Value"") VALUES (3,'DDD')"
strSQL = "UPDATE ""Table1"" SET ""Value"" = 'CCC' WHERE ID = 0"
Stmt.executeUpdate(strSQL)
Conn.close()
End Sub
Note that the data can also be modified with a form or by editing the table directly.
Under some circumstances it is possible to create an update query. I couldn't get this to work with the default built-in HSQLDB 1.8 engine, but it worked with MYSQL.
In the Queries section, Create Query in SQL View
Click the toolbar button to Run SQL Command directly.
Enter a command like the following:
update mytable set mycolumn = 'This is some text.' where ID = 59;
Hit F5 to run the query.
It gives an error that The data content could not be loaded, but it still performs the update and changes the data. To get rid of the error, the command needs to return a value. For example, I created this stored procedure in MYSQL:
DELIMITER $$
CREATE PROCEDURE update_val
(
IN id_in INT,
IN newval_in VARCHAR(100)
)
BEGIN
UPDATE test_table SET value = newval_in WHERE id = id_in;
SELECT id, value FROM test_table WHERE id = id_in;
END
$$
DELIMITER ;
Then this query in LibreOffice Base modifies the data without giving any errors:
CALL update_val(2,'HHH')
See also:
https://forum.openoffice.org/en/forum/viewtopic.php?f=5&t=75763
https://forum.openoffice.org/en/forum/viewtopic.php?f=61&t=6655
https://ask.libreoffice.org/en/question/32700/how-to-create-an-update-query-in-base-sql/
Modifying table entries from LibreOffice Base, possible?

Squirrel Client DB2 Stored Procedure Session Delimiter

I am facing a tricky problem while I am trying to run stored procedure via Squirrel against DB2 database.
In the stored procedure have part where combine an sql statement like this:
SET V_SQL = 'SELECT DISTINCT ' || PARAM_COLUMNNAME || ' FROM '||PARAM_TABLENAME||' WHERE '||PARAM_COLUMNNAME||'<'||PARAM_NUMBER||';';
I changed the session statement paramater ; --> # to run properly the procedure call, but I get error message:
An unexpected token "" was found following "". Expected tokens may
include: "WHERE REP_ID<201506".. SQLCODE=-104, SQLSTATE=42601,
DRIVER=3.59.81 SQL Code: -104, SQL State: 42601
I guessed it is because of the delimeter ';' of the inside sql script so I changed the code like this:
SET V_SQL = 'SELECT DISTINCT ' || PARAM_COLUMNNAME || ' FROM '||PARAM_TABLENAME||' WHERE '||PARAM_COLUMNNAME||'<'||PARAM_NUMBER||'#';
Then I get this message:
The numeric literal "201506#" is not valid.. SQLCODE=-103,
SQLSTATE=42604, DRIVER=3.59.81 SQL Code: -103, SQL State: 42604
Do you have any idea?
Squirrel: SQuirreL SQL Client snapshot-20150623_2101 DB2: 9.5
Thanks and Cheers.
So finally what for me worked it was the comment sign:
SET V_SQL = 'SELECT DISTINCT ' || PARAM_COLUMNNAME || ' FROM '||PARAM_TABLENAME||' WHERE '||PARAM_COLUMNNAME||'<'||PARAM_NUMBER||';';--
The explanation I am not sure why pass through in this way the database engine, but worked.
Thanks!

DB2 error : Keyword SELECT not expected. Valid tokens: FOR WITH WITHOUT

I am trying to execute the following stored procedure on db2 database using iSeries Navigator:
CREATE PROCEDURE ZSPPQDELETEUSERIDBA (
IN P_USERID CHAR(10) ,
IN P_BUSINESSAREA CHAR(10))
DYNAMIC RESULT SETS 1
LANGUAGE SQL
SPECIFIC ZSPPQDELETEUSERIDBA
NOT DETERMINISTIC
MODIFIES SQL DATA
CALLED ON NULL INPUT
SET OPTION ALWBLK = *ALLREAD ,
ALWCPYDTA = *OPTIMIZE ,
COMMIT = *CS ,
CLOSQLCSR = *ENDMOD ,
DECRESULT = (31, 31, 00) ,
DFTRDBCOL = *NONE ,
DYNDFTCOL = *NO ,
DYNUSRPRF = *USER ,
RDBCNNMTH = *RUW ,
SRTSEQ = *HEX
P1 : BEGIN
DECLARE TOTALCNT INTEGER DEFAULT 0 ;
DECLARE C1 CURSOR WITH RETURN TO CALLER
SELECT Count (*) FROM USERBUSINESSAREA
WHERE USERID = P_USERID
AND BUSINESSAREA = P_BUSINESSAREA;
OPEN C1;
FETCH C1 INTO TOTALCNT;
IF ( TOTALCNT = 1) THEN
DELETE
FROM USERBUSINESSAREA
WHERE USERID = P_USERID
AND BUSINESSAREA = P_BUSINESSAREA;
END IF;
END P1 ;
I am getting the following error when I try to execute the above procedure:
SQL State: 42601
Vendor Code: -199
Message: [SQL0199] Keyword SELECT not expected. Valid tokens: FOR WITH WITHOUT. Cause . . . . . : The keyword SELECT was not expected here. A syntax error was detected at keyword SELECT. The partial list of valid tokens is FOR WITH WITHOUT. This list assumes that the statement is correct up to the unexpected keyword. The error may be earlier in the statement but the syntax of the statement seems to be valid up to this point. Recovery . . . : Examine the SQL statement in the area of the specified keyword. A colon or SQL delimiter may be missing. SQL requires reserved words to be delimited when they are used as a name. Correct the SQL statement and try the request again.
What's going wrong here?
I found out where the problem is.I was supplying the wrong input values for the stored procedure(i.e one in place of another..data types are different). That caused the issue. Now it is resolved

Calling Oracle 11g Stored Procedure Using VB6

I have a simple Oracle procedure as below. I am trying to call the procedure using VB6 and extract the output from the procedure.
CREATE OR REPLACE PROCEDURE EXTRACTTXN (reportdate IN DATE, p_recordset OUT SYS_REFCURSOR) AS
BEGIN
OPEN p_recordset FOR
SELECT
TXN_ID,
TXN_ACTION,
TXN_STATUS,
TXN_DATE,
TXN_AMOUNT
FROM TRANSACTIONS
WHERE
TRUNC(TXN_DATE) = TRUNC(reportdate)
END EXTRACTTXN;
The VB Code goes like this;
Sub FetchTransactions(ByVal ReportDate As Date, cnnMine as ADODB.Connection)
On Error GoTo TrapErr
Dim cmdMine As ADODB.Command, rsMine As ADODB.Recordset
cmdMine.ActiveConnection = cnnMine
cmdMine.CommandTimeout = 300
cmdMine.CommandType = adCmdStoredProc
cmdMine.CommandText = "EXTRACTTXN"
cmdMine.Parameters.Append cmdMine.CreateParameter("reportdate", adDate, adParamInput, , Format(ReportDate, "DD-MMM-YYYY"))
cmdMine.Parameters.Append cmdMine.CreateParameter("p_recordset", adVariant, adParamOutput)
Set rsMine = cmdMine.Execute
Do While rsMine.EOF
Debug.Print rsMine!TXN_ID, rsMine!TXN_ACTION, rsMine!TXN_STATUS, rsMine!TXN_DATE, rsMine!TXN_AMOUNT
rsMine.MoveNext
Loop
rsMine.Close
Exit Sub
TrapErr:
MsgBox Err.Number & " - " & Err.Description, vbExclamation, App.ProductName
End Sub
While running the code, I get the following Error:
ORA-06550: line 1, column 7:
PLS-00306: wrong number or types of arguments in call to 'EXTRACTTXN'
ORA-06550: line 1, column 7:
PL/SQL: Statement ignored
Anything wrong in my code? Appreciate help.
Niz
The problem is that the types of your arguments as specified in your VB code don't match the types of the arguments as specified in your PL/SQL code. The most likely reason for your problem is that the Format function in VB6 returns a Variant type, not a Date type, and that Variant type is set to be a String type. See this for more information on the Format function.
In case you don't know, the way that Variant variables are set up is that they reserve 8 bytes to tell the world what the actual variable type is. So, if you pass your ReportDate in after applying the Format function to it, it will be a Variant that's telling the world it's a string. It's possible that the ADO Parameter object is happy with that (SQL Server is happy to parse properly-formatted strings into Date fields, after all) and Oracle isn't. In my limited experience with Oracle, I've found that it's fussier about that sort of thing than SQL Server.
Try losing the Format function and see if you at least get a different error.
I have managed to get this sorted. It's mainly due to me being new to Oracle and its complexity.
Here are the changes I made;
Stored Procedure Changes. Note that I have changed TRUNC(reportdate, 'DD') on the Where clause.
CREATE OR REPLACE PROCEDURE EXTRACTTXN (reportdate IN DATE, p_recordset OUT SYS_REFCURSOR) AS
BEGIN
OPEN p_recordset FOR
SELECT
TXN_ID,
TXN_ACTION,
TXN_STATUS,
TXN_DATE,
TXN_AMOUNT
FROM TRANSACTIONS
WHERE
TRUNC(TXN_DATE) = TRUNC(reportdate, 'DD')
END EXTRACTTXN;
VB Code Changes (Note that I have change the CommandText within parenthesis with a Call, removed the parameter name, changed the date format to DD/MMM/YYYY and removed the output parameter)
Sub FetchTransactions(ByVal ReportDate As Date, cnnMine as ADODB.Connection)
On Error GoTo TrapErr
Dim cmdMine As ADODB.Command, rsMine As ADODB.Recordset
cmdMine.ActiveConnection = cnnMine
cmdMine.CommandTimeout = 300
cmdMine.CommandType = adCmdStoredProc
cmdMine.CommandText = "{ call EXTRACTTXN}"
cmdMine.Parameters.Append cmdMine.CreateParameter(, adDate, adParamInput, , Format(ReportDate, "DD/MMM/YYYY"))
Set rsMine = cmdMine.Execute
Do While rsMine.EOF
Debug.Print rsMine!TXN_ID, rsMine!TXN_ACTION, rsMine!TXN_STATUS, rsMine!TXN_DATE, rsMine!TXN_AMOUNT
rsMine.MoveNext
Loop
rsMine.Close
Exit Sub
TrapErr:
MsgBox Err.Number & " - " & Err.Description, vbExclamation, App.ProductName
End Sub
The above worked perfectly.
Regards, Niz

Sybase ASE - INSERT INTO statment i stored procedure, problems in formating string

I have following problem formatting the #p_f_field variable correct, I get the error:
Could not execute statement.
Incorrect syntax near ‘+’
Sybase error code=102, SQLState=”42000”
Severity Level=15, State=181, Transaction State=1
Line 7
My stored procedure:
create proc dbo.sp_m_efi_dw_full_dsa_table_load_im ( -- parameter examples:
#p_skm_name varchar(4096),
#p_usr_name varchar(4096),
#p_t_name_dsa varchar(4096),
#p_t_name_bas varchar(4096),
#p_f_name varchar(4096),
#p_f_field varchar(4096)
)
as begin
declare #sql varchar(4096)
if #p_skm_name is not null
begin
if #p_usr_name is not null
begin
set #sql='TRUNCATE TABLE '+#p_skm_name+'.'+#p_usr_name+'.'+#p_t_name_dsa
exec (#sql)
end
end
set #sql='INSERT INTO '+#p_skm_name+'.'+#p_usr_name+'.'+#p_t_name_dsa+' '+#p_f_name+
' VALUES '+#p_f_field
exec (#sql)
end
My call to the stored procedure:
execute BAS_efi.dbo.sp_m_efi_dw_full_dsa_table_load_im
#p_skm_name = 'B_ef',
#p_usr_name = 'dbo',
#p_t_name_dsa = 'a_log',
#p_t_name_bas = 'a_log',
#p_f_name = '(NEWFIELD1)',
#p_f_field = '('+char(39)+'daf9af01-6bc2-11e-b23182b0623e'+char(39)+')'
Any suggestions on how to format the variable #p_f_field correct, or any others suggestions on how to execute this simple INSERT INTO procedure?

Resources