将增量值插入表

时间:2019-09-10 17:48:46

标签: tsql sql-insert

我正在尝试将值插入表中。一些字段已声明公式。插入后数据正确增加。 例如,字段“ SAPPRDONo”应从13366开始每行增加1。 这是我的脚本:

      Declare @count int = 1
      Declare @Qty int
      Declare @scrap int
      Declare @lot nvarchar(100)
      Declare @lotno nvarchar(10)
      Declare @insertlot nvarchar(100)
      Declare @yield int = 1
      Declare @prdo int = 1

      While @count <= 100
          Begin
              Set @Qty = round(430 * (@yield/100),0,0)
              Set @insertlot = @lot + @lotno
              Set @scrap = 430 - @qty
          Insert into Report_comp_EWS (SpinwebABI, SAPPRDONo, 
    CompletionType, Quantity, ParentLotNo, NewLotNo, WhseFinish, 
    ItemCodeStart, ItemCodeFinish, ScrapQty) 
    values ('Stress Test', '13366', 'Complete', 4, 'StressTestAuto', 
    'StressTest1', 'E_CHD', 'WA00N44A', 'Lynx', 426)
                Set @count = @count + 1
                Set @yield = @yield + 1
                Set @prdo = 13366 +1
                Set @lotno = @lotno + 1
            End

当我插入这些值时,字段SAPPRDONo从13366开始,但没有增加。对于第一行,该值应等于“ 13366”,下一行应为“ 13367”,下一行为“ 13367”,依此类推。

任何帮助将不胜感激。

我已经测试过设置声明prdo int = 13366,然后设置@prdo = 13366 +1

我希望输出13366、13367、13368,但每一行都返回13366

1 个答案:

答案 0 :(得分:0)

您要在插入查询中为所有行插入“ 13366”。 您必须增加它。 请尝试以下:

  Declare @count int = 1
  Declare @Qty int
  Declare @scrap int
  Declare @lot nvarchar(100)
  Declare @lotno nvarchar(10)
  Declare @insertlot nvarchar(100)
  Declare @yield int = 1
  Declare @prdo int = 13366

  While @count <= 100
      Begin
          Set @Qty = round(430 * @yield)/100,0,0)
          Set @insertlot = @lot + @lotno
          Set @scrap = 430 - @qty
      Insert into Report_comp_EWS (SpinwebABI, SAPPRDONo, 
CompletionType, Quantity, ParentLotNo, NewLotNo, WhseFinish, 
ItemCodeStart, ItemCodeFinish, ScrapQty) 
values ('Stress Test', CAST(@prdo as varchar(5)), 'Complete', 4, 'StressTestAuto', 
'StressTest1', 'E_CHD', 'WA00N44A', 'Lynx', 426)
            Set @count = @count + 1
            Set @yield = @yield + 1
            Set @prdo = @prdo +1 -- increment by 1
            Set @lotno = @lotno + 1
        End

更新的公式-数量返回0

     Declare @count int = 1
        Declare @Qty int 
        Declare @scrap int
        Declare @lot nvarchar(100) = 'StressTest'
        Declare @lotno nvarchar(10) = 1
        Declare @insertlot nvarchar(100)
        Declare @yield int = 1
        Declare @prdo int = 13366

        While @count <= 100
            Begin
        Set @Qty = round(430 * @yield)/100,0,0)
        Set @insertlot = @lot + @lotno
        Set @scrap = 430 - @qty
    Insert into Report_comp_EWS (SpinwebABI, SAPPRDONo, CompletionType, 
    Quantity, ParentLotNo, NewLotNo, WhseFinish, ItemCodeStart, ItemCodeFinish, 
    ScrapQty) values ('Stress Test' , CAST(@prdo as varchar(5)), 'Complete', CAST(@qty as 
    varchar(5)), 'StressTestAuto', Cast(@insertlot as nvarchar(100)), 'E_CHD', 
    'WA00N44A', 'Lynx', 426)
                Set @count = @count + 1
                Set @yield = @yield + 1
                Set @prdo = @prdo +1
                Set @lotno = @lotno + 1
                       End