This is one of those "can it be done" questions. I've got a stored procedure that could take quite a few input parameters so I was wondering if I can establish one input parameter and then set variables based off the comma separated input. Example...
drop procedure if exists sptest;
delimiter $$
create procedure sptest (v1 varchar(254))
begin
if v1=1 then set #vx1:='test1';end if;
if v1=2 then set #vx2:='test2';end if;
if v1=3 then set #vx3:='test3';end if;
if v1=4 then set #vx4:='test4';end if;
select v1;
select #vx1,#vx2,#vx3,#vx4;
end
call sptest('1,2,3,4');
If possible, any examples/guidance would be appreciated.
MySQL comes with a string function named FIND_IN_SET. I think that's your answer. Here's how it works for you:
DROP PROCEDURE IF EXISTS sptest;
DELIMITER $$
CREATE PROCEDURE sptest (v1 VARCHAR(254))
BEGIN
IF FIND_IN_SET('1', v1) THEN SET #vx1:='test1'; END IF;
IF FIND_IN_SET('2', v1) THEN SET #vx2:='test2'; END IF;
IF FIND_IN_SET('3', v1) THEN SET #vx3:='test3'; END IF;
IF FIND_IN_SET('4', v1) THEN SET #vx4:='test4'; END IF;
SELECT v1;
SELECT #vx1, #vx2, #vx3, #vx4;
END$$
CALL sptest('1,2,3,4');
Related
I'm trying to create a stored procedure for deleting a record in my table.
I have tried the following but it doesn't seem to work:
Delimiter //
CREATE PROCEDURE BIRD_STRIKE_INCIDENT_DELETE #row text
AS
BEGIN
DELETE FORM bird_strike.incidents WHERE row_names = #row
END
delimiter ;
Call BIRD_STRIKE_INCIDENT_DELETE('11')
Can someone provide pointers on what I might be doing wrong here?
Thanks!
Your code looks more like SQL Server than MySql
This should be so
Delimiter //
CREATE PROCEDURE BIRD_STRIKE_INCIDENT_DELETE (_row text)
BEGIN
DELETE FROM bird_strike.incidents WHERE row_names = _row;
END//
delimiter ;
You can consider this approach.
IF EXISTS(SELECT 1 FROM sys.procedures
WHERE Name = 'BIRD_STRIKE_INCIDENT_DELETE')
BEGIN
DROP PROCEDURE dbo.BIRD_STRIKE_INCIDENT_DELETE;
END;
else
BEGIN
CREATE PROCEDURE BIRD_STRIKE_INCIDENT_DELETE (_row text)
BEGIN
DELETE FROM bird_strike.incidents WHERE row_names = _row;
END
END;
DELIMITER $$
USE `skilltest`$$
CREATE PROCEDURE Validate_User1($Username NVARCHAR(20), $_Password NVARCHAR(20))
BEGIN
DECLARE $UserId INT;DECLARE $LastLoginDate DATETIME;
IF EXISTS(SELECT (Username) FROM Users WHERE Username=$Username)
THEN
BEGIN
IF EXISTS(SELECT * FROM Users WHERE Username=$Username AND _Password=$_Password)
THEN
BEGIN
SELECT -1 -- UserValidate
END$$
ELSE
BEGIN
SELECT -2 -- Password Wrong
END$$
END$$
ELSE
BEGIN
SELECT -3 -- USERNAME DOES NOT EXIST
END$$
END$$
DELIMITER ;
Assuming this is mysql (there are elements in the code which suggest sql server) every statement must terminate with ; , a comment should be preceeded by # (not --), every if must have a matching end if; , begin..end blocks are not required in the if...endif blocks, only one else is allowed per if, every begin must have a matching end;
, only 1 end $$ is required. Although not required syntactically indenting your code makes it much more readable for us humans.
The following at least syntaxes
DROP PROCEDURE IF EXISTS Validate_User1;
DELIMITER $$
CREATE PROCEDURE Validate_User1($Username NVARCHAR(20), $_Password NVARCHAR(20))
BEGIN
DECLARE $UserId INT;DECLARE $LastLoginDate DATE;
IF EXISTS(SELECT Username FROM Users WHERE Username=$Username) THEN
IF EXISTS(SELECT * FROM Users WHERE Username=$Username AND _Password=$_Password) THEN
SELECT -1; #UserValidate
ELSE
SELECT -2; #Password Wrong
END IF;
ELSE
SELECT -3; #USERNAME DOES NOT EXIST
END IF;
END $$
DELIMITER ;
Here is my Trigger
DELIMITER $$
CREATE TRIGGER before_insert BEFORE INSERT ON SampleTable
FOR EACH ROW
BEGIN
IF NEW.flag =0 THEN
SET NEW.media = "../trigger/smile1.png";
ELSEIF NEW.flag = 1 THEN
SET NEW.media = "../trigger/smile2.png";
ELSEIF NEW.flag = 2 THEN
SET NEW.media = "../trigger/smile3.png";
END IF;
CALL Media (NEW.flag);
END$$
DELIMITER ;
The trigger was created successfully. And I called stored procedure from a trigger.
procedure
DELIMITER $$
CREATE PROCEDURE Media (a INT)
BEGIN
SELECT firstname, lastname, media FROM SampleTable WHERE flag = a;
END $$
DELIMITER ;
When I use insert query it shows the following error.
not allowed to return a result set from a trigger
I found select query is a reason for the error. But I don't know how to solve it.
Any Help...
I would like to know why my code is not working. The ideia is to verify that when a idseq is added to a certain table, to see if that same idseq already exists in any other table.
The following code produces ERROR 1235 in mySQL:
delimiter //
CREATE TRIGGER trigger_pagina
BEFORE INSERT ON pagina
FOR EACH ROW
BEGIN
IF (EXISTS(
SELECT R.idseq
FROM registo R
WHERE (NEW.idseq = R.idseq)
) )THEN
CALL Ilegal_Insert();
END IF;
END;
BEGIN
IF (EXISTS(
SELECT T.idseq
FROM tipo_registo T
WHERE (NEW.idseq = T.idseq)
)) THEN
CALL Ilegal_Insert();
END IF;
END;
BEGIN
IF (EXISTS(
SELECT V.idseq
FROM valor V
WHERE (NEW.idseq = V.idseq)
)) THEN
CALL Ilegal_Insert();
END IF;
END;
BEGIN
IF (EXISTS(
SELECT C.idseq
FROM campo C
WHERE (NEW.idseq = C.idseq)
)) THEN
CALL Ilegal_Insert();
END IF;
END;//
delimiter ;
Thanks in advance !
You need another, outermost BEGIN ... END block to enclose the ones you have above, like
delimiter //
CREATE TRIGGER trigger_pagina
BEFORE INSERT ON pagina
FOR EACH ROW
BEGIN -- Add a BEGIN here...
BEGIN
IF (EXISTS(
...
END IF;
END;
BEGIN
...
END;
END; -- and an END here
//
delimiter ;
This is because CREATE TRIGGER actually only allows the trigger body to contain a single statement, and BEGIN ... END is treated, along with its contents, as a single statement.
So yes, you can (and in this case must) use nested BEGIN ... END blocks in a trigger body.
I am trying to run a query which I first typed out like this:
DELIMITER $$
DROP PROCEDURE IF EXISTS development $$
CREATE PROCEDURE development()
BEGIN
DECLARE i INT DEFAULT 1;
WHILE i <= 500 DO
IF NOT EXISTS ((SELECT * FROM customers WHERE customerID = 500)) THEN INSERT INTO customers (firstName) VALUES (''); END IF;
SET i = i + 1;
END WHILE;
END $$
CALL development() $$
DROP PROCEDURE IF EXISTS development $$
DELIMITER ;
but then "compressed" into this:
DELIMITER $$ DROP PROCEDURE IF EXISTS development $$ CREATE PROCEDURE development() BEGIN DECLARE i INT DEFAULT 1; WHILE i <= 500 DO IF NOT EXISTS ((SELECT * FROM customers WHERE customerID = 500)) THEN INSERT INTO customers (firstName) VALUES (''); END IF; SET i = i + 1; END WHILE; END $$ CALL development() $$ DROP PROCEDURE IF EXISTS development $$ DELIMITER ;
on one line. The problem is that the first (multiline) code works, and does what it's supposed to do, while the other (single line) version, doesn't. It doesn't fail or throw errors, it just doesn't insert the rows like the multiline version. Why is this? More importantly, how can I make the single line version work?
Thanks in advance!
please try it this way:
DELIMITER $$
DROP PROCEDURE IF EXISTS development $$ CREATE PROCEDURE development() BEGIN DECLARE i INT DEFAULT 1; WHILE i <= 500 DO IF NOT EXISTS ((SELECT * FROM customers WHERE customerID = 500)) THEN INSERT INTO customers (firstName) VALUES (''); END IF; SET i = i + 1; END WHILE; END $$ CALL development() $$ DROP PROCEDURE IF EXISTS development $$
DELIMITER ;
The last line break might not be neccessary.
But I am fairly confident that, when inlining the whole code you end up setting a delimiter much longe than "$$"
I just executed the completely inlined example on a random sql server of mine and always received a positive result, even though I have not set up the related db structure.
with line breaks there are complaints about missing tables (as would be expected)
Regards
Stefan
You cannot run any SQL on the same line as delimiter. delimiter is a built in mysql client command that sets the statement seperator, not an sql interpreter. It takes one argument, read up to the first space or newline. Everything else is ignored.
delimiter $$ select now() $$
No output, because everything after $$ is thrown away.
Or illustrated by bad syntax
delimiter "$$" select; nothing order by from nowhere group , oh forget it $$