gpt4 book ai didi

sql-server - 如何使用 SQL Server 将存储过程的结果存储在变量中

转载 作者:行者123 更新时间:2023-12-02 18:16:55 25 4
gpt4 key购买 nike

我正在处理一个 SQL 查询,我必须将存储过程的结果存储到字符串类型变量中。有一个名为 SP_CheckAgentProperty 的存储过程,它返回字符串类型的值“y”、“N”或“NP”。

我正在将一个整数值传递给存储过程。我想将此输出存储在任何字符串变量中。为此,我使用以下 SQL 查询:

我的存储过程是:

CREATE Procedure [dbo].[SP_CheckAgentProperty] --12
(
@ID bigint =null
)
As
BEGIN
------Calculating total Ads Post allowed of any specific package of any user-----
DECLARE @Ad int=(SELECT tblPackage.Ads FROM tblPayment_Details INNER JOIN tblPayments ON tblPayments.ID =
tblPayment_Details.Payment_ID INNER JOIN tblPackage ON tblPayments.Package_ID = tblPackage.ID
WHERE (tblPayment_Details.Payment_ID =(SELECT MAX(ID) AS d FROM tblPayments AS tblPayments_1 WHERE (User_ID = @ID))))
print @Ad
------Calculating the date when the user makes the last payment------
DECLARE @St DATE=(SELECT tblPayment_Details.Date FROM tblPayment_Details INNER JOIN tblPayments ON
tblPayments.ID = tblPayment_Details.Payment_ID INNER JOIN tblPackage ON tblPayments.Package_ID = tblPackage.ID
WHERE (tblPayment_Details.Payment_ID =(SELECT MAX(ID) AS d FROM tblPayments AS tblPayments_1 WHERE (User_ID = @ID))))
print @St
------Calculating the validity of specific package taken by any user-----
DECLARE @LT int=(SELECT tblPackage.Validity FROM tblPayment_Details INNER JOIN tblPayments ON tblPayments.ID =
tblPayment_Details.Payment_ID INNER JOIN tblPackage ON tblPayments.Package_ID = tblPackage.ID
WHERE (tblPayment_Details.Payment_ID =(SELECT MAX(ID) AS d FROM tblPayments AS tblPayments_1 WHERE (User_ID = @ID))))
print @LT
print dateadd(DAY,@LT,@St)
-------Calculating the Remaining days of package taken by the user
DECLARE @NoOfDays int=(select DATEDIFF(DAY,GETDATE(),dateadd(DAY,@LT,@St)))
print @NoOfDays
-------Calculating if the user makes does not any payment in history------
DECLARE @SS int=(ISNULL(DATEDIFF(DAY, GETDATE(), @St), 0))
IF(@SS='0')
BEGIN
select 'NP' as Message
END
ELSE
BEGIN
if(@NoOfDays<=0)
BEGIN
--select 'This User Does Not Make a Payment.' as Message
select 'MP' as Message
END
ELSE
BEGIN
DECLARE @TOT int=(select count(*) from tblProperty where tblProperty.Date between @St and dateadd(DAY,@LT,@St))
--group by tblProperty.ID
--select count(*) from tblProperty where tblProperty.Date between '2015-07-04' and dateadd(DAY,20,'2015-07-04')
IF(@TOT<@Ad)
BEGIN
select 'y' as Message
END
ELSE
BEGIN
select 'N' as Message
END
END
END
END

我正在使用上面的存储过程,如下所示:

declare @ss varchar(10)

exec @ss = SP_CheckAgentProperty 10

if(@ss='NP')
BEGIN
print 'Not Payment'
END
else
BEGIN
print 'Payment'
END

上面的查询返回适当的结果,但是当我在 if 条件下使用它的输出时,它不起作用。

最佳答案

如果过程通过选择值来“返回”值,则必须使用 insert into ,如下所示:

declare @ss table (ss varchar(10))

insert into @ss exec SP_CheckAgentProperty 10

if(exists (select 1 from @ss where ss='NP') ...

或者如果它有输出参数,那么调用应该是:

declare @ss varchar(10)

exec SP_CheckAgentProperty 10, @ss output

if(@ss='NP')

关于sql-server - 如何使用 SQL Server 将存储过程的结果存储在变量中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31423660/

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