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

时间:2015-10-02 10:01:30

标签: mysql stored-procedures phpmyadmin subquery routines

我正在尝试创建一个过程(在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,这也没有用,有人可以帮我解决这个问题吗?

您看到的params(以下划线(_)开头)都是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;

但仍然没有运气

2 个答案:

答案 0 :(得分:0)

我已经解决了如何执行此操作 - 虽然不使用子查询,但我有这个存储过程:

BEGIN

INSERT INTO `users` (`username`, `password`, `email`, `user_role_ID`, `date_registered`)
    VALUES (username, pwd, email, role, datenow);

END

和这个PHP函数和类一起工作:

class create_user
{
    public static function CreateUser($username, $password, $email, $role)
    {
        global $link;
        #region Get role id
        $role_q = "SELECT `ID` FROM `roles` WHERE `role_name` = '{$role}';";
        $role_rs = mysqli_fetch_row($link->query($role_q));
        $role = $role_rs[0];
        #endregion

        #region Create user
        $link->query("CALL sp_create_user('{$username}', '" . hash("sha256", $password) . "', '{$email}', '{$role}', '" . date("y-m-d H:i:s") . "')") ? $inserted = true : $inserted = false;
        #endregion

        #region check if user created
        if ($inserted)
        {
            return true;
        }
        else
        {
            return "User was not created. Either the username or email is already in use.";
        }
        #endregion
    }
}

如果有人可以使用子查询执行此操作,那么这将是正确的答案,因为这可行,但不是我的问题的完整答案。

快乐的SQLing!

答案 1 :(得分:0)

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