TSQL Query that loops through table with Where Clause Variable

Hello Experts,

I want to write a TSQL query that will search my database for all instances of a serial number based on a variable in the WHERE clause.  I want the variable to be acquired from a value populated in a column from a table with thousands of rows.  The SELECT statement would need to loop through each row of the table holding the serial number value, search the database for each, and write the results into a temp table.  I am having difficulty with the logic for the loop.  Pseudo code (so far) looks like this:

DECLARE @serial as varchar

SET @serial = (Some logic acquired by searching a table with a column named SerialNum)

SELECT * FROM (some Joined tables)

INTO SomeTempTable      

WHERE somejoinedtable.SerialNum = @serial

Loop through this procedure until all SerialNumbers have been searched
ccgmikeTAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

SharathData EngineerCommented:
It would be better to explain this with some sample data and expected result.
0
Scott PletcherSenior DBACommented:
You should loop thru the part that generates the @serial numbers to search for, then do a single read to the db tables to get all the search numbers.

For example:

CREATE TABLE #serialNums (
    serialNum varchar(...) PRIMARY KEY
    )

INSERT INTO #serialNums ( serialNum )
SELECT (Some logic acquired by searching a table with a column named SerialNum)

SELECT *
INTO SomeTempTable
FROM (some Joined tables) sjt
INNER JOIN #serialNums s ON
    s.serialNum = somejoinedtable.SerialNum
0
ccgmikeTAuthor Commented:
I agree Scott, but I'm having difficulty with the logic syntax.  What I have been doing is running a query for all serial numbers for a given period and then pasting them into the where clause as shown below.  I want to design the logic so that a sub (or parent) query would replace the need for me to do this.


Select * from some tables

where

ass.serialnumber = '2D8HN44E39R' or
ass.serialnumber = '5N1BV28UXXX' or
ass.serialnumber = '1TKC024221B1' or
ass.serialnumber = '1G2NE52T5WC' or
ass.serialnumber = '1FTRW08L11K' or ..........  and this list continues for several thousand numbers
ass.serialnumber = '1HD1FB4189Y641721' or
ass.serialnumber = '1FTPW14574KC43694' or
ass.serialnumber = '4TARN01P7PZ134784' or
0
Scott PletcherSenior DBACommented:
I'd still suggest what I suggested before:

Insert all the serialnumbers into a keyed table, and do a join to that table to do the lookup on the main table.

Looking up one serial number at a time for several thousand serial numbers would naturally take a long time.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
ccgmikeTAuthor Commented:
Thank you Scott, I ran this approach past some of my peers and they all agree with you.

Regards,

MikeT
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft SQL Server

From novice to tech pro — start learning today.