gpt4 book ai didi

mysql - 如何使用子查询作为存储过程的一部分 MySQL

转载 作者:行者123 更新时间:2023-11-28 23:49:14 25 4
gpt4 key购买 nike

我正在尝试创建一个过程(在 phpMyAdmin 中称为例程)并且当前有以下查询:

BEGIN

INSERT INTO `users` (`username`, `password`, `email`, `user_role_id`,` date_registered`)
VALUES (_username, _password, _email, (SELECT `ID` FROM `roles` WHERE `role_name` = _role) , _date);

END;

但这行不通,我已经尝试为 role_ID 声明一个变量,但这也行不通,有人可以帮我解决这个问题吗?

注意

您看到的参数(以下划线 (_) 开头)都是在 phpMyAdmin 例程创建器中使用的 IN 参数

编辑

我试过的其他代码是这样的:

BEGIN

DECLARE role_id AS/*=*/ (SELECT `ID` FROM `roles` WHERE `role_name` = _role); /* tried both AS and = */

INSERT INTO `users` (`username`, `password`, `email`, `user_role_id`, `date_registered`)
VALUES (_username, _password, _email, role_id, _date);

END;

还注意到DECLARE 的一个小错误,将上面的代码更改为:

BEGIN

DECLARE @role_id ;
SET @role_id = (SELECT `ID` FROM `roles` WHERE `role_name` = _role);

INSERT INTO `users` (`username`, `password`, `email`, `user_role_id`, `date_registered`)
VALUES (_username, _password, _email, @role_id, _date);

END;

还是不行

最佳答案

USE [FydaDB]
GO
/****** Object: StoredProcedure [dbo].[CountryManagerReport] Script Date: 5/25/2018 2:12:42 AM ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER PROCEDURE [dbo].[CountryManagerReport]
@FromDate datetime,
@Todate datetime,
@countryId int
AS
BEGIN
Select FirstName+ ' ' + MiddleName+ '' + LastName as FullName ,Email,CNIC,((select CountryCode from Country where ID=FydaAdmin.CountryId)+ '-' +(select PhoneNo from Contacts where FydaAdminId=FydaAdmin.ID)) as Mobile,
(ZipCode+ ' ,' +[Address]+ ' ,' + (select Name from City where ID=FydaAdmin.CityId )+ ', ' +(select Name from States where ID=FydaAdmin.StateId )+ ', ' +(select Name from Country where ID=FydaAdmin.CountryId)
) as [Address]
from FydaAdmin where CreatedDateTime BETWEEN @FromDate and @Todate or CountryId=@countryId
END

关于mysql - 如何使用子查询作为存储过程的一部分 MySQL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32904855/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com