【问题标题】:TSQL Inserting values based on integer results from select statementTSQL根据select语句的整数结果插入值
【发布时间】:2018-07-05 13:46:16
【问题描述】:

我正在运行以下查询:

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

如你所知,this 的结果是一个整数;在这种情况下,结果是 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次。

感谢您的协助。

阿蒂。

【问题讨论】:

  • 字面意思是同一行,24 次?我只是想确保你想要相同的值,24 次。或者,“加 1”注释的意思是从子查询中将 1 加到最大值。此外,您标记了 3 个不同版本的 sql server。我都删除了,请只添加重要的。
  • 谢谢@scsimon!是的,只是相同的值 24 次。 Plus 1 仅供我自己参考 - (稍后在该字段的末尾添加一个计数器)。看看我对托马斯的回应,他的建议很有帮助!再次感谢!
  • 旁白:如果这是您从 SSMS 做的一次性事情,您可以使用 GO 命令上的好奇 count 参数多次重复该语句(批处理)。

标签: sql sql-server


【解决方案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

结果

【讨论】:

  • 非常感谢@Thomas!这个就像一个魅力,我只是改变了一点,让它在我的环境中工作。谢谢!
  • @Birel 很高兴我能提供帮助 - 请记住将其标记为正确答案,以便其他人可以从中受益。祝你有美好的一天。
【解决方案2】:

有很多方法可以做到这一点。 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!

如果是我,我会在客户端使用代码而不是在数据库中执行所有这些操作。我是老派,我不认为像我的第一个示例那样在数据库中构建这样的解决方案时可以很好地扩展。

【讨论】:

  • 这会很糟糕。这是一个 RBAR 操作,而不是 SQL 的开发方式。只是提醒一下。
【解决方案3】:

感谢大家的贡献!

特别感谢@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

【讨论】:

    猜你喜欢
    • 2018-10-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多