WHILE循环中的递增/递减变量名(SQL Server)

j2qf4p5b  于 2022-12-26  发布在  SQL Server
关注(0)|答案(1)|浏览(185)

我试图减少WHILE循环中的变量名编号,因此它以@chk3开始,然后是@chk2,依此类推。
这是我目前拥有的代码,但是我在@chk_current上创建的“变量”被视为文本。
如何创建一个实际的变量,然后使用固定文本@chk和@task_total number来减少变量?因此,在第一个循环中,变量名将为@chk3(值= NO),第二个为@chk2(值= YES),然后为@chk1(值= YES),然后代码将退出循环。

DECLARE ALL...

SET @chk1 = 'YES'
SET @chk2 = 'YES'
SET @chk3 = 'NO'

SELECT @task_total = COUNT(*) 
FROM R5TASKCHECKLISTS 
WHERE TCH_TASK = @tasknum

WHILE @task_total >= 0
BEGIN
    SET @chk_current = CONCAT('@chk', @task_total)

    INSERT INTO R5TRACKINGDATA
        (TKD_TRANS, TKD_TRACKDATE,
         TKD_PROMPTDATA1, TKD_PROMPTDATA2, TKD_PROMPTDATA3,
         TKD_PROMPTDATA4, TKD_PROMPTDATA5, TKD_PROMPTDATA6,
         TKD_PROMPTDATA7, TKD_PROMPTDATA8, TKD_PROMPTDATA9,
         TKD_PROMPTDATA10, TKD_PROMPTDATA11, TKD_PROMPTDATA12,
         TKD_PROMPTDATA13, TKD_PROMPTDATA14, TKD_PROMPTDATA15) 
    VALUES ('AO01',   --TKD_TRANS
            GETDATE(),
            @next_evtcode,    -- EVT_CODE
            @ot_desc,         -- EVT_DESC
            @ot_dates,        -- EVT_DATE (dd/mm/yyyy)
            '100',            -- EVT_MRC
            '282',            -- EVT_ORG
            'PMM',            -- EVT_JOBTYPE
            @ot_dates,        -- EVT_REPORTED (dd/mm/yyyy)
            @ot_object,       -- EVT_OBJECT
            '282',            -- EVT_OBJECT_ORG
            @ot_dates,        -- EVT_TARGET (dd/mm/yyyy)
            @ot_dates,        -- EVT_COMPLETED (dd/mm/yyyy)
            @ot_createdby,    -- EVT_ENTEREDBY
            @tasklist,        -- TSK_CODE
            @task_total,      -- ACK_SEQUENCE
            @chk_current      -- ACK_NO_YES (NO or YES)
        )

    SET @task_total = @task_total - 1;
END
xfb7svmp

xfb7svmp1#

将代码简化为MCVE

DECLARE @chk1 varchar(5) = 'YES'
      , @chk2 varchar(5) = 'YES'
      , @chk3 varchar(5) = 'NO'
      , @task_total int = 3

--SELECT @task_total = COUNT(*) FROM R5TASKCHECKLISTS WHERE TCH_TASK = '123' --VALUE IS 3

WHILE @task_total >= 0
BEGIN

        SET @chk_current = CONCAT('@chk', @task_total)

        INSERT INTO R5TRACKINGDATA
        (
            TKD_PROMPTDATA14,
            TKD_PROMPTDATA15
            ) VALUES (
             @task_total --ACK_SEQUENCE
            ,@chk_current --ACK_NO_YES (NO or YES)
        )

        SET @task_total = @task_total - 1;

END

SQL不使用数组。它使用表。因此,要在循环中执行此操作:

DECLARE @chk table (id int identity(1,1), val varchar(5))
insert @chk (val)
      select 'YES'
union select 'YES'
union select 'NO'
union select 'NO'
union select 'YES'
union select 'NO'

declare @chk_current varchar(5)
declare @task_total int = 3
--SELECT @task_total = COUNT(*) FROM R5TASKCHECKLISTS WHERE TCH_TASK = '123' --VALUE IS 3

WHILE @task_total >= 0
BEGIN

        SELECT @chk_current = c.val
        FROM @chk c
        WHERE c.id = @task_total

        INSERT INTO R5TRACKINGDATA
        (
            TKD_PROMPTDATA14,
            TKD_PROMPTDATA15
            ) VALUES (
             @task_total --ACK_SEQUENCE
            ,@chk_current --ACK_NO_YES (NO or YES)
        )

        SET @task_total = @task_total - 1;
END

如果变量(@chk*)的数量未知,我假设你是从数据中获取的,这里模拟了这个过程,使用了一条INSERT语句。

with 
chk (id, val) as (
        select 1, 'YES'
  union select 2, 'YES'
  union select 3, 'NO'
  union select 4, 'NO'
  union select 5, 'YES'
  union select 6, 'NO'
)

insert into R5TRACKINGDATA (TKD_PROMPTDATA14, TKD_PROMPTDATA15)
select c.id, c.val
from chk c
where c.id <= (SELECT COUNT(*) FROM R5TASKCHECKLISTS WHERE TCH_TASK = '123')

因此,不考虑可能的SQL注入,您的php代码可能如下所示:

$sql = "with 
chk (id, val) as (
        select 1, " . $chk1 . "
  union select 2, " . $chk2 . "
  union select 3, " . $chk3 . "
  union select 4, " . $chk4 . "
  union select 5, " . $chk5 . "
  union select 6, " . $chk6 . "
  union select 7, " . $chk7 . "
  union select 8, " . $chk8 . "
  union select 9, " . $chk9 . "
  union select 10, " . $chk10 . "
)

insert into R5TRACKINGDATA (TKD_PROMPTDATA14, TKD_PROMPTDATA15)
select c.id, c.val
from chk c
where c.id <= (SELECT COUNT(*) FROM R5TASKCHECKLISTS WHERE TCH_TASK = '123')";

相关问题