0

挿入ストアド プロシージャがあり、ユーザーが送信するパラメーター値に応じて句テーブルを動的に設定したいSELECTと考えています。

*編集、最初に間違ったコード例を投稿しました。お詫び *

CREATE PROC [spInsertLOG_ReqClone]
  (@NewReqID char(8),
   @ReqID char(8),
   @SiteCode char(3),
   @RequestorID char(6),
   @CloneFromPo bit) 

AS BEGIN
DECLARE @ReqCreatedDate datetime = GetDate() 
SET @NewReqID = dbo.fnLOG_ReqIDGenerator(@SiteCode) --Generates New ReqID

INSERT INTO LOG_ReqHeader (

        ReqID,
        ReqCreatedDate,
        RequestDate,
        RequiredByDate

        )
(SELECT
 @NewReqID,
 @ReqCreatedDate,
 @ReqCreatedDate,
 @RequiredByDate


FROM LOG_ReqHeader h
WHERE h.ReqID = @ReqID )


INSERT INTO LOG_ReqLineItems
 (ReqID,
  QtyOrdered,
  UI,
  PartNumber,
  Nomenclature )

If @CloneFromPO = 0  -- Use line items intially entered in ReqLineITems table
BEGIN
(SELECT 
 @NewReqID,
 l.QtyOrdered,
 l.UI,
 l.PartNumber,
 l.Nomenclature

FROM ReqLineItems l 
WHERE l.ReqID = @ReqID)

END

If @CloneFromPO = 1  -- Use line items after PO was issued from PO table

Begin
(SELECT 
 @NewReqID,
 l.Qty_Ord,
 l.UnitOfIssue,
 l.Part_Number,
 l.Item_Desc

FROM PO_LineItems l 
WHERE l.ReqID = @ReqID)

END
END
4

3 に答える 3

0

重複のため、これはあまりきれいな解決策ではありませんが、これはあなたが望むものかもしれません:

    CREATE PROC [spInsertLOG_ReqClone]

(@NewReqID char(8),
@ReqID char(8),
@SiteCode char(3),
@RequestorID char(6),
@CloneFromPo bit) 

IF @CloneFromPo = 0 

BEGIN
    INSERT INTO LOG_ReqHeader ( 
            ReqID,
            ReqCreatedDate,
            RequestDate, SOME_OTHER_FIELDS)
    (SELECT
     @NewReqID,
     @ReqCreatedDate,
     @ReqCreatedDate, SOME_OTHER_FIELDS
        FROM Table_1
        WHERE reqID = @ReqID)

END

IF @CloneFromPo = 1

BEGIN
    INSERT INTO LOG_ReqHeader ( 
            ReqID,
            ReqCreatedDate,
            RequestDate, SOME_OTHER_FIELDS)
    (SELECT
     @NewReqID,
     @ReqCreatedDate,
     @ReqCreatedDate, SOME_OTHER_FIELDS
        FROM Table_1
        WHERE reqID = @ReqID)

END
于 2013-02-20T00:57:59.800 に答える