SQL Scripting - how to program input

I have a script that I run on a SQL Server 2008 and it works fine. My problem or need is that the script is a query that does a condition on the select with a where  X in (a, b, c, d, e,). Specifically I want to search a table where the rows are a, b, c, d, e, etc. (a,b,c,d,e are policy numbers).  As I state the script works when I manually input the policy numbers and my problem is that I can copy/paste the policy numbers into the In ( ) but I am limited to 8000 characters which is the max amount of characters allowed. I have 1000's of policy numbers that are all 16 characters.
Any ideas that would allow me to input all my policy numbers at once?
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.

Bob BenderCommented:
If you have a list of policies that can be extracted from a table with a common theme, you can use a Nested SQL statement.   A SQL nested query is a SELECT query that is nested inside a SELECT, UPDATE, INSERT, or DELETE SQL query.

A simple example of SQL nested query

SELECT Model FROM Product
WHERE ManufacturerID
IN (SELECT ManufacturerID FROM Manufacturer WHERE Manufacturer = 'Dell')

The nested query (withinin the IN clause) will select all models from the Product table manufactured by Dell:

Inspiron B120
Inspiron B130
Inspiron E1705


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
SeTechAuthor Commented:
Bob -- that looks like it will work. Thanks Much
Starting SQL 2005 the varchar allows MAX size:

Declare @value_list varchar(MAX)='<values list here>'

The limit is 2GB.
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

Scott PletcherSenior DBACommented:
Load the policy numbers into a keyed temp table and join to it.

CREATE TABLE #policy_numbers (
    policy_number char(16) PRIMARY KEY
INSERT INTO #policy_numbers
SELECT ...list of policy#s...

FROM data_table dt
INNER JOIN #policy_numbers pn ON
    pn.policy_number = dt.policy_number
What is the volatility of the parameter? (do you change it often or infrequently or rarely)

Where do you copy/paste the policy numbers from?
Bob BenderCommented:
Thanks for the vote of confidence  :D
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.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.