我正在尝试创建一个SQL语句,根据select语句的结果将多行插入表中。
这样做的目的是我们有2个表,其中应包含与相同数据相关的数据行。我创建了一个语句来识别一个表中不在另一个表中的所有行,以及第二个语句将这些行插入到第二个表中。
这两个单独解决我遇到的问题是当我尝试将它们组合起来希望一次运行所有插入时它成功但仅在第一行结果
IF OBJECT_ID('tempdb..#retentiontemp') IS NOT NULL
/* Then it exists */
DROP TABLE #retentiontemp
Select *
Into #retentiontemp
From
(SELECT VCHRNMBR,dex_row_id
FROM JCRFP100
WHERE VCHRNMBR NOT IN (SELECT VCHRNMBR FROM JC10500)
) As VCHRNMBR
declare @VENDORID char(15),@VCHRNMBR char(20), @DOCTYPE smallint, @BACHNUMB char(15), @ACTINDX int, @TAXDTLID char(15), @TAXAMNT numeric(19,5), @ORTAXAMT numeric(19,5), @PCTAXAMT numeric(19,5), @ORPURTAX numeric(19,5), @TDTTXPUR numeric(19,5), @ORTXBPUR int, @TXDTTPUR numeric(19,5), @ORTOTPUR numeric(19,5), @POSTED tinyint;
select
@VENDORID = VENDORID,
@VCHRNMBR = VCHNUMWK,
@DOCTYPE = DOCTYPE,
@BACHNUMB = BACHNUMB,
@TAXDTLID = Taxschid,
@TAXAMNT = TAXAMNT,
@TDTTXPUR = CURTRXAM,
@ORTXBPUR = CURTRXAM,
@TXDTTPUR = CURTRXAM,
@ORTOTPUR = CURTRXAM,
@ORTAXAMT = @TAXAMNT,
@PCTAXAMT = @TAXAMNT,
@ORPURTAX = @TAXAMNT,
@POSTED = POSTED
from jcrfp100 where dex_row_id in (select dex_row_id from #retentiontemp)
If @TAXAMNT Is Null
set @ACTINDX = '27'
Else
set @ACTINDX = '28'
INSERT INTO PM10500 (VENDORID, VCHRNMBR, DOCTYPE, BACHNUMB, TAXDTLID, BKOUTTAX, TAXAMNT, ORTAXAMT, PCTAXAMT, ORPURTAX, FRTTXAMT, ORFRTTAX, MSCTXAMT, ORMSCTAX, ACTINDX, TRXSORCE, TDTTXPUR, ORTXBPUR, TXDTTPUR, ORTOTPUR, CURRNIDX, POSTED) VALUES ( @VENDORID, @VCHRNMBR, @DOCTYPE, @BACHNUMB, @TAXDTLID, '0', @TAXAMNT, @ORTAXAMT, @PCTAXAMT, @ORPURTAX, '0', '0', '0', '0', @ACTINDX, '0', @TDTTXPUR, @ORTXBPUR, @TXDTTPUR, @ORTOTPUR, '0', @POSTED)
INSERT INTO JC10500 (VENDORID, VCHRNMBR, DOCTYPE, BACHNUMB, TAXDTLID, BKOUTTAX, TAXAMNT, ORTAXAMT, PCTAXAMT, ORPURTAX, FRTTXAMT, ORFRTTAX, MSCTXAMT, ORMSCTAX, ACTINDX, TRXSORCE, TDTTXPUR, ORTXBPUR, TXDTTPUR, ORTOTPUR, CURRNIDX, POSTED) VALUES ( @VENDORID, @VCHRNMBR, @DOCTYPE, @BACHNUMB, @TAXDTLID, '0', @TAXAMNT, @ORTAXAMT, @PCTAXAMT, @ORPURTAX, '0', '0', '0', '0', @ACTINDX, '0', @TDTTXPUR, @ORTXBPUR, @TXDTTPUR, @ORTOTPUR, '0', @POSTED)
select * from PM10500 where VCHRNMBR in (select VCHRNMBR from #retentiontemp)
select * from JC10500 where VCHRNMBR in (select VCHRNMBR from #retentiontemp)
答案 0 :(得分:0)
变量一次只能存储一个值,因此当您使用INSERT INTO - VALUES
语句时,只会输入一行。
请尝试使用INSERT INTO - SELECT
。如下所示:
IF OBJECT_ID('tempdb..#retentiontemp') IS NOT NULL
/* Then it exists */
DROP TABLE #retentiontemp
Select *
Into #retentiontemp
From
(SELECT VCHRNMBR,dex_row_id
FROM JCRFP100
WHERE VCHRNMBR NOT IN (SELECT VCHRNMBR FROM JC10500)
) As VCHRNMBR
INSERT INTO PM10500 (VENDORID, VCHRNMBR, DOCTYPE, BACHNUMB, TAXDTLID, BKOUTTAX, TAXAMNT, ORTAXAMT, PCTAXAMT, ORPURTAX, FRTTXAMT, ORFRTTAX, MSCTXAMT, ORMSCTAX, ACTINDX, TRXSORCE, TDTTXPUR, ORTXBPUR, TXDTTPUR, ORTOTPUR, CURRNIDX, POSTED)
SELECT
VENDORID, VCHNUMWK, DOCTYPE, BACHNUMB, Taxschid, '0', TAXAMNT, TAXAMNT, TAXAMNT, TAXAMNT, '0', '0', '0', '0', CASE WHEN TAXAMNT IS NULL THEN '27' ELSE '28' END, '0', CURTRXAM, CURTRXAM, CURTRXAM, CURTRXAM, '0' POSTED FROM jcrfp100 WHERE dex_row_id in (SELECT dex_row_id FROM #retentiontemp)
INSERT INTO JC10500 (VENDORID, VCHRNMBR, DOCTYPE, BACHNUMB, TAXDTLID, BKOUTTAX, TAXAMNT, ORTAXAMT, PCTAXAMT, ORPURTAX, FRTTXAMT, ORFRTTAX, MSCTXAMT, ORMSCTAX, ACTINDX, TRXSORCE, TDTTXPUR, ORTXBPUR, TXDTTPUR, ORTOTPUR, CURRNIDX, POSTED)
SELECT
VENDORID, VCHNUMWK, DOCTYPE, BACHNUMB, Taxschid, '0', TAXAMNT, TAXAMNT, TAXAMNT, TAXAMNT, '0', '0', '0', '0', CASE WHEN TAXAMNT IS NULL THEN '27' ELSE '28' END, '0', CURTRXAM, CURTRXAM, CURTRXAM, CURTRXAM, '0' POSTED FROM jcrfp100 WHERE dex_row_id in (SELECT dex_row_id FROM #retentiontemp)
select * from PM10500 where VCHRNMBR in (select VCHRNMBR from #retentiontemp)
select * from JC10500 where VCHRNMBR in (select VCHRNMBR from #retentiontemp)