So this seems like something that should be easy. But say I had an insert:
insert into TABLE VALUES ('OP','OP_DETAIL','OP_X')
and I wanted X to go from 1-100. (Knowing there are some of those numbers that already exist, so if the insert fails I want it to keep going)
how would I do such a thing?
Advertisement
Answer
Here’s a slightly faster way
-- techniques from Jeff Moden and Itzik Ben-Gan: ;WITH E00(N) AS (SELECT 1 UNION ALL SELECT 1), E02(N) AS (SELECT 1 FROM E00 a, E00 b), E04(N) AS (SELECT 1 FROM E02 a, E02 b), E08(N) AS (SELECT 1 FROM E04 a, E04 b), cteTally(N) AS (SELECT ROW_NUMBER() OVER (ORDER BY N) FROM E08) INSERT INTO yourTable SELECT 'OP','OP_DETAIL','OP_' + CAST(N AS varchar) FROM cteTally WHERE N <= 100