TSQL根据select语句的整数结果插入值

时间:2018-07-05 13:46:17

标签: sql sql-server

我正在运行以下查询:

select
    max(count) Max
from
    (select 
         count(iStockID) Count
     from   
         _etblInvJrBatchLines
     group by 
         iStockID) X

您知道,结果是整数;在这种情况下,结果为5。

基于以上结果,我需要将通用值插入另一个表,如下所示:

INSERT INTO _etblInvJrBatches 
(
    cInvJrNumber            --  IJ0001      Plus 1
,   cInvJrDescription       --  Inventory Journal Batch
,   cInvJrReference         --  IJR10001    Plus 1
,   iCreateAgentID          --  1
,   bClearAfterPost         --  1
,   bAllowDupRef            --  1
,   bAllowEditGLContra      --  0
,   iNewLineDateOpt         --  0
,   iNewLineRefOpt          --  0
,   cNewLineRefDef          --  ''
,   bNewLineRefInc          --  0
,   iNewLineDescOpt         --  0
,   cNewLineDescDef         --  ''
,   bNewLineDescInc         --  0
,   iNewLineProjectOpt      --  0
,   iNewLineProjectDefID    --  0
,   iNewLineWarehouseOpt    --  0
,   iNewLineWarehouseDefID  --  0
,   bJustCleared            --  0
,   iTransactionCode        --  31 (select TrCodeID where TrCode = 'ADJ')
)
SELECT 
    'IJ000'             --  Plus 1
,   'Inventory Journal Batch'
,   'IJR1000'               --  Plus 1
,   1
,   1
,   1
,   0
,   0
,   0
,   ''
,   0
,   0
,   ''
,   0
,   0
,   0
,   0
,   0
,   0
,   (select idTrCodes from TrCodes where Code = 'ADJ')

唯一的问题是,它只能插入一次。

如何根据我的第一条select语句获得的结果插入5次?

换句话说,如果整数结果为24,则需要导入/插入上述24次。

感谢您的协助。

Attie。

3 个答案:

答案 0 :(得分:1)

有很多方法可以完成此任务。 INSERT仅将一行插入目标表。最终,您将不得不循环插入。

可能的解决方案...

在数据库中循环...

declare @count int = (select
    max(count) Max
from
    (select 
         count(iStockID) Count
     from   
         _etblInvJrBatchLines
     group by 
         iStockID) X)
    declare @increment int =1
while @increment <= @count 
{
-- do your insert here...
@increment = @increment +1
}

在您的客户端代码中循环。 从您的初始查询中检索计数值

For increment = 1 to @count
'execute SQL to do insert here...
Next increment

或更佳(如@scsimon所暗示)...

For increment = 1 to @count
'build the VALUES () clauses for your insert statement...
Next
'execute your insert statement

我没有意识到T-SQL在INSERT中允许多个VALUES子句。谢谢@scsimon!

如果是我,那么我将使用代码而不是数据库在客户端中完成所有这些操作。我是老学校,我不认为像在我的第一个示例中那样在数据库中构建这样的解决方案时伸缩性很好。

答案 1 :(得分:1)

您不能只使用理货表吗?

declare @count int



 set @count = (select
    max(count) Max
from
    (select 
         count(iStockID) Count
     from   
         _etblInvJrBatchLines
     group by 
         iStockID) X) --Hardcoded 10
IF OBJECT_ID('tempdb..#tally') IS NOT NULL
  /*Then it exists*/
  DROP TABLE #tally
SELECT TOP (@count) --equates to more than 30 years of dates
        IDENTITY(INT,1,1) AS N
   INTO #tally
   FROM Master.dbo.SysColumns sc1,
        Master.dbo.SysColumns sc2

--PRINT @count
INSERT INTO  _etblInvJrBatches 
(
    cInvJrNumber            --  IJ0001      Plus 1
,   cInvJrDescription       --  Inventory Journal Batch
,   cInvJrReference         --  IJR10001    Plus 1
,   iCreateAgentID          --  1
,   bClearAfterPost         --  1
,   bAllowDupRef            --  1
,   bAllowEditGLContra      --  0
,   iNewLineDateOpt         --  0
,   iNewLineRefOpt          --  0
,   cNewLineRefDef          --  ''
,   bNewLineRefInc          --  0
,   iNewLineDescOpt         --  0
,   cNewLineDescDef         --  ''
,   bNewLineDescInc         --  0
,   iNewLineProjectOpt      --  0
,   iNewLineProjectDefID    --  0
,   iNewLineWarehouseOpt    --  0
,   iNewLineWarehouseDefID  --  0
,   bJustCleared            --  0
,   iTransactionCode        --  31 (select TrCodeID where TrCode = 'ADJ')
)


select N,    'IJ000'             --  Plus 1
,   'Inventory Journal Batch'
,   'IJR1000'               --  Plus 1
,   1
,   1
,   1
,   0
,   0
,   0
,   ''
,   0
,   0
,   ''
,   0
,   0
,   0
,   0
,   0
,   0 from #tally where n >0 and n <= @count

结果

enter image description here

答案 2 :(得分:0)

谢谢大家的贡献!

特别感谢@Thomas,因为他的建议在我的环境中效果最佳。

我对他的脚本做了一些改动,以适应参考编号:

declare @count int

 set @count = (select
    max(count) Max
from
    (select 
         count(iStockID) Count
     from   
         _etblInvJrBatchLines
     group by 
         iStockID) X) --Hardcoded 10
IF OBJECT_ID('tempdb..#tally') IS NOT NULL
  /*Then it exists*/
  DROP TABLE #tally
SELECT TOP (@count) --equates to more than 30 years of dates
        IDENTITY(INT,1,1) AS N
   INTO #tally
   FROM Master.dbo.SysColumns sc1,
        Master.dbo.SysColumns sc2

--PRINT @count
INSERT INTO  _etblInvJrBatches 
(
    cInvJrNumber            --  IJ0001
,   cInvJrDescription       --  Inventory Journal Batch
,   cInvJrReference         --  IJR10001
,   iCreateAgentID          --  1
,   bClearAfterPost         --  1
,   bAllowDupRef            --  1
,   bAllowEditGLContra      --  0
,   iNewLineDateOpt         --  0
,   iNewLineRefOpt          --  0
,   cNewLineRefDef          --  ''
,   bNewLineRefInc          --  0
,   iNewLineDescOpt         --  0
,   cNewLineDescDef         --  ''
,   bNewLineDescInc         --  0
,   iNewLineProjectOpt      --  0
,   iNewLineProjectDefID    --  0
,   iNewLineWarehouseOpt    --  0
,   iNewLineWarehouseDefID  --  0
,   bJustCleared            --  0
,   iTransactionCode        --  31 (select idTrCodes from TrCodes where Code = 'ADJ')
)

select
    'IJ000' + cast(N as varchar)
,   'Inventory Journal Batch'
,   'IJR1000' + cast(N as varchar)
,   1
,   1
,   1
,   0
,   0
,   0
,   ''
,   0
,   0
,   ''
,   0
,   0
,   0
,   0
,   0
,   0
,   (select idTrCodes from TrCodes where Code = 'ADJ')
from #tally where n >0 and n <= @count

这很有魅力!

查看结果:Results