如果查看下面的代码,我将使用8个SET语句从Temp插入表中。我要做的是让代码存在以运行脚本,并在每次执行后更改SET值。我有一份2500+逆转付款清单。设置Set语句组并分别运行每个语句将花费很长时间。我希望有人可以帮助我完成一个更简单的过程。在实际插入之前看到将要写入的所有数据也很高兴。
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 =@Tran_No
SELECT * from @InsertRequest --(Used to view dataset being Inserted)
--Begin Tran
--Insert INTO Fuji..Cortland_Event
--SELECT * FROM @InsertRequest
----Commit
----rollback
答案 0 :(得分:0)
我假设您需要插入的值在Excel网格中(或者可以导入到一个网格中)。您可以使用Excel将每个行的插入sql构建到表@InsertRequest中。例如,假设我有一个Excel网格:
Col1 | Col2 | Col3
x | y | z
创建一个使用以下公式附加它们的列:
=CONCATENATE("insert @InsertRequest values ('",A2, "', '",B2,"', '",C2, "')")
将产生:
insert @InsertRequest values ('A', 'B', 'C')
然后切割&超过你脚本中的2500多个插入内容。