我想在sql中使用for循环迭代一些varchar值,但似乎不可能。无论我在哪里寻找示例,似乎for循环只能迭代整数值而不是sql中的varchar值。 这是真的吗? 以下是我的sql代码
declare
@exist number
my_array=('100161','100164','102226','100143','108942','106922','108949','107191','100098','107182','107193','98646','100102','100105','103044','103293','103296','103297','104178','103018','104145','103017','103019','108991','108995','109000','103020','102121','103021','106284','103951','100117','102872','102873','100125','101582','102234','103027','103028','102225','101574','106964','106969','108956','109719','101581','102346','106997','107028','107030','107031','107070','102347','107083','107084','107085','107086','103633','107124','100191','100172','100204','104148','104163','100190','107180','109849','109852','110047','107473','107502','100091','100096','106265','108346','108222','109382','107814','107823','108167','109359','100171','103300','108268','108300','108860','108982','102342','102344','100089','108675','108880','109341','109875','109877','109884','108854','101912','102829','103317','104323','104324','104389','107239','108271','108273','108275','108277','108279','108872','108885','108957','108983','109878','109148','109279','109399','109443','109922','103318','109448','109452')
for i in my_array
@exist=select id from BacktestingCriteria where nodeId=i and hierarchyViewId=16008 and varCriteriaId=16 and pnlCriteriaId=3;
if @exist is null
insert into BacktestingCriteria(nodeId,hierarchyViewId,varCriteriaId,pnlCriteriaId) values
(i,16008,16,3)
@exist=select id from BacktestingCriteria where nodeId=i and hierarchyViewId=16008 and varCriteriaId=16 and pnlCriteriaId=4;
if @exist is null
insert into BacktestingCriteria(nodeId,hierarchyViewId,varCriteriaId,pnlCriteriaId) values
(i,16008,16,4)
end loop;
答案 0 :(得分:0)
您似乎在VARCHAR列中使用数字,因此您可以在整数中投放值,其中使用循环:
@exist=select id from BacktestingCriteria where nodeId= CAST(i as INT) and hierarchyViewId=16008 and varCriteriaId=16 and pnlCriteriaId=3;
答案 1 :(得分:0)
在SQL Server中,没有FOR LOOP。但是,您使用WHILE LOOP模拟FOR LOOP。通常在整数递增之前完成工作。 SQL中的许多for循环实际上在它们的工作中使用了这个整数(从一行到另一行迭代或导致临时表的结果),并且如果增量发生在循环的开头而不是结束时可能会抛出。
没有for循环,只有while循环:
DECLARE @x int = 0
WHILE @x < 10
BEGIN
SET @x = @x + 1
/* work to be done */
END