-- I have a situation where doing
-- first example
-- 1. Get series of values througha query into a string (@val)like
'1,2,3,4':
declare @val varchar(4000)
select @Val = @val + cast(myval as varchar) + ',' -- myval is an
integer variable
from xyz
where xyz.field = 33
SET @val = left(@val, len(@val) - 1)
-- 2. EXEC a query using IN (' + @val + ')'
EXEC('
select *
from qpr
where qpr.fieldx IN (' + @val + ')
')
-- is much faster than doing
-- second example
select *
from qpr
where qpr.fieldx IN (select myval
from xyz
where xyz.field = 33)
-- Since second example does not have a correlateed query, why is it
slower?
-- Thanks in advance,
-- Caveman
-- first example
-- 1. Get series of values througha query into a string (@val)like
'1,2,3,4':
declare @val varchar(4000)
select @Val = @val + cast(myval as varchar) + ',' -- myval is an
integer variable
from xyz
where xyz.field = 33
SET @val = left(@val, len(@val) - 1)
-- 2. EXEC a query using IN (' + @val + ')'
EXEC('
select *
from qpr
where qpr.fieldx IN (' + @val + ')
')
-- is much faster than doing
-- second example
select *
from qpr
where qpr.fieldx IN (select myval
from xyz
where xyz.field = 33)
-- Since second example does not have a correlateed query, why is it
slower?
-- Thanks in advance,
-- Caveman
Comment