Is it equivalent oracle cursor parameter feature in mysql? - mysql

Is it equivalent ORACLE cursor parameter feature in MySQL ?
For example :-
CURSOR cursorname(paramter_name datatype) IS SELECT * FROM TABLE_NAME;

You can try the following:
CURSOR select_curs IS SELECT * FROM tbl WHERE id = #id;
set #id = 1;
OPEN ..
FETCH ..
CLOSE ..

like this:
The procedure uses the var_id_res parameter to specify a particular reservation:
This procedure parameter is used to control a cursor to select only items that correspond
CREATE PROCEDURE `UpdatePriceAndVatAndDiscountForReservationItems`(
IN var_id_res INTEGER
)
...to the reservation passed by the parameter:
-- the line reserve curosr
DECLARE cur_res CURSOR FOR
SELECT id_line
, id_prod
, disc_bool
, no_days
FROM line_reserve
WHERE id_res = var_id_res;

Related

MySQL procedure with query string

I am a newbie to using query strings in mysql and I have tried to write this procedure to drop tables under certain conditions. I don't reall know what I'm doing wrong and need help with getting the procedure to work or for someone to point me in the right direction. Thanks.
BEGIN
DECLARE String scheduler = 'select status from mysql.scheduler where id=0' ;
DECLARE String auftragpos = 'SELECT count("SchemaName") FROM "SYS.Tables" where "SchemaName" = dwh and "Name" = lexware_fk_auftragpos';
DECLARE String auftrag = 'SELECT count("SchemaName") FROM "SYS.Tables" where "SchemaName" = dwh and "Name" = lexware_fk_auftrag';
IF(auftragpos > 1)
BEGIN
drop table "dwh.lexware_fk_auftragpos";
END
IF(auftrag > 1)
BEGIN
drop table "dwh.lexware_fk_auftrag";
END
END
The syntax for declaring a variable is
DECLARE variablename datatype;
So it should be
DECLARE auftragpos INT;
Then you need to assign the result of the query to the variable. You do that by putting the SELECT query in parentheses.
You also should not quote table names, and you need to quote the literal strings you're using for the schema and table names you're searching for.
SET auftragpos = (
SELECT count(*)
FROM INFORMATION_SCHEMA.TABLES
WHERE TABLE_SCHEMA = 'dwh' AND TABLE_NAME = 'lexware_fk_auftragpos');
But there's really no need to do this. If you want to prevent an error from DROP TABLE, just add the IF EXISTS option.
DROP TABLE IF EXISTS dwh.lexware_fk_auftragpos;

Removing the last character of a field in mysql

I have a procedure that receives 2 parameters I would like to take the last character of one of these parameters does anyone know how to do, using mysql?
CREATE PROCEDURE `sp_status`(IN status_p CHAR(1), IN codigo_p VARCHAR(255))
BEGIN
UPDATE cartaodigital SET statusPedido = status_p WHERE id_cartaodigital = codigo_p;
END
The function you are looking for is SUBSTRING:
select substring(COLUMNNAME from length(COLUMNNAME))
from TABLENAME;
or
select substring(COLUMNNAME, length(COLUMNNAME))
from TABLENAME;
or
select substring(COLUMNNAME from -1)
from TABLENAME;
or
select substring(COLUMNNAME, -1)
from TABLENAME;

MySQL turn a JOIN statement into a Stored Procedure or Function?

So I have a JOIN statement that I will be using in multiple places. It essentially finds an IP address and matches it to said location from the intermediate table. I need to pass in two variables - one being the prefix of my database/schema and the other being the IP Address itself.
Therefore used is the CONCAT() function in order to piece it together.
So at the moment I have a procedure that looks something like this:
CREATE DEFINER=`root`#`%` PROCEDURE `LocationFromIp`(
ipAddress VARCHAR(16),
clientComp VARCHAR(32)
)
BEGIN
DECLARE test VARCHAR(255);
SET #networkSql = CONCAT("
SET #Location =
(SELECT `network`.`Name`
FROM `", clientComp, "-settings`.`iptable` AS `iptable`
LEFT JOIN `", clientComp, "-settings`.`network` AS `network`
ON `network`.`Subnet` = `iptable`.`Subnet`
WHERE `iptable`.`IP` = '", ipAddress, "'
LIMIT 1);
");
PREPARE test1 FROM #networkSql;
EXECUTE test1;
SELECT #Location AS `Location`;
It returns the result I want however I don't know how I can use this in a statement.
SELECT `IPAddress` AS CALL LocationFromIp('clientComp', `IPAddress`)
(
SELECT `IPAddress`
FROM `clientComp-data`.`tablename`
WHERE #date > `Date`;
)
GROUP BY `IPAddress`
The above does not work but I hope you can understand my thinking!
So how would I do it?
Why can't you adjust your stored procedure to include all the fields that you need from the very beginning?

can we create create statement inside cursor loop in oracle

Can we do CTAS inside a cursor in oracle
I am trying below code
declare
l_email_string varchar2(100);
cursor c1 is
select * from EMAIL_OBS where rownum < 2;
begin
for rec in C1
loop
create table ABC_TEST
(
row_id ,
email_string
)
as
select
rowid ,
jasbk
from EMAIL_OBS ;
end loop ;
end ;
/
but it is showing error while if I remove CTAS then it is working fine
Please suggest
Thanks ,
Abhimpi
You cannot perform DDL in PL/SQL like this (CTAS is DDL). You will need to use Dynamic SQL. Look up 'EXECUTE IMMEDIATE' for examples.

Using Arrays in SQL

I have this sql statement:
DECLARE #option_id INT;
SELECT DISTINCT product_options.option_id
INTO #option_id
FROM product_options,
product_options_descriptions
WHERE product_options.product_id = '31288'
AND product_options_descriptions.option_name = "Color";
SELECT #option_id;
Which works fine. What I would like to do is is use #option_id to select multiple ids into an array. However as far as I can tell DECLARE #var only works with single values and I can't find any information on array datatypes in sql.
In outline I want to do the following
declare #option_id;
SELECT #option_id;
DECLARE #id_array;
SELECT into #id_array WHERE id = #option_id;
DECLARE #return_array;
FOREACH #id IN #id_array {
#return_array[] = SELECT value FROM column where id = #id
}
SELECT #return_array
Does anyone know where I can find tutorials ect to achieve this?
You can use temporary table , table prefixed with #
CREATE TABLE #YourTable (
YourColumn int
)
LInk : http://www.sqlteam.com/article/temporary-tables
For mysql syntax : http://dev.mysql.com/doc/refman/5.1/en/create-table.html