我有一个插入存储过程,我想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