如何在2表&之间运行存储过程查询在参考栏上插入数据库?

时间:2013-12-23 04:54:28

标签: sql sql-server-2008

我已经创建了一个简单的存储过程来更新我的Sample_TAG_NUMBERTEST表,但是我需要它来使用ItemCode和&同时从另一个表PartItem获取SOLineSONbr进行查询,但我不知道如何编写。

我需要做的第二件事是当我的StatusCode列为N001时,我的DESC列会自动插入NEW ORDER数据值,PR002我的DESC列将成为Progress

我该怎么做?提前谢谢。

ALTER PROCEDURE [dbo].[SampleTagNumberUpdate] 
@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,
@ItemCode nvarchar(50) = NULL,
@CrDateTime nvarchar(50) = NULL,
@CrUserID nvarchar(50) = NULL,
@return nvarchar(50) = NULL OUTPUT
AS
BEGIN
SET NOCOUNT ON;

    IF EXISTS(SELECT sONbr , SOLine
              FROM [SampleSystem].[dbo].[Sample_TAG_NUMBERTEST]
              WHERE sONbr = @sONbr AND SOLine = @SOLine)
BEGIN
    UPDATE [SampleSystem].[dbo].[Sample_TAG_NUMBERTEST]
    SET     SerialNbr = @SerialNbr
            ,StatusCode = @StatusCode
            ,PackType = @PackType
            ,PalletID = @PalletID
            ,PackingListNo = @PackingListNo
            ,ItemCode = @ItemCode
            ,LastUpdDateTime = GETDATE()
            ,LastUpdUserID = @CrUserID

    WHERE sONbr = @sONbr AND SOLine = @SOLine

    IF @@ERROR <> 0
      Set @Return = 'UPDATE FAILED'
    ELSE
      Set @Return = 'UPDATE SUCCESSFULLY'
END
ELSE
BEGIN
   INSERT INTO [SampleSystem].[dbo].[Sample_TAG_NUMBERTEST](SONbr, SOLine, SerialNbr
            ,StatusCode
            ,PackType
            ,PalletID
            ,PackingListNo
            ,ItemCode
            ,CrDateTime
            ,CrUserID)
   VALUES(@sONbr, @SOLine, @SerialNbr, @StatusCode, @PackType
            ,@PalletID
            ,@PackingListNo
            ,@ItemCode
            ,GETDATE()
            ,@CrUserID)

   IF @@ERROR <> 0
  Set @Return = 'INSERT DATA FAILED'
   ELSE
      Set @Return = 'INSERT DATA SUCCESSFULLY'
END
END

1 个答案:

答案 0 :(得分:0)

我相信我已经为以下两个问题提供了一个例子:

UPDATE T
   SET ItemCode = (SELECT PI.ItemCode FROM PartItem AS PI WHERE PI.SO = @SO AND PI.SO_LINE = @SO_LINE),  
   DESC = CASE @StatusCode WHEN 'N001' THEN 'NEW ORDER' 
          ELSE CASE @StatusCode WHEN 'PR002' THEN  'PROGRESS' ELSE '' END
          END,
        <other columns here...>
FROM SAMPLE_TAG_NUMBERTEST AS T