SQL Server equivalent of DECLARE CONTINUE HANDLER in MySQL? - mysql

I am trying to execute following mySql statement
"DECLARE DONE INT(1) default 0;
DECLARE CONTINUE HANDLER FOR NOT FOUND SET DONE = 1;"
in SQL Server 2005 like
" DECLARE #DONE INT,
DECLARE CONTINUE HANDLER FOR NOT FOUND SET DONE = 1"
it gives error like
"Msg 156, Level 15, State 1, Line 1
Incorrect syntax near the keyword 'CONTINUE'.
Msg 136, Level 15, State 1, Line 1
Cannot use a CONTINUE statement outside the scope of a WHILE statement."
MySQL DECLARE ... HANDLER command

SQL Server has no direct equivalent of the handler mechanism described in the documentation linked above. The most obvious alternatives are:
TRY...CATCH...
Write a CLR stored procedure and use .NET code to implement whatever handling code you need; this question also has some useful information

Related

Getting compile time error while deploying application in IBM Integration Toolkit 10

I am getting below error while deploying my application on EG exist on remote integration node.
BIP2087E: Integration node 'IIB10Node' was unable to process the
internal configuration message.
The entire internal configuration message failed to be processed
successfully.
Use the messages following this message to determine the reasons for
the failure. If the problem cannot be resolved after reviewing these
messages, contact your IBM Support center. Enabling service trace may
help determine the cause of the failure.
BIP4041E: Integration server 'EGTesting' received an administration
request that encountered an exception.
While attempting to process an administration request, an exception
was encountered. No updates have been made to the configuration of the
integration server.
Review related error messages to determine why the administration
request failed.
BIP2401E: (com.esql.code.UAE_FTProcess_Transaction.Main, 51.47) :
Syntax error : expected '=' but found '.'.
The expected token was not found.
Correct the syntax of your ESQL expression in node
'com.esql.code.UAE_FTProcess_Transaction.Main', around line and column
'51.47', then redeploy the message flow.
BIP2871I: The request made by user 'iibadmin[Default]' to 'deploy' the
resource
'C:/Users/905293/IBM/IIBT10/Adil_workspace/GeneratedBarFiles/AsyncUAE_FTAppproject.generated.bar'
of type 'BAR' on parent 'EGTesting' of type 'ExecutionGroup' has the
status of 'FAILED'.
Below is my esql code where this compilation error mention line number and file name. My code is mostly commented and no semantic error are showing. I've clean my project and change my workspace several time and deploy but getting same error every time. The line where my error occur on esql are commented still it showing this. I cannot understand this what i could do now. Kindly help.
CREATE COMPUTE MODULE UAE_FTProcess_Transaction
CREATE FUNCTION Main() RETURNS BOOLEAN
BEGIN
DECLARE Schemas CHARACTER 'IIBUAT';
DECLARE Table CHARACTER 'ft_transaction';
DECLARE HEADER_IN CHARACTER '';
DECLARE TRAN_DETAIL CHARACTER '';
DECLARE DR_INFO CHARACTER '';
DECLARE CR_INFO CHARACTER '';
DECLARE SC_DETAIL CHARACTER '';
DECLARE REV_DETAIL CHARACTER '';
DECLARE META_DATA CHARACTER '';
DECLARE XML_REQ CHARACTER '';
DECLARE XML_RESP CHARACTER '';
DECLARE i INT 1;
SET Environment.Variables.FTData[]= SELECT * FROM Database.{Schemas}.{Table} AS A WHERE
A.TRANSACTION_STATUS='N' AND A.TRANSACTION_COUNT=1;
DECLARE ft_data REFERENCE TO Environment.Variables;
WHILE(i<=CARDINALITY(ft_data.FTData[])) DO
DECLARE ft_seq REFERENCE TO ft_data.FTData[i];
-- SET HEADER_IN=
-- '<HEADER_IN><CORR_ID>DAPP888260726808</CORR_ID><USER_NAME>CBS</USER_NAME><CHANNEL>DAPP</CHANNEL><APPLICATION>GNBAQRG</APPLICATION><WORK_STATION>Kci-DigAppBack</WORK_STATION><AUTH_KEY></AUTH_KEY></HEADER_IN>';
-- SET TRAN_DETAIL=
-- '<MSG_TYPE>O</MSG_TYPE><STAN>'||ft_seq.STAN||'</STAN><TRAN_DATETIME>'||ft_seq.TRAN_DATETIME||'</TRAN_DATETIME><TRAN_AMT>'||ft_seq.TRAN_AMT||'</TRAN_AMT><TRAN_DESC>'||ft_seq.TRAN_DESC||'</TRAN_DESC><CHEQUE_NO></CHEQUE_NO><CCY_RATE/>';
-- SET DR_INFO=
-- '<DR_INFO><DR_BANK_CODE>'||ft_seq.DR_BANK_CODE||'</DR_BANK_CODE><DR_BANK_NAME>'||ft_seq.DR_BANK_NAME||'</DR_BANK_NAME><DR_ACCT_TITLE>'||ft_seq.DR_ACCT_TITLE||'</DR_ACCT_TITLE><DR_ACCT_IBAN>'||ft_seq.DR_ACCT_IBAN||'</DR_ACCT_IBAN><DR_ACCT_TYPE>'||ft_seq.DR_ACCT_TYPE||'</DR_ACCT_TYPE><DR_ACCT_NO>'||ft_seq.DR_ACCT_NO||'</DR_ACCT_NO><DR_BRANCH>'||ft_seq.DR_BRANCH||'</DR_BRANCH><DR_CCY>'||ft_seq.DR_CCY||'</DR_CCY><DR_CLIENT_NO>'||ft_seq.DR_CLIENT_NO||'</DR_CLIENT_NO><DR_CRC>PKR</DR_CRC><DR_VALUE_DATE>'||ft_seq.DR_VALUE_DATE||'</DR_VALUE_DATE></DR_INFO>';
--SET CR_INFO=
--'<CR_INFO><CR_BANK_CODE>'||ft_seq.CR_BANK_CODE||'</CR_BANK_CODE><CR_BANK_NAME>'||ft_seq.CR_BANK_NAME||'</CR_BANK_NAME><CR_ACCT_TITLE>'||ft_seq.CR_ACCT_TITLE||'</CR_ACCT_TITLE><CR_ACCT_TYPE>'||ft_seq.CR_ACCT_TYPE||'</CR_ACCT_TYPE><CR_ACCT_NO>'||ft_seq.CR_ACCT_NO||'</CR_ACCT_NO><CR_BRANCH>'||ft_seq.CR_BRANCH||'</CR_BRANCH><CR_CCY>'||ft_seq.CR_CCY||'</CR_CCY><CR_CLIENT_NO>'||ft_seq.CR_CLIENT_NO||'</CR_CLIENT_NO><CR_CRC>PKR</CR_CRC><CR_VALUE_DATE>'||ft_seq.CR_VALUE_DATE||'</CR_VALUE_DATE></CR_INFO>';
SET SC_DETAIL='<SC_DETAIL><ORIG_TRAN_REF></ORIG_TRAN_REF></SC_DETAIL>';
SET REV_DETAIL='<REV_DETAIL><TRAN_SEQ_NO></TRAN_SEQ_NO><TRAN_DATE></TRAN_DATE></REV_DETAIL>';
SET META_DATA='<META_DATA><META_COLUMNS/></META_DATA>';
-- SET XML_REQ='<CBS_TRAN_IN>'||HEADER_IN||TRAN_DETAIL||DR_INFO||
-- CR_INFO||SC_DETAIL||REV_DETAIL||META_DATA||'</CBS_TRAN_IN>';
SET XML_REQ='<CBS_TRAN_IN>'||SC_DETAIL||REV_DETAIL||META_DATA||'</CBS_TRAN_IN>';
-- CALL PROCESS_TRAN(XML_REQ,XML_RESP);
IF(SUBSTRING(SUBSTRING(XML_RESP AFTER '<RESULT_CODE>') BEFORE '</RESULT_CODE>')='0') THEN
UPDATE Database.{Schemas}.{Table} AS B SET B.TRANSACTION_STATUS='A' AND
B.TRANSACTION_COUNT=2 WHERE B.STAN=ft_seq.STAN;
ELSE
UPDATE Database.{Schemas}.{Table} AS B SET B.TRANSACTION_COUNT=2 WHERE B.STAN=ft_seq.STAN;
END IF;
SET i=i+1;
END WHILE;

Querying web services JSON from SQL server

I'm following this tutorial. I created GetWebService stored procedure from tutorial, and example from there works fine
DECLARE #response NVARCHAR(MAX)
EXECUTE dbo.GetWebService 'http://headers.jsontest.com/', #response OUTPUT
SELECT #response
But, when I try to run this:
DECLARE #response NVARCHAR(MAX)
EXECUTE dbo.GetWebService 'https://www.alphavantage.co/query?function=TIME_SERIES_DAILY&symbol=MSFT&apikey=demo', #response OUTPUT
SELECT #response
it fails
Msg 50000, Level 16, State 1, Procedure dbo.GetWebService, Line 48 [Batch Start Line 0]
sp_OAMethod read response failed
How I can fix it?
Don't follow that tutorial. You should use a CLR stored procedure to make web service calls from SQL Server. There are lots of samples out there.
If you are going to use the deprecated and potentially dangerous sp_OAxxx stored procedures, still don't follow that tutorial. It's got issues. Try the implementation in my answer here instead: Set option 9 in SQL Server stored procedure using WinHttp.WinHttpRequest.5.1 for TLS 1.2

Error #1064 when declaring a variable

I am trying to declare and manipulate a variable in MySQL like this:
DECLARE #tim;
//find the difference between current time and time at which the data was stored in the database
SET #tim=(select TIMEDIFF(NOW(),E.reg_date)
FROM `mytable2` E)
But I get the following error:
error:#1064 - You have an error in your SQL syntax; check the manual
that corresponds to your MySQL server version for the right syntax to
use near 'DECLARE #tim' at line 1
What is causing this error and how can I avoid it? The MySQL server version is 5.6.17.
As the error message says, your syntax is wrong.
tl;dr: SET the variable, do not DECLARE it.
DECLARE is only used when working with variables in routines such as stored procedures, functions and triggers. See the documentation:
DECLARE is permitted only inside a BEGIN ... END compound statement
and must be at its start, before any other statements.
You are trying to set a user-defined variable outside of a routine:
One way to set a user-defined variable is by issuing a SET statement:
SET #var_name = expr [, #var_name = expr] ...
Everything you need to know about this statement, including quite a bit of information about setting session and global variables, is in the documentation for SET.
select TIMEDIFF(NOW(),E.reg_date) into #tim FROM mytable2 E

YA MySQL Error #1064 in CALL

I've created the following stored procedure:
CREATE DEFINER=`root`#`localhost` PROCEDURE `ABON_PLATA`(DATE_BAL1 datetime)
BEGIN
DECLARE IMSI1 varchar(6);
DECLARE DATE1 datetime;
DECLARE ID_U1 integer;
DECLARE PAY1 double;
DECLARE PAYSUM double;
DECLARE OLD1 double;
DECLARE REASON1 varchar(16);
DECLARE FLAG integer DEFAULT 0;
DECLARE C1 CURSOR FOR SELECT ID_U FROM podkl_otkl_uslug WHERE IMSI1=IMSI;
DECLARE CONTINUE HANDLER FOR SQLSTATE '02000' SET FLAG=1;
OPEN C1;
REPEAT
FETCH C1 INTO ID_U1;
IF FLAG=0 THEN
SELECT SUM(PAY) INTO PAY1 FROM uslugi WHERE ID_U1=ID_U AND DATE_PODKL<DATE_BAL1 AND DATE_OTKL IS NULL;
SET REASON1 = 'ABON PLATA';
SET DATE1 = DATE_BAL1;
SET PAYSUM = `NEW`-PAY1;
SET OLD1 = `NEW`;
END IF;
INSERT history (`DATE`, REASON, `NEW`, OLD, IMSI) VALUES (DATE1, REASON1, PAYSUM, OLD1, IMSI1);
UNTIL FLAG=1
END REPEAT;
CLOSE C1;
END
It is used to simulate subscribtion fee of mobile operator. As I am trying to call it, I get the notorious error:
CALL ABON_PLATA(2013-07-07 12:00:00);
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '12:00:00)' at line 1
Considering it is an error at line 1, I get extremely confused. What super obvious am I missing?
Datetime literals must be in quotes.
CALL ABON_PLATA('2013-07-07 12:00:00');
The reason it reports an error at line 1 is that it's the CALL line that contains an error. It has nothing to do with your procedure code, because the error happens before it can even call your procedure.
Re your comment:
Thread stack overrun: 6892 bytes used of a 131072 byte stack, and 128000 bytes needed. Use 'mysqld --thread_stack=#' to specify a bigger stack.
This indicates that your MySQL has a configured value of thread_stack that is too small to run statements in Stored Procedures. This can happen, for example, if you use the my-small.cnf configuration file that is bundled with some releases of MySQL.
Increase the value of thread_stack in your configuration file. The default is 256K, and it's only the my-small.cnf that sets it lower. There has been a bug logged about this setting being too low.
Don't forget to restart your MySQL service after you make this change to the config file.

EXCEPTION WHEN OTHERS equivalent in MySQL

I am trying to port an Oracle trigger to MySQL. There's an EXCEPTION WHEN OTHERS statement in the trigger and while I have found equivalent statements for everything else, I cannot find one for this. The trigger is something like:
IF (yada yada)
THEN
BEGIN
SELECT a
INTO generic_variable1
FROM table
WHERE condition;
SET generic_variable2 = value1;
EXCEPTION WHEN OTHERS --this part needs to be replaced by valid MySQL syntax
SET generic_variable2 = value2;
END;
END IF;
How do I convert that part into MySQL?
You should understand that MySQL has a very limited stored proc / trigger language compared to Oracle. While porting to MySQL, many Oracle users say over and over again, "I can't believe MySQL can't do X!!!!"
The closest thing to EXCEPTION WHEN OTHERS may be DECLARE CONTINUE HANDLER.
Example (not tested):
DECLARE CONTINUE HANDLER FOR SQLEXCEPTION SET #generic_variable2 = 1;
But you'd declare that before the block of code that might throw the exception, not after.
See http://dev.mysql.com/doc/refman/5.5/en/declare-handler.html for full docs.