I have a table, let's call it Foo, there is a field called Bar in the table, also, there is a field called LoremIpsum.
Bar is an nvarchar but I'm only interested in Bar values which are convertable to an integer number.
LoremIpsum is also a number.
I want to get the first n (value) numbers beginning with k from my Foo table where LoremIpsum has a specific value (LoremIpsum1) and (value) not in select Bar from Foo where LoremIpsum = LoremIpsum1
My input is: (LoremIpsum1, n, k)
where LoremIpsum1 is a particular value for LoremIpsum, n is the number of numbers to get and k is the offset.
My output should be a set of numbers which don't exist in the table as Bar values if LoremIpsum = LoremIpsum1. My current command:
WITH q AS
(
SELECT convert(bigint, 50000) AS num
UNION ALL
SELECT convert(bigint, num + 1)
FROM q
)
SELECT top 200 *
FROM q
where convert(bigint, num) not in (select convert(bigint, Bar) from Foo where LoremIpsum = 68 and isnumeric(Bar + 'e0') = 1)
option (maxrecursion 365);
This query doesn't always work, at a computer the problem was that an nvarchar couldn't be converted to a bigint, probably it was a problem with the settings, the other error occurs if the needed recursion is deeper than the given maxrecursion.
In this case n = 200, k = 50000, LoremIpsum1 = 68
The main problem is that this query is recursive and I would like to modify it to be an iterative query.
Any help is greatly appreciated.
Best regards, Lajos Arpad.