简体   繁体   English

使用 Laravel 从 Microsoft SQL 存储过程获取 output 参数值

[英]Getting output parameter value from Microsoft SQL stored procedure using Laravel

I am trying to insert a record into the Microsoft SQL database via store procedure using Laravel and get the value from an output parameter.我正在尝试使用 Laravel 通过存储过程将记录插入 Microsoft SQL 数据库,并从 output 参数中获取值。 My record is inserted but did not get @RetValue .我的记录已插入但没有得到@RetValue For this, I tried为此,我尝试了

DB::select("DECLARE @RetValue INT; SELECT @RetValue as 'return'; EXEC AddDistrict 'somevalue', 1, @RetValue OUTPUT; SELECT @RetValue as 'abc';");
DB::select(DB::Raw("Declare @RetValue int EXEC AddDistrict 'somevalue', '1', @RetValue OUTPUT"),'select @RetValue as abc');
DB::select(DB::statement('CALL AddDistrict("DName"="asdfasdf", "PID"=1,"RetValue"="" )').'@RetValue as RetValue');
DB::select(" EXEC AddDistrict ?, ?", array( 'some_value',1));
DB::select(DB::raw("exec AddDistrict @DName = 'some_value', @PID = 1, @RetValue=''"));
DB::select('CALL AddDistrict(?, ?, ?)',
     array(
        'DName' => $request->DistrictName,
        'PID' => $request->province,
         'RetValue' => ''
        ));
DB::select('exec AddDistrict(?,?)',"some_value',1);

and many others but did not get the @RetValue.和许多其他人,但没有得到@RetValue。 mostly I get an empty array like this [].大多数情况下,我得到一个像这样的空数组 []。

My Store Procedure looks like我的存储过程看起来像

CREATE PROCEDURE [dbo].[AddDistrict]
    
    @DName nvarchar(50)
    ,@PID int
    
    ,@RetValue int output
    
AS
BEGIN
    -- SET NOCOUNT ON added to prevent extra result sets from
    -- interfering with SELECT statements.
    SET NOCOUNT ON;

    -- Insert statements for procedure here
    if not exists(select * from District where District_Name = @DName)

begin
    INSERT INTO [dbo].District
           ([District_Name],ProvienceID)
     VALUES (@DName,@PID)
     set @RetValue=1;
     end
     else
     begin
     set @RetValue=-1;
     end
END

I want if the record inserts then give me 1 if did not insert then give me -1 as described in the SP我想要如果记录插入然后给我 1 如果没有插入然后给我 -1 如 SP 中所述

I found a solution to that.我找到了解决方案。 Maybe it's not the proper way.也许这不是正确的方法。 But my code is now running smoothly after the below changes.但经过以下更改后,我的代码现在可以顺利运行了。 Just remove this variable @RetValue int output and replace set @RetValue=1;只需删除此变量@RetValue int output并替换set @RetValue=1; to SELECT 1;SELECT 1;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM