declare a variable in sql query with multiple values

Here is the issue I need to pass jobID from the first query to the second and third.
and sometimes there are 1 ,2 ,3 or more jobIds and I need to pass them all or get the data for all - there is always only 1 defaultlocationindex. I tried this but that doesnt work
DECLARE @JOB INT = 0;
WHILE 
(

SELECT @JOB = JobID FROM   Job where JobInProgress = 'true'   OR JobInitiated= 'true' AND DefaultLocationIndex = 4 

)
BEGIN
SELECT @JOB as jobid
 SELECT j.JobID,jde.ToolNumber,'TN' as ToolType 
 FROM   Job j  
 LEFT JOIN JobDownholeEquipment jde ON j.JobID = jde.JobID  
 WHERE (j.JobInProgress= 'true'   OR j.JobInitiated= 'true')   
 AND jde.PickedUp = 0  AND j.DefaultLocationIndex = 4 
 union all
 SELECT j.JobID,jue.SurfaceSystemSN,'SS' as ToolType 
 FROM   Job j  LEFT JOIN JobUpholeEquipment jue ON j.JobID = jue.JobID  
 WHERE (j.JobInProgress= 'true'   OR j.JobInitiated= 'true') 
 AND j.JobID in (@JOB)  
 AND jue.PickedUpSS = 1 
union all 
 SELECT j.JobID,jue.Transducer,'PT' as ToolType 
 FROM   Job j  LEFT JOIN JobUpholeEquipment jue ON j.JobID = jue.JobID  
 WHERE (j.JobInProgress= 'true'   OR j.JobInitiated= 'true')   
 AND j.JobID in( @JOB)
 AND jue.PickedUpPT = 1 
 END

Open in new window

LVL 6
r3nderAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
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.

Jeff DarlingDeveloper AnalystCommented:
SELECT j.JobID
	,jde.ToolNumber
	,'TN' AS ToolType
FROM Job j
LEFT JOIN JobDownholeEquipment jde ON j.JobID = jde.JobID
WHERE (
		j.JobInProgress = 'true'
		OR j.JobInitiated = 'true'
		)
	AND jde.PickedUp = 0
	AND j.DefaultLocationIndex = 4
	and j.JobID in (SELECT JobID FROM   Job where JobInProgress = 'true'   OR JobInitiated= 'true' AND DefaultLocationIndex = 4)

UNION ALL

SELECT j.JobID
	,jue.SurfaceSystemSN
	,'SS' AS ToolType
FROM Job j
LEFT JOIN JobUpholeEquipment jue ON j.JobID = jue.JobID
WHERE (
		j.JobInProgress = 'true'
		OR j.JobInitiated = 'true'
		)
	AND jue.PickedUpSS = 1
    and j.JobID in (SELECT JobID FROM   Job where JobInProgress = 'true'   OR JobInitiated= 'true' AND DefaultLocationIndex = 4)

UNION ALL

SELECT j.JobID
	,jue.Transducer
	,'PT' AS ToolType
FROM Job j
LEFT JOIN JobUpholeEquipment jue ON j.JobID = jue.JobID
WHERE (
		j.JobInProgress = 'true'
		OR j.JobInitiated = 'true'
		)
    and j.JobID in (SELECT JobID FROM   Job where JobInProgress = 'true'   OR JobInitiated= 'true' AND DefaultLocationIndex = 4)
	AND jue.PickedUpPT = 1

Open in new window

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
Scott PletcherSenior DBACommented:
Instead of:

IN (@JOB)

just use the actual full subquery there, in both queries using IN (@JOB):
...
AND j.JobID in ( SELECT JobID FROM   Job where JobInProgress = 'true'   OR JobInitiated= 'true' AND DefaultLocationIndex = 4 )
...
r3nderAuthor Commented:
Thanks Jeff
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 2008

From novice to tech pro — start learning today.