BEGIN CASE assetTypeNo
WHEN 0 THEN
(
SELECT *
FROM
books
)
WHEN 1
THEN
SELECT *
FROM magazines
END;
END
In creating stored procedures in mySQL.Is this possible? If so. Please help. I can't find the answer anywhere.
try this,
DELIMITER $$
CREATE PROCEDURE ProcName
BEGIN
IF assetTypeNo = 0 THEN
SELECT *
FROM ;
ELSEIF assetTypeNo = 1 THEN
SELECT *
FROM magazines
END;
END $$
DELIMITER ;
The syntax was incorrect. Try this script -
BEGIN
CASE assetTypeNo
WHEN 0 THEN
SELECT * FROM books;
WHEN 1 THEN
SELECT * FROM magazines;
END CASE;
END
Try this one. This should work.
CREATE DEFINER = 'root'#'%' PROCEDURE `test_proc`(
IN assetTypeNo INTEGER(11)
)
NOT DETERMINISTIC
CONTAINS SQL
SQL SECURITY DEFINER
COMMENT ''
BEGIN
CASE assetTypeNo
WHEN 0 THEN SELECT * FROM `books`;
WHEN 1 THEN SELECT * FROM `magazines` ;
ELSE
BEGIN
END;
END CASE;
END;
Related
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 ;
Following is a simple stored procedure to calculate male count from a table , I have declared a variable total_count inside the proc where i'm storing my result.
DELIMITER //
CREATE PROCEDURE GetMaleCount()
BEGIN
DECLARE total_count INT DEFAULT 0 ;
SELECT COUNT(STUDENT_ID) INTO total_count
FROM [table1]
where STUDENT_GENDER = 'M' ;
END //
DELIMITER ;
call GetMaleCount();
select #total_count as tc;
When i executed this procedure i'm getting NULL as the answer, but when i seperately executed just the inner sql query i got the right answer 1852. have i declared the variable in the wrong way ?
total_count that you've declared is visible only in procedure. That is why it is NULL outside of it. You need to use OUT parameter when defining procedure:
DELIMITER //
CREATE PROCEDURE GetMaleCount(OUT total_count INT)
BEGIN
SELECT COUNT(STUDENT_ID) INTO total_count
FROM [table1]
where STUDENT_GENDER = 'M' ;
END //
DELIMITER ;
call GetMaleCount(#total_count);
select #total_count as tc;
You need to use OUT parameter.
DELIMITER //
CREATE PROCEDURE GetMaleCount(OUT total_count INT)
BEGIN
SELECT COUNT(STUDENT_ID) INTO total_count
FROM [table1]
where STUDENT_GENDER = 'M' ;
END //
DELIMITER ;
call GetMaleCount(#total_count);
select #total_count as tc;
I'm trying to insert into a table if the value doesn't exist by using IF EXISTS THEN. Everytime I try to execute this query
DELIMITER //
CREATE PROCEDURE addnTag
BEGIN
IF (EXISTS (SELECT * FROM Tag WHERE Tag.Tag = 'tt'))
THEN SELECT -1;
ELSE
INSERT INTO Tag(Tag) VALUES('tt')
SELECT last_insert_id();
END IF;
END//
DELIMITER ;
I got the
error (1064) 'BEGIN IF (EXISTS (SELECT * FROM Tag WHERE Tag.Tag =
'tt')) THEN SELECT -1; ELSE' at line 2 '
I can't find where is the error in my query.
Please help me. Thanks.
I think you are just missing the parentheses after the procedure name, and the semi-colon after your INSERT statement.
This should work:
DELIMITER //
CREATE PROCEDURE addnTag()
BEGIN
IF (EXISTS (SELECT * FROM Tag WHERE Tag.Tag = 'tt'))
THEN
SELECT -1;
ELSE
INSERT INTO Tag(Tag) VALUES('tt');
SELECT last_insert_id();
END IF;
END//
DELIMITER ;
Hello_
I don't get errors with this syntax:
DELIMITER //
DROP PROCEDURE IF EXISTS addnTag //
CREATE PROCEDURE addnTag()
BEGIN
IF (EXISTS (SELECT * FROM Tag WHERE Tag.Tag = 'tt'))
THEN SELECT -1;
ELSE
INSERT INTO Tag(Tag) VALUES('tt');
SELECT last_insert_id();
END IF;
END //
DELIMITER ;
You were missing () on function declaration and the semicolon in the end of your INSERT statement.
why won't this run? Complains of error when I try to create the procedure.
CREATE PROCEDURE `xxx_testIF`
(
IN _memberId int
)
BEGIN
IF _memberId = 0
select * from incident_report
ELSE
select * from incident_report where filed_by = _memberId
END IF;
END
Always use Delimiters while creating procedures and also you have missing THEN after the IF condition.
Try this:
DELIMITER $$
CREATE PROCEDURE `xxx_testIF`
(
IN _memberId int
)
BEGIN
IF _memberId = 0 THEN
select * from incident_report;
ELSE
select * from incident_report where filed_by = _memberId;
END IF;
END$$
DELIMITER ;
How does one check if a select statement inside a stored procedure returns any rows.
select * from creditcards;
If sqlcod = 0 THEN
I'd like to do something like this for example but sqlcod doesn't seem to work in MySql.
try using COUNT,
DELIMITER $$
CREATE PROCEDURE procName()
BEGIN
SET #recCount = (select count(*) from creditcards);
If #recCount = 0 THEN
-- statement here ;
END IF;
END $$
DELIMITER ;
Try using like below also:
Use FOUND_ROWS() and SQL_CALC_FOUND_ROWS:
DELIMITER $$
create procedure myproc()
begin
SELECT SQL_CALC_FOUND_ROWS *
FROM tbl_name
WHERE id > 100
LIMIT 10;
if SELECT FOUND_ROWS() = 0 then
-- log away bro!
end if;
end $$
DELIMITER $$