nodes
while ループを削除し、XML をシュレッドするために一度に挿入を行うことができます。
insert into mytable([Word])
select N.value('@Entry', 'nvarchar(max)')
from OtherTable
cross apply XmlColumn.nodes('word') as T(N)
where ID = 2
@j
挿入する行数を制限する必要がある場合はmytable
、代わりにこれを使用できます。
insert into mytable([Word])
select ID
from
(
select N.value('@Entry', 'nvarchar(max)') as ID,
row_number() over(order by T.N) as rn
from OtherTable
cross apply XmlColumn.nodes('word') as T(N)
where ID = 2
) T
where rn <= @j
何らかの理由で本当にループを使用したい場合は、代わりにこのようにすることができます。
while @Count <= @j
begin
insert into mytable([Word])
select XMLColumn.value('(/word[sql:variable("@Count")]/@Entry)[1]', 'nvarchar(max)')
from OtherTable
where ID = 2