我正在尝试创建如下基本存储过程:
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER PROCEDURE [dbo].[spUpdate1]
@registration_id NCHAR(8),
@registration_status INT
AS
BEGIN
UPDATE dbo.Test1
SET Registration_Status = @registration_status
WHERE Registration_id = @registration_id;
END
此存储过程可以正常工作,但是registration_id
不是我想要传递的参数。
我的表中有三列:
Reference_Number, Registration_id, Registration_Status
我希望能够传递Reference_Number
作为参数,该语句从此处从表的相关行中选择registration_id
,然后将其传递给update语句以执行更新
答案 0 :(得分:1)
这就是您想要的吗?基于Reference_Number
获得Registration_id吗?
ALTER PROC [dbo].[spUpdate1]
@registration_id nchar(8),
@registration_status int,
@Reference_Number nchar(8) -- added this
AS
BEGIN
update t2
set Registration_Status = @registration_status
from dbo.Test1 t1
inner join dbo.Test1 t2 on t1.Registration_id = t2.Registration_id
where t1.Reference_Number = @Reference_Number
END
答案 1 :(得分:1)
根据您的评论,您可以执行此操作;
ALTER PROC [dbo].[spUpdate1]
@registration_status int,
@reference_number nvarchar(max)
AS
BEGIN
UPDATE t1
SET registration_status = @registration_status
FROM dbo.Test1 t1
WHERE CAST(t1.reference_number as NVARCHAR(MAX)) = @reference_number
END
尽管我建议将Reference_Number的数据类型更改为nvarchar(max)而不是文本...