SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


using IN statment & a string variable.


using IN statment & a string variable.

Author
Message
Tava
Tava
Right there with Babe
Right there with Babe (747 reputation)Right there with Babe (747 reputation)Right there with Babe (747 reputation)Right there with Babe (747 reputation)Right there with Babe (747 reputation)Right there with Babe (747 reputation)Right there with Babe (747 reputation)Right there with Babe (747 reputation)

Group: General Forum Members
Points: 747 Visits: 806
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?
dwain.c
dwain.c
SSCoach
SSCoach (17K reputation)SSCoach (17K reputation)SSCoach (17K reputation)SSCoach (17K reputation)SSCoach (17K reputation)SSCoach (17K reputation)SSCoach (17K reputation)SSCoach (17K reputation)

Group: General Forum Members
Points: 17451 Visits: 6431
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
Tava
Tava
Right there with Babe
Right there with Babe (747 reputation)Right there with Babe (747 reputation)Right there with Babe (747 reputation)Right there with Babe (747 reputation)Right there with Babe (747 reputation)Right there with Babe (747 reputation)Right there with Babe (747 reputation)Right there with Babe (747 reputation)

Group: General Forum Members
Points: 747 Visits: 806
double post... removed.
Tava
Tava
Right there with Babe
Right there with Babe (747 reputation)Right there with Babe (747 reputation)Right there with Babe (747 reputation)Right there with Babe (747 reputation)Right there with Babe (747 reputation)Right there with Babe (747 reputation)Right there with Babe (747 reputation)Right there with Babe (747 reputation)

Group: General Forum Members
Points: 747 Visits: 806
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.
dwain.c
dwain.c
SSCoach
SSCoach (17K reputation)SSCoach (17K reputation)SSCoach (17K reputation)SSCoach (17K reputation)SSCoach (17K reputation)SSCoach (17K reputation)SSCoach (17K reputation)SSCoach (17K reputation)

Group: General Forum Members
Points: 17451 Visits: 6431
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
Tava
Tava
Right there with Babe
Right there with Babe (747 reputation)Right there with Babe (747 reputation)Right there with Babe (747 reputation)Right there with Babe (747 reputation)Right there with Babe (747 reputation)Right there with Babe (747 reputation)Right there with Babe (747 reputation)Right there with Babe (747 reputation)

Group: General Forum Members
Points: 747 Visits: 806
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,',')
dwain.c
dwain.c
SSCoach
SSCoach (17K reputation)SSCoach (17K reputation)SSCoach (17K reputation)SSCoach (17K reputation)SSCoach (17K reputation)SSCoach (17K reputation)SSCoach (17K reputation)SSCoach (17K reputation)

Group: General Forum Members
Points: 17451 Visits: 6431
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
Tava
Tava
Right there with Babe
Right there with Babe (747 reputation)Right there with Babe (747 reputation)Right there with Babe (747 reputation)Right there with Babe (747 reputation)Right there with Babe (747 reputation)Right there with Babe (747 reputation)Right there with Babe (747 reputation)Right there with Babe (747 reputation)

Group: General Forum Members
Points: 747 Visits: 806
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.
dwain.c
dwain.c
SSCoach
SSCoach (17K reputation)SSCoach (17K reputation)SSCoach (17K reputation)SSCoach (17K reputation)SSCoach (17K reputation)SSCoach (17K reputation)SSCoach (17K reputation)SSCoach (17K reputation)

Group: General Forum Members
Points: 17451 Visits: 6431
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
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search