1
如果您查看下面的代碼,我使用8個SET語句從Temp插入表中。我期望做的是讓代碼存在來運行腳本,並在每次執行後更改SET值。我有2500+沖銷付款的列表運行。設置Set語句組並分別運行每個語句將需要很長時間。我希望有人能以更簡單的過程幫助我。在實際插入之前查看將要寫入的所有數據也很好。想要使用多個SET語句從temptable插入多行
DECLARE
@C_Id int, @CA_Id int,
@Event_Type_Code int, @Event_Type_Detail_Code int,
@Term_No int, @OriginalAmount money, @NewAmount money,
@Tran_No int
SET @C_Id = "insert acct # here"
SET @CA_Id = "insert sub acct # here"
SET @Event_Type_Code = 2 -- 2 = Payment
SET @Event_Type_Detail_Code = 13 --12 = positive; 13 = negative
SET @Term_No = 2 --What term is this payment for
SET @OriginalAmount = 1119.55 --Original payment entered
SET @NewAmount = -1119.55 --Reversal payment to negate original payment
SET @Tran_No = 69879237 -- Orig trans we are basing new reversal payment on
Select * from Cortland_Event_Log
where @C_Id = C_Id
and @CA_Id = CA_Id
and @Term_No = Term_No
and Event_Type_Code = 2 -- Payment
and Event_Type_Detail_Code = 12 --Positive Payment
and Message_Completed_Time IS NOT NULL
ORDER BY Event_Creation_Time
DECLARE @InsertRequest TABLE
(
[Event_Type_Code] [int] NOT NULL,
[Event_Type_Detail_Code] [int] NULL,
[C_Id] [int] NOT NULL,
[CA_Id] [smallint] NOT NULL,
[Amount] [money] NULL,
[S_Id] [int] NULL,
[SA_Id] [smallint] NULL,
[Tran_No] [int] NULL,
[F_Extracted] [int] NOT NULL,--really boolean
[Retries] [int] NOT NULL,
[Insert_Time] [datetime] NOT NULL,
[Insert_User] [varchar](50) NOT NULL,
[Update_Time] [datetime] NULL,
[Update_User] [varchar](50) NULL,
[Pmnt_Term] [smallint] NULL,
[School_Term_Id] [varchar](50) NULL,
[Message_Id] [varchar](50) NULL,
[Term_No] [int] NOT NULL,
[HT_Event_Id] [int] NULL
)
INSERT INTO @InsertRequest
SELECT TOP 1
@Event_Type_Code,
@Event_Type_Detail_Code,
C_Id,
CA_Id,
@NewAmount,--Amount,
S_Id,
SA_Id,
Tran_No,
0, --F_Extracted] [int] NOT NULL,--really boolean
0, --[Retries] [int] NOT NULL,
getDate(),--[Insert_Time] [datetime] NOT NULL,
SUSER_SNAME(),--[Insert_User] [varchar](50) NOT NULL,
GETDATE(),--[Update_Time] [datetime] NULL,
SUSER_SNAME(), --[Update_User] [varchar](50) NULL,
NULL,--[Pmnt_Term] [smallint] NULL,
NULL,--[School_Term_Id] [varchar](50) NULL,
NULL,--[Message_Id] [varchar](50) NULL,
@Term_No,--Term_No
NULL--[HT_Event_Id] [int] NULL
FROM TMSEnterprise..CA_Tran_Detl --Fuji..Cortland_Event_Log
WHERE C_Id = @C_Id AND CA_Id = @CA_Id
AND Trans_Amt = @OriginalAmount --If using CA_Tran_Detl in above FROM statement USE "Trans_Amt", If using Cortland_Event_Log USE "Amount"
AND Tran_No [email protected]_No
SELECT * from @InsertRequest --(Used to view dataset being Inserted)
--Begin Tran
--Insert INTO Fuji..Cortland_Event
--SELECT * FROM @InsertRequest
----Commit
----rollback
請註明RDBMS – mstbaum
我很抱歉。我以爲我在那裏。我正在使用SQL Server 2012 –
變量的值來自哪裏?這也是基於集合的查詢/插入的一個很好的介紹。 –