2

I am trying to create a procedure (called a routine in phpMyAdmin) and currently have this query:

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;

But this does not work, I have tried to declare a variable to a role_ID and this also did not work, can anyone help me to fix this please?

NOTE

The params you see (starting with an underscore (_)) are all IN params used in the phpMyAdmin routine creator

EDIT

The other code I have tried is this:

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;

Also noticed a small error with the DECLARE, changed the above code to:

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;

But still no luck

4

2 回答 2

1
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
于 2018-05-25T09:49:02.957 回答
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!

于 2015-10-02T10:56:16.557 回答