我正在尝试将此查询转换为MySQL函数
SET @v1 := (
SELECT count(id) as count
FROM article_category
where (title like "About Usd")
or (title like "About Usd-%" and title regexp '[0-9]$')
);
INSERT INTO `article_category`
(`title`,`meta_data`,`meta_description`)
VALUES
( IF(@v1 <= 0, "About Usd", CONCAT("About Usd","-",@v1) ),
"ddddD",
"ddddddd" );
此函数应返回下一个可用名称,以便我可以直接在insert语句中使用它:
CREATE FUNCTION `get_next_unique_name`(tabel nvarchar(255),naem nvarchar(255)) RETURNS NVARCHAR(255)
DETERMINISTIC
BEGIN
SET @dd = SELECT count(id) as count FROM tabel where (title like naem) or (title like (naem+'-%') and title regexp '[0-9]$');
RETURN (naem+'-'+@dd);
END
答案 0 :(得分:2)
您必须在SET @dd
行上收到错误。
您不能通过指定语句将值设置为任何变量
您必须使用SELECT .... INTO ...
语法为变量设置值。
您的代码中还有其他几个错误 按照以下建议更改它们:
CREATE
FUNCTION `get_next_unique_name`( tabel nvarchar(255), naem nvarchar(255) )
RETURNS NVARCHAR(255)
DETERMINISTIC
BEGIN
DECLARE _count INT DEFAULT 0;
SELECT count(id) INTO _count
FROM tabel
WHERE ( title LIKE naem )
OR ( title LIKE CONCAT( naem, '-%' ) AND title REGEXP '[0-9]$' );
RETURN CONCAT( naem, '-', _count );
END;