×
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!
  • Students Click Here

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

Jobs

Get "available" numbers

Get "available" numbers

Get "available" numbers

(OP)
I have a table that contains a list of products. One of the columns contains a product number (it is not an identity column). I would like to create a stored proc that will give me all available product numbers inside a range. So for example, my table may have 10 records and the barcode numbers are 101, 102, 104, 105, 107, 108, 109, 111, 112, 113.... I would like to call a stored proc with the parameters StartProductNumber of 101 and EndProductNumber 113. The results would give me 103, 106, and 110 since they do not exist. What's the easiest way to accomplish this?

RE: Get "available" numbers

A simple query does this:

CODE

SELECT * FROM products where barcode between 101 and 113 

or parameterized:

CODE

lnProductBarcodeLow = 101
lnProductBarcodeHigh = 113
SELECT * FROM products where barcode between ?m.lnProductBarcodeLow and ?m.lnProductBarcodeHigh 

Bye, Olaf.

RE: Get "available" numbers

(OP)
the query needs to give me the numbers that are missing from the sequence

RE: Get "available" numbers

I expect you could use something like this where you create a table of all values and left join your product number:

CODE --> SQL

WITH
cteNums As
(SELECT ones.n + 10*tens.n + 1000 Num
FROM (VALUES(0),(1),(2),(3),(4),(5),(6),(7),(8),(9)) ones(n),
     (VALUES(0),(1),(2),(3),(4),(5)                ) tens(n)
WHERE ones.n + 10*tens.n + 1000 BETWEEN 101 AND 113)
SELECT Num 
FROM cteNums LEFT JOIN [YourTableName] ON Num = SomeField
WHERE [somefield] is Null 

Duane
Minnesota
Hook'D on Access
MS Access MVP 2001-2016

RE: Get "available" numbers

Ok, my bad not reading thoroughly.

Then a tally table stored proc should generate the full list 101...113 and you could do an EXCEPT query with the products.

CODE -->

Declare @products as Table (barcode int);


--barcodes 101..113 with several gaps
Insert into @products values (102),(104),(105),(106),(109),(110),(113);

WITH Tally (num) AS
(
    -- 1000 rows
    SELECT ROW_NUMBER() OVER (ORDER BY (SELECT NULL))
    FROM (VALUES(0),(0),(0),(0),(0),(0),(0),(0),(0),(0)) a(n)
    CROSS JOIN (VALUES(0),(0),(0),(0),(0),(0),(0),(0),(0),(0)) b(n)
    CROSS JOIN (VALUES(0),(0),(0),(0),(0),(0),(0),(0),(0),(0)) c(n)
)

Select num from Tally where num between 101 and 113
Except 
Select barcode from @products where barcode between 101 and 113 

If you need more than 1000 tally numbers see http://www.sqlservercentral.com/blogs/dwainsql/201...

If you know you never are interested in more than 1000 consecutive numbers, you don't need any advanced tally table procedure, but the low value might be much higher (as in 12 digit barcodes). For that you can also use lowvalue+ROWNUMBER()-1, of course and num doesn't start at 1.

Bye, Olaf.

RE: Get "available" numbers

(Untested) but what about....

CODE

SELECT DISTINCT n=number FROM master..[spt_values] WHERE Number BETWEEN 101 and 113
EXCEPT
Select barcode from @products WHERE barcode between 101 and 113 

Just my $.02

"What the captain doesn't realize is that we've secretly replaced his Dilithium Crystals with new Folger's Crystals."

--Greg

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members!

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close