Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2000 Forums
 Transact-SQL (2000)
 Select combined with UDF

Author  Topic 

bglodde
Starting Member

25 Posts

Posted - 2002-08-15 : 18:07:12

I've been driving myself mad with this one...
I must be missing some simple concept. Please help

A query using a User defined function (dbo.QuoteCSV):

DECLARE @CSVString varchar( 255 )
DECLARE @TMP varchar( 255 )

SELECT @TMP = ( SELECT tbl1.csvList FROM tbl1 WHERE tbl1.id = 5 )
SELECT @CSVString = ( SELECT dbo.QuoteCSV( @TMP ) )
SELECT
tbl2.tbl2_id AS ID,
tbl2.tbl2_name AS Name,
tbl2.tbl2_city_name AS City,
'SomeOtherValue' AS Type,
CONVERT( varchar(255), tbl2.tbl2_profile) AS Profile
FROM tbl2
WHERE tbl2.tbl2_id2 = 5
AND CONVERT( varchar( 255 ), tbl2.tbl2_id ) IN ( @CSVString )




My UDF simply takes and input 3, 5, 6 and returns '3','5','6' for comparison using the IN statement.

If you substitute the values '3','5','6' in the query rather than @CSVString, it matches up perfectly. But it will not work for me dynamically.

I am sure this subjects been beaten around here a time or two, I just wasn't able to find the exact solution by searching.


Edited by - bglodde on 08/15/2002 18:08:38

MichaelP
Jedi Yak

2489 Posts

Posted - 2002-08-15 : 18:39:00
Have you tried this:

DECLARE @CSVString varchar( 255 )
DECLARE @TMP varchar( 255 )

SELECT @TMP = ( SELECT tbl1.csvList FROM tbl1 WHERE tbl1.id = 5 )
SELECT @CSVString = ( SELECT dbo.QuoteCSV( @TMP ) )
SELECT
tbl2.tbl2_id AS ID,
tbl2.tbl2_name AS Name,
tbl2.tbl2_city_name AS City,
'SomeOtherValue' AS Type,
CONVERT( varchar(255), tbl2.tbl2_profile) AS Profile
FROM tbl2
WHERE tbl2.tbl2_id2 = 5
AND
CONVERT( varchar( 255 ), tbl2.tbl2_id ) IN ((SELECT dbo.QuoteCSV(@TMP)))



If this doesn't work, you may have to do dynamic sql. Do a search on the forums for dynamic sql.

Michael

<Yoda>Use the Search page you must. Find the answer you will.</Yoda>
Go to Top of Page

bglodde
Starting Member

25 Posts

Posted - 2002-08-15 : 18:41:46
Thanks for the quick response, Michael. Still nothing though. I've actually been through every variation you can imagine...the worst part is if you copy the value returned from my function into the query analyzer, it does exactly what it should.

Go to Top of Page

MichaelP
Jedi Yak

2489 Posts

Posted - 2002-08-15 : 18:55:54
I think I have a solution for you, but a few questions first:
Does this "row" contain the values stored as CSV?

 
SELECT tbl1.csvList FROM tbl1 WHERE tbl1.id = 5


If so, is there any way to get it NOT as a CSV value, but three records? If so you could do this:



SELECT
tbl2.tbl2_id AS ID,
tbl2.tbl2_name AS Name,
tbl2.tbl2_city_name AS City,
'SomeOtherValue' AS Type,
CONVERT( varchar(255), tbl2.tbl2_profile) AS Profile
FROM tbl2
WHERE tbl2.tbl2_id2 = 5
AND CONVERT( varchar( 255 ), tbl2.tbl2_id ) IN (CONVERT( varchar(255), SELECT tbl1.ID FROM tbl1 WHERE tbl1.id = 5))



Michael

<Yoda>Use the Search page you must. Find the answer you will.</Yoda>
Go to Top of Page

MichaelP
Jedi Yak

2489 Posts

Posted - 2002-08-15 : 19:12:16
Here's an example from northwind:


Select * from employees
WHERE employeeID IN (select employeeID from Orders where OrderDate BETWEEN '7/4/1996' AND '7/10/1996')


Michael

<Yoda>Use the Search page you must. Find the answer you will.</Yoda>
Go to Top of Page

bglodde
Starting Member

25 Posts

Posted - 2002-08-15 : 19:23:36

Yes, the row is stored as a comma delimited list of items. I then show or hide based on their existence. The length is variable.

I just can't understand why this doesn't work!?! Especially if the values are printed, copied and replaced where the variable is, then it works

Also, this query is part of a huge union that's part of a pagination routine...it would be tremendous if there was some way to make it work as it is!! Isn't a varchar a varchar? Any reason it would be interpreted differently coming from a UDF or variable?


Go to Top of Page

MichaelP
Jedi Yak

2489 Posts

Posted - 2002-08-15 : 19:38:30
I think given that your data is stored as CSV, you'll need to use dynamic SQL.

Try this (You might need to fix soem quotes etc to make this work)


DECLARE @CSVString varchar( 255 )
DECLARE @TMP varchar( 255 )
DECLARE @SQL VARCHAR (4000)

SELECT @TMP = ( SELECT tbl1.csvList FROM tbl1 WHERE tbl1.id = 5 )
SELECT @CSVString = ( SELECT dbo.QuoteCSV( @TMP ) )

SELECT @SQL = '
SELECT
tbl2.tbl2_id AS ID,
tbl2.tbl2_name AS Name,
tbl2.tbl2_city_name AS City,
'SomeOtherValue' AS Type,
CONVERT( varchar(255), tbl2.tbl2_profile) AS Profile
FROM tbl2
WHERE tbl2.tbl2_id2 = 5
AND CONVERT( varchar( 255 ), tbl2.tbl2_id ) IN ('

SELECT @SQL = @SQL + @CSVString + ')'
EXEC (@SQL)


For more details, check here
http://www.sqlteam.com/SearchResults.asp?SearchTerms=dynamic+SQL&SUBMITs1=Search

Michael

<Yoda>Use the Search page you must. Find the answer you will.</Yoda>
Go to Top of Page

bglodde
Starting Member

25 Posts

Posted - 2002-08-15 : 19:48:53
Michael, Yes, you're correct, I am going to have to do it that way. Here's a good explanation I found in the newsgroups:

quote:

T-SQL is a compiled language, not an interpretative language. As such you need to view how the code looks to the SQL Server database engine. When it is presented with code such as:

WHERE c1 in (@parameter)

the SQL Server database engine is treating the data in @parameter as a single value, even if you happen to be passing a comma-delimited set of values into the stored procedure. To clarify this a bit, let's assume that you have a column holding character information and the strings being stored themselves contain commas. So you have data that looks like:

Washington, George
Adams, John
Jefferson, Thomas

If you passed in for the value of @parameter, "Adams, John", you would want SQL Server to treat the embedded comma as a character in the search string, not as a delimiter between separate elements. Well this in fact is how SQL Server interprets the value of @parameter ... as a **single** element. It may appear to you as a comma-delimited string of values, but SQL Server **must** interpret it as a single element.



So I guess there really is a difference when csv values are passed into variables.

Thanks for your excellent help, I'll post the final outcome for future reference.


Go to Top of Page

MichaelP
Jedi Yak

2489 Posts

Posted - 2002-08-15 : 19:51:14
/me pats self on back

Thanks for the compliment :)

If you can get around storing the data as a CSV value in a single field, we can do it a much better (and faster) way.

Michael

<Yoda>Use the Search page you must. Find the answer you will.</Yoda>
Go to Top of Page

bglodde
Starting Member

25 Posts

Posted - 2002-08-15 : 19:54:12
Works perfectly! Thanks again

Go to Top of Page
   

- Advertisement -