用case删除数据行

时间:2014-02-04 14:27:09

标签: sql sql-server stored-procedures

我创建了一个简单的存储过程来更新我的表,但目前我需要的是

Delete * from [ABCSystem].[dbo].[NEW_TEST_NUMBER] 
WHERE sONbr = @sONbr AND SOLine = @SOLine

如果使用大小写@Statuscode =“N001”。有人可以给我看一个简单的样品吗?

谢谢。

ALTER PROCEDURE [dbo].[usp_Testing] 
-- Add the parameters for the stored procedure here
@sONbr nvarchar(50) = NULL,
@SOLine nvarchar(50) = NULL,
@SerialNbr nvarchar(50) = NULL,
@StatusCode nvarchar(50) = NULL,
@PackType nvarchar(50) = NULL,
@PalletID nvarchar(50) = NULL,
@PackingListNo nvarchar(50) = NULL,
@CrDateTime nvarchar(50) = NULL,
@CrUserID nvarchar(50) = NULL,
@return nvarchar(50) = NULL OUTPUT
AS
BEGIN
-- SET NOCOUNT ON added to prevent extra result sets from
-- interfering with SELECT statements.
SET NOCOUNT ON;

-- Insert statements for procedure here
IF EXISTS(
            SELECT sONbr , SOLine
            FROM [ABCSystem].[dbo].[NEW_TEST_NUMBER]
            WHERE sONbr = @sONbr AND SOLine = @SOLine
          )
BEGIN
    UPDATE [ABCSystem].[dbo].[NEW_TEST_NUMBER]
    SET     StatusCode = @StatusCode
            ,LastUpdDateTime = GETDATE()
            ,LastUpdUserID = @CrUserID
            ,StatusDesc = 
            CASE @StatusCode WHEN 'N001' THEN 'New'
                             WHEN 'PR001' THEN 'Prepack In Progress'
                             WHEN 'PR002' THEN 'PrePacking Completed'
                             WHEN 'WE002' THEN 'Weight Complete'
            END

            ,PalletID =
            CASE @StatusCode WHEN 'N001' THEN cast(null as nvarchar(50))
            ELSE PalletID
            END

            ,PackType =
            CASE @StatusCode WHEN 'N001' THEN cast(null as nvarchar(50))
            ELSE PackType
            END

            ,JobID =
            CASE @StatusCode WHEN 'N001' THEN  cast(null as nvarchar(50))
            ELSE JobID
            END

            ,JobCrDateTime =
            CASE @StatusCode WHEN 'N001' THEN  cast(null as nvarchar(50))
            ELSE JobCrDateTime
            END

            ,PackingListNo =
            CASE @StatusCode WHEN 'N001' THEN  cast(null as nvarchar(50))
            ELSE PackingListNo
            END

    WHERE sONbr = @sONbr AND SOLine = @SOLine

    IF @@ERROR <> 0
      Set @Return = 'UPDATE FAILED!'
    ELSE
      Set @Return = 'UPDATE SUCCESSFULLY.'
END
ELSE
BEGIN
   Set @Return = 'NO DATA EXIST!'
END

1 个答案:

答案 0 :(得分:0)

就我的问题而言,你必须创建一个简单的表来告诉你INSERT,UPDATE,DELETE。链接存储过程中的表。这将帮助您解决问题。

OR

简单地在存储过程中的循环中对所有内容进行排序,告诉

WHEN @StatusCode = 'N001'
DO update 

ELSE 
DO something else