我在我的一个应用程序中使用以下程序
ALTER procedure [dbo].[FeeRecordSelect]
@GRNo varchar(4),
@PaymentModeId numeric(2) output
as
begin
SELECT @PaymentModeId = Students.PaymentModeId FROM Students where Students.GRNo = @GRNo
end
GO
我的问题是:我想在@PaymentModeId中选择1,如果我没有得到@GRNo的结果
我已经尝试过这个
create procedure [dbo].[FeeRecordSelect10]
@GRNo varchar(4),
@PaymentModeId numeric(2) output
as
begin
SELECT @PaymentModeId = isnull(Students.PaymentModeId, 1) FROM Students where Students.GRNo = @GRNo
end
GO
答案 0 :(得分:2)
IsNull()不起作用的原因是因为结果集不包含任何行。
ALTER procedure [dbo].[FeeRecordSelect]
@GRNo varchar(4),
@PaymentModeId numeric(2) output
as
begin
set @PaymentModeId = isnull((select Students.PaymentModeId FROM Students where Students.GRNo = @GRNo), 1)
end
GO
答案 1 :(得分:1)
放
Set @PaymentModeId = 1
在选择之前将默认值设置为1.现在,如果没有选择行,它仍将返回1.
编辑:
ALTER procedure [dbo].[FeeRecordSelect]
@GRNo varchar(4),
@PaymentModeId numeric(2) output
as
begin
Set @PaymentModeId = 1
SELECT @PaymentModeId = Students.PaymentModeId FROM Students where Students.GRNo = @GRNo
end
GO