作者:肾虚养生 | 来源:互联网 | 2023-05-17 18:08
SoIvefinallydecidedtogetaroundtolearninghowtousestoredprocedures,andalthoughIdoha
So I've finally decided to get around to learning how to use stored procedures, and although I do have them working, I'm unsure if I'm doing it correctly - aka. the best way. So here's what I've got.
所以我最终决定开始学习如何使用存储过程,虽然我确实让它们正常工作,但我不确定我是否正确地做了 - 也就是说。最好的方法。所以这就是我所拥有的。
Three procedures: TryAddTag, CheckTagExists, and AddTag.
三个程序:TryAddTag,CheckTagExists和AddTag。
TryAddTag is the procedure that is my intermediary between other code (eg. PHP, etc...) and the other two procedures, so this is the one that gets called.
TryAddTag是我在其他代码(例如PHP等)和其他两个过程之间的中介的过程,因此这是被调用的过程。
TryAddTag
DELIMITER //
CREATE PROCEDURE TryAddTag(
IN tagName VARCHAR(255)
)
BEGIN
-- Check if tag already exists
CALL CheckTagExists(tagName, @doesTagExist);
-- If it does not exist, add it
IF @doesTagExist = FALSE THEN
CALL AddTag(tagName);
END IF;
END //
DELIMITER ;
AddTag
DELIMITER //
CREATE PROCEDURE AddTag(
IN tagName VARCHAR(255)
)
BEGIN
INSERT INTO
tags
VALUES(
NULL,
tagName
);
END //
DELIMITER ;
CheckTagExists
DELIMITER //
CREATE PROCEDURE CheckTagExists(
IN
tagName VARCHAR(255),
OUT
doesTagExist BOOL
)
BEGIN
-- Check if tag exists
SELECT
EXISTS(
SELECT
*
FROM
tags
WHERE
tags.NAME = tagName
)
INTO
doesTagExist;
END //
DELIMITER ;
My problems stem from this and use of @doesTagExist.
我的问题源于此并使用@doesTagExist。
-- Check if tag already exists
CALL CheckTagExists(tagName, @doesTagExist);
Is the the correct way to use one of these variables? And/or, how can I use a DECLARE'd variable to store the result of CheckTagExists within TryAddTag? I expected something along the lines of
使用这些变量之一的正确方法是什么?和/或,如何使用DECLARE变量在CheckAddTag中存储CheckTagExists的结果?我期待的是某些东西
...
DECLARE doesTagExist BOOL;
SET doesTagExist = CheckTagExist('str');
...
or something like that...
或类似的东西...
4 个解决方案