if else存储过程中的语句?

时间:2010-08-04 04:28:38

标签: c# sql

我有这个存储过程从excell获取数据然后更新状态:

ALTER PROCEDURE [dbo].[sp_AllocateSerial]
@Limit int,
@Part varchar (50),
@Status varchar(50)
AS
BEGIN
-- SET NOCOUNT ON added to prevent extra result sets from
-- interfering with SELECT statements.
SET NOCOUNT ON;

-- Insert statements for procedure here

SELECT TOP (@LIMIT) PartNumber,SerialNumber,Batch,Location,PalletNumber,Status
FROM dbo.FG_FILLIN where Status='FG-FRESH' and PartNumber=@Part ORDER BY PartNumber

END

现在:

如果我的序列号状态已分配,我想显示一条错误消息,表明该序列已被分配。

3 个答案:

答案 0 :(得分:2)

尝试类似这样的东西我大多使用假值,所以你必须改变你的环境。

IF NOT EXISTS (SELECT SerialNumber WHERE SerialNumber = @SerialNumber AND Status = 'Allocated')
BEGIN
//Logic for if the serial number is not allocated
END
ELSE
BEGIN
//Logic for the serial number being in the allocated state
END

答案 1 :(得分:2)

IF EXISTS (SELECT * FROM FG_FILLIN WHERE Status = 'Allocated' AND SerialNumber = @Serial) 
BEGIN

  RAISERROR
    (N'Serial number %s has already been allocated.',
     10, -- Severity.
     1, -- State.
     @Serial)

END

答案 2 :(得分:0)

您可能希望使用out参数,然后让客户端测试而不是sql异常