In my SP I am creating a temp table #PolicyNumbers
and populating it with Policy Numbers
based on the parameters that will be supplied to the SP.
CREATE TABLE #PolicyNumbers (PolicyNumber varchar(50))
INSERT INTO #PolicyNumbers SELECT PolicyNumber
FROM PlazaInsuranceWPDataSet
WHERE State IN (SELECT * FROM [dbo].[StringOfStringsToTable](@State,','))
AND Coverage IN (SELECT * FROM [dbo].[StringOfStringsToTable](@Coverage,','))
AND SICCode IN (SELECT * FROM [dbo].[StringOfStringsToTable](@SICCode,','))
Further below in my SP I'm using EXISTS
statement to filter only those
PolicyNumber
s that been selected based on parameters.
WHERE EXISTS (SELECT PolicyNumber FROM #PolicyNumbers pn WHERE pn.PolicyNumber = piwp.PolicyNumber)
So the result from #PolicyNumbers
DOES contain duplicates PolicyNumbers:
Then furthure in my SP I have SELECT
statement and the result also contains duplicate PolicyNumber
s:
SELECT
PolicyNumber,
Cast(PolicyEffectiveDate AS DATE) AS PolicyEffectiveDate,
Cast(PolicyExpirationDate AS DATE) AS PolicyExpirationDate,
WrittenPremium
FROM PlazaInsuranceWPDataSet
And now I am using EXIST
statement:
SELECT
PolicyNumber,
Cast(PolicyEffectiveDate AS DATE) AS PolicyEffectiveDate,
Cast(PolicyExpirationDate AS DATE) AS PolicyExpirationDate,
WrittenPremium
FROM PlazaInsuranceWPDataSet piwp
WHERE EXISTS (SELECT PolicyNumber FROM #PolicyNumbers pn WHERE pn.PolicyNumber = piwp.PolicyNumber)
Is everything correct so far?
If yes, then why do I have slightly different result using SELECT
statement and passing same parameters in WHERE
clause using IN (@MyParameter)?
SELECT
PolicyNumber,
Cast(PolicyEffectiveDate AS DATE) AS PolicyEffectiveDate,
Cast(PolicyExpirationDate AS DATE) AS PolicyExpirationDate,
WrittenPremium
FROM PlazaInsuranceWPDataSet piwp
WHERE State IN (@State)
AND SICCode IN (@SICCode)
AND Coverage IN (@Coverage)