別のテーブルに挿入された1つの行に別のクエリの行を挿入する必要があります。 1つの行だけが返された場合に動作します。ただし、行数が1より大きい場合は失敗します。私はforループを理解できません - - **セクションで示されています。sql forループには、SQLインサートの一部として返された値が含まれています
declare @cn as int, @i as int
set @cn = 569
declare @dM table(dM varchar(max))
declare @rowNum table (rowNum int)
set @i = 1
insert @rowNum
exec ('select count(*) from table1 where c = ' + @cn)
--select rowNum from @rowNum as NumberRows --return 2 rows
if (select rowNum from @rowNum as NumberRows) > 1
begin
insert @dM
exec ('select d.d + '' '' + o.o + '' '' + d.v as rtM from table1 where c = ' + @countNumber)
--returns 2 rows as rtM so there will be two inserted rows
--going now okay
--going later okay
--**
while (@i <= (select count(*) from @rowNum)) --didn't work
--for each row returned in rtM in need to include as part of the overall insert
insert into table2 (cn, rtM, idate)
select
@cn
,'Message is: ' + (select dM from @dM) + ' - the message.'
cz.idate + ' ' + qw.txt
from table3 as cz
inner join table4 as qw on cz.id = qw.id
where cz.cn = @cn
set @i = @i + 1
--**
end
else
begin
--there is only 1 row returned from rtM so there will be a single inserted row
insert @dM
exec ('select d.d + '' '' + o.o + '' '' + d.v as rtM from table where c = ' + @countNumber)
insert into table2 (cn, rtM, idate)
select
@cn
,'Message is: ' + (select dM from @dM) + ' - the message.'
cz.idate + ' ' + qw.txt
from table3 as cz
inner join table4 as qw on cz.id = qw.id
where cz.cn = @cn
end
何が間違っているのか分かりませんが、@rowNumをINTにして、table1のcount()と同じ値に設定してください。 – JamieD77
@rowNumはテーブルが保存されていますdMクエリで検出された行の数。 – wl2m
私は次のように変更しました:@ i = 0を設定するために@i = 0を設定して、コードを実行して、dM行が2であるところの次のコードを取得しました。 =、!=、<, <= , >、> =、またはサブクエリが式として使用されている場合は、これは許可されません – wl2m