如何为存储过程中的变量赋值 [英] how to assign value to variable in stored procedure
本文介绍了如何为存储过程中的变量赋值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
ALTER proc [dbo].[Sp_Update_OPD_Docket]
@Docket_no varchar(50)
as
Declare @active tinyint
set @active=select IsActive from tbl_OPDappointment where Docket_no=@Docket_no
if (@active<>3)
BEGIN
update tbl_OPDappointment set IsActive=3 where Docket_No=@Docket_no
END
i只想分配价值选择commant到变量active.plz帮助我。我,有需要。
提前致谢
i just want to assign value of select commant to variable active.plz help me out. i , in need.
Thanks in advance
推荐答案
自从您的存储过程只是检查IsActive值是否为3,如果不是,则更新为3,您可以使用此代码实现相同的功能:
Since your stored procedure simply checks whether the IsActive value is 3, and if it''s not, update to 3, you could achieve the same thing with this code:
update tbl_OPDappointment set IsActive=3
where Docket_No=@Docket_no
and IsActive<>3
请通过_Damian S_读取解决方案1。这是非常好的。
请更改您的查询:
Please, read solution1 by _Damian S_. It''s very good.
Please, change your query:
Declare @active tinyint
set @active=select IsActive from tbl_OPDappointment where Docket_no=@Docket_no
with:
with:
Declare @active tinyint
select @active=COALESCE(IsActive, 0) from tbl_OPDappointment where Docket_no=@Docket_no
--show value
select @active As IsActive
我使用 COALESCE [ ^ ]函数用默认值替换NULL(在本例中为0)。
I use COALESCE[^] function to replace NULLs with default value (in this case with 0).
这篇关于如何为存储过程中的变量赋值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文