Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase 12»»

using IN statment & a string variable. Expand / Collapse
Author
Message
Posted Sunday, September 2, 2012 7:37 PM
SSC Journeyman

SSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC Journeyman

Group: General Forum Members
Last Login: Sunday, April 27, 2014 8:38 PM
Points: 90, Visits: 420
Hi,

I need some assistance and i've read multiple answers which are very similar but just not sure how to tie it in with my code as what I've done has failed. I have a newly created function & below is a snippet of the code it's not the whole lot but crucial parts to understand and provide advice.

----------------------------------------

ALTER FUNCTION [dbo].[fDayCount]
(
@WorkDate DATETIME
)

RETURNS
@returntable TABLE
(
WorkDate DATETIME,
CompanyID VARCHAR(255)
)

AS
BEGIN

DECLARE @CompanyID varchar(50)

SET @CompanyID =
(
SELECT
CompID
FROM
dbo.config
WHERE
configva = 'compidstring'
)

INSERT @returntable
(
WorkDate
CompanyID
)

SELECT
workdate,
@companyID
FROM
dbo.table1
INNER JOIN
dbo.table2
ON
table1.a = table2.a
WHERE
date = @date
AND
companyid IN(@CompanyID)

RETURN
END

----------------------------------------------------

@CompanyID is a string of all companies that are relevant e.g. '1','33',97' ... i could hardcode this inside the IN statement and it works but this is changed from time to time which means the statement will need to be updated every so often, while the variable will be updated from SSIS feeds.

I've read doing it as SET @SQL = ' statement' + @varible +' )' but just not working for me.

Any idea on how to get around this?


Post #1353303
Posted Sunday, September 2, 2012 7:48 PM


Hall of Fame

Hall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of Fame

Group: General Forum Members
Last Login: Thursday, August 27, 2015 5:48 PM
Points: 3,971, Visits: 6,355
Try changing:

AND
companyid IN(@CompanyID)


To:

AND
companyid IN(SELECT item FROM dbo.DelimitedSplit8K(@CompanyID, ','))


Where DelimitedSplit8K can be found here:
http://www.sqlservercentral.com/articles/Tally+Table/72993/



My mantra: No loops! No CURSORs! No RBAR! Hoo-uh!

My thought question: Have you ever been told that your query runs too fast?

My advice:
INDEXing a poor-performing query is like putting sugar on cat food. Yeah, it probably tastes better but are you sure you want to eat it?
The path of least resistance can be a slippery slope. Take care that fixing your fixes of fixes doesn't snowball and end up costing you more than fixing the root cause would have in the first place.


Need to UNPIVOT? Why not CROSS APPLY VALUES instead?
Since random numbers are too important to be left to chance, let's generate some!
Learn to understand recursive CTEs by example.
Splitting strings based on patterns can be fast!
My temporal SQL musings: Calendar Tables, an Easter SQL, Time Slots and Self-maintaining, Contiguous Effective Dates in Temporal Tables
Post #1353305
Posted Sunday, September 2, 2012 10:32 PM
SSC Journeyman

SSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC Journeyman

Group: General Forum Members
Last Login: Sunday, April 27, 2014 8:38 PM
Points: 90, Visits: 420
double post... removed.
Post #1353329
Posted Sunday, September 2, 2012 10:32 PM
SSC Journeyman

SSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC Journeyman

Group: General Forum Members
Last Login: Sunday, April 27, 2014 8:38 PM
Points: 90, Visits: 420
When using that example it produced an error

IN
(
SELECT ConfiguredValue
FROM
dbo.table1(@CompanyID, ',')
)


Parameters supplied for object 'dbo.table1' which is not a function. If the parameters are intended as a table hint, a WITH keyword is required.
Post #1353330
Posted Sunday, September 2, 2012 10:59 PM


Hall of Fame

Hall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of Fame

Group: General Forum Members
Last Login: Thursday, August 27, 2015 5:48 PM
Points: 3,971, Visits: 6,355
You need to call DelimitedSplit8K (not dbo.Table1) as I did. Find it in the link I provided.


My mantra: No loops! No CURSORs! No RBAR! Hoo-uh!

My thought question: Have you ever been told that your query runs too fast?

My advice:
INDEXing a poor-performing query is like putting sugar on cat food. Yeah, it probably tastes better but are you sure you want to eat it?
The path of least resistance can be a slippery slope. Take care that fixing your fixes of fixes doesn't snowball and end up costing you more than fixing the root cause would have in the first place.


Need to UNPIVOT? Why not CROSS APPLY VALUES instead?
Since random numbers are too important to be left to chance, let's generate some!
Learn to understand recursive CTEs by example.
Splitting strings based on patterns can be fast!
My temporal SQL musings: Calendar Tables, an Easter SQL, Time Slots and Self-maintaining, Contiguous Effective Dates in Temporal Tables
Post #1353334
Posted Monday, September 3, 2012 12:05 AM
SSC Journeyman

SSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC Journeyman

Group: General Forum Members
Last Login: Sunday, April 27, 2014 8:38 PM
Points: 90, Visits: 420
sorry just a little confused. So i have to create the function (as per the script) & once created call it in the way you did to my reply.

1. CREATE FUNCTION [dbo].[DelimitedSplit8K]
2. Perform the IN ( select 'column' from dbo.delimitedSplit,',')

Post #1353350
Posted Monday, September 3, 2012 12:23 AM


Hall of Fame

Hall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of Fame

Group: General Forum Members
Last Login: Thursday, August 27, 2015 5:48 PM
Points: 3,971, Visits: 6,355
Tava (9/3/2012)
sorry just a little confused. So i have to create the function (as per the script) & once created call it in the way you did to my reply.

1. CREATE FUNCTION [dbo].[DelimitedSplit8K]

Yes

Tava (9/3/2012)
2. Perform the IN ( select 'column' from dbo.delimitedSplit,',')


No. You must:

SELECT item FROM dbo.DelimitedSplit8K(@CompanyID, ',')


Item is the name of the column returned by DelimitedSplit8K that you need to look in.



My mantra: No loops! No CURSORs! No RBAR! Hoo-uh!

My thought question: Have you ever been told that your query runs too fast?

My advice:
INDEXing a poor-performing query is like putting sugar on cat food. Yeah, it probably tastes better but are you sure you want to eat it?
The path of least resistance can be a slippery slope. Take care that fixing your fixes of fixes doesn't snowball and end up costing you more than fixing the root cause would have in the first place.


Need to UNPIVOT? Why not CROSS APPLY VALUES instead?
Since random numbers are too important to be left to chance, let's generate some!
Learn to understand recursive CTEs by example.
Splitting strings based on patterns can be fast!
My temporal SQL musings: Calendar Tables, an Easter SQL, Time Slots and Self-maintaining, Contiguous Effective Dates in Temporal Tables
Post #1353358
Posted Monday, September 3, 2012 12:24 AM
SSC Journeyman

SSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC Journeyman

Group: General Forum Members
Last Login: Sunday, April 27, 2014 8:38 PM
Points: 90, Visits: 420
excellent, I got it working, created the Function & in the subquery now getting the results i wanted... really appreciate your help on this... no way i would have solved it.

IN
(
SELECT
Item
FROM
dbo.fDelimitedSplit8K(@CompanyID, ',')
)

Thanks again.
Post #1353359
Posted Monday, September 3, 2012 1:21 AM


Hall of Fame

Hall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of Fame

Group: General Forum Members
Last Login: Thursday, August 27, 2015 5:48 PM
Points: 3,971, Visits: 6,355
Happy to be of service. You're welcome.


My mantra: No loops! No CURSORs! No RBAR! Hoo-uh!

My thought question: Have you ever been told that your query runs too fast?

My advice:
INDEXing a poor-performing query is like putting sugar on cat food. Yeah, it probably tastes better but are you sure you want to eat it?
The path of least resistance can be a slippery slope. Take care that fixing your fixes of fixes doesn't snowball and end up costing you more than fixing the root cause would have in the first place.


Need to UNPIVOT? Why not CROSS APPLY VALUES instead?
Since random numbers are too important to be left to chance, let's generate some!
Learn to understand recursive CTEs by example.
Splitting strings based on patterns can be fast!
My temporal SQL musings: Calendar Tables, an Easter SQL, Time Slots and Self-maintaining, Contiguous Effective Dates in Temporal Tables
Post #1353365
Posted Tuesday, September 4, 2012 8:49 AM


SSChampion

SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

Group: General Forum Members
Last Login: 2 days ago @ 3:08 PM
Points: 14,542, Visits: 14,314
Tava (9/3/2012)
excellent, I got it working, created the Function & in the subquery now getting the results i wanted... really appreciate your help on this... no way i would have solved it.


But do you UNDERSTAND it? It is great that you have it working but unless you understand it you are only half way done. You are the person getting the phone call at 3am when your production system is down. If you don't understand it then you need to go back and read that article over and over until you do.


_______________________________________________________________

Need help? Help us help you.

Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.

Need to split a string? Try Jeff Moden's splitter.

Cross Tabs and Pivots, Part 1 – Converting Rows to Columns
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs
Understanding and Using APPLY (Part 1)
Understanding and Using APPLY (Part 2)
Post #1353955
« Prev Topic | Next Topic »

Add to briefcase 12»»

Permissions Expand / Collapse