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 ;
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 ;
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.
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 $$
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;
I am running a stored procedure. The issue seems to be that it will go into the if statement. Also for some reason or another regardless of how many selects I use it will only return the first. I've copied this from another stored procedure that works like a charm, but this one just won't go. Any ideas?
DROP PROCEDURE IF EXISTS genSelPriceTier;
DELIMITER $$
CREATE PROCEDURE genSelPriceTier(tier_id INT, default_id INT)
BEGIN
DECLARE rowCount INT DEFAULT 0;
SELECT * FROM price_tier WHERE price_tier_id = tier_id;
SET rowCount = FOUND_ROWS();
IF rowCount < 1 THEN
SELECT * FROM price_tier WHERE price_tier_id = default_id;
END IF;
END$$
DELIMITER ;
There is a bug reported related to the usage of FOUND_ROWS(). So, I recommend using Count(*) for the number of rows returned. Something like the following should work.
DROP PROCEDURE IF EXISTS genSelPriceTier;
DELIMITER $$
CREATE PROCEDURE genSelPriceTier(tier_id INT, default_id INT)
BEGIN
DECLARE rowCount INT DEFAULT 0;
SELECT COUNT(*) INTO rowCount FROM price_tier WHERE price_tier_id = tier_id
IF rowCount < 1 THEN
SELECT * FROM price_tier WHERE price_tier_id = default_id;
END IF;
END$$
DELIMITER ;