我有以下表格
TableA(Field1,Field2)
TableB(Field3,Field4)
现在我在这些表中选择值并返回。但是,如果TableB
中的字段3为null
,则返回错误。
我的程序如下
create proc name
@Field1 varchar,
@Field2 varchar,
@Field3 varchar out,
@Field4 varchar out,
@Error varchar out
AS
BEGIN
SELECT @Field2 = tA.Field2,
@Field3 = tA.Field3,
FROM tA C INNER JOIN TableB tB ON
tA.Field1 = tB.Field1
WHERE tA.Field4 = @Field4
如果字段3为空,我不知道如何返回错误
我正在考虑为此表编写另一个查询为null的查询
答案 0 :(得分:0)
您可以使用RAISEERROR来使用已定义的错误 More on RAISEERROR
create proc name
@Field1 varchar,
@Field2 varchar,
@Field3 varchar out,
@Field4 varchar out,
@Error varchar out
AS
BEGIN
SELECT @Field2 = tA.Field2,
@Field3 = tA.Field3,
FROM tA C INNER JOIN TableB tB ON
tA.Field1 = tB.Field1
WHERE tA.Field4 = @Field4
if @field is null
begin
RAISERROR ('Fields 3 cannot be null .', -- Message text.
16, -- Severity.
1 -- State.
);
end
else
select @Field2 as Field2, @Field3 as Field3
答案 1 :(得分:0)
您可以尝试使用此代码来引发错误消息:
BEGIN TRY
IF @Field3 IS NULL
RAISERROR ('Fields 3 cannot be null .', 16,1);
END TRY
BEGIN CATCH
SET @Error = ERROR_MESSAGE()
END CATCH