1

I have custom function which return a table it accepts two varchars, it splits the varchar based on the delimeter

SELECT VALUE FROM dbo.Split('xxx','_') --- working

select abc from abcd a,cde b where a.abc like (SELECT VALUE FROM dbo.Split(b.abc,'_'))-- not working


select abc from abcd a,cde b where a.abc like (SELECT VALUE FROM dbo.Split('xx','_'))-- working

select abc from abcd a,cde b where a.abc like (SELECT b.abc)-- working

How to get the not working case to work.

Error i get it Incorrect syntax near '.'.

6
  • 1
    I'm sure you meant to ask a question as well ...? Commented Feb 7, 2012 at 11:19
  • 1
    What's the column type of abc from table b? Commented Feb 7, 2012 at 11:20
  • @MikaelEriksson he wants to know why that case isn't working, check the comments. Commented Feb 7, 2012 at 11:20
  • 1
    Can we see your code for dbo.Split() function Commented Feb 7, 2012 at 11:21
  • 1
    It would help if you updated your question with some sample data that causes the error and please add the error message as well. I guess you are getting something like "Subquery returned more than 1 value.". Try to change like to in instead. It might do what you want. Commented Feb 7, 2012 at 11:31

2 Answers 2

2

Using CROSS APPLY allows you to use a variable as a parameter to a function.

SELECT abc
FROM   abcd a
       , cde b
       CROSS APPLY (select VALUE from dbo.Split(b.abc, '_')) f
WHERE  a.abc LIKE f.Value

or

SELECT  *
FROM    abcd a
        , cde b        
        CROSS APPLY dbo.Split(b.abc, '_') f
WHERE   a.abc LIKE f.Value

The APPLY operator allows you to invoke a table-valued function for each row returned by an outer table expression of a query.

Test script

CREATE FUNCTION dbo.Split(@a VARCHAR(4), @b VARCHAR(4)) 
RETURNS TABLE 
AS RETURN
(
  SELECT Value = 'Test'  
)
GO

;WITH abcd (abc) AS (
  SELECT 'Test'
)
, cde (abc) AS (
  SELECT 'Test'
)  
SELECT  *
FROM    abcd a
        , cde b        
        CROSS APPLY (SELECT Value FROM dbo.Split(b.abc, '_')) f
WHERE   a.abc LIKE f.Value
Sign up to request clarification or add additional context in comments.

5 Comments

SELECT * FROM tablesxx c,tableyyy y CROSS APPLY (SELECT TOP 1 VALUE FROM dbo.Split(x.abc,'_')) f WHERE x.abc LIKE f.value Incorrect syntax near 'APPLY'. Incorrect syntax near '.'.
@user1178514 - I've missed one ). The answer has been corrected.
Thanks alot. It works now..CROSS APPLY does not work because I am on SQL SERVER 2000...so I used another method which appraently you suggested stackoverflow.com/questions/4644740/…
@user1178514 - It wasn't clear from your question that there was only one result returned but if it worked for you, you can mark this as answered and upvote my other answer :). See How does accepting an answer works
Marked Answered...Can't upvote have got that much reputation yet...joined recently
0

have you tried:

select abc 
from abcd a, cde b 
where a.abc like dbo.Split(b.abc,'_')

1 Comment

As the function returns a table when I call it like this it throws invalid object name error.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.