2

I have a SQL Server table-valued function which is created by me I have pasted the create script below

CREATE FUNCTION [dbo].[getTableFromString]
(       
    @String AS nvarchar(max)

)
RETURNS @ReturnTable TABLE ( StringValues nvarchar(10)  )   
AS begin 

    if (SELECT CHARINDEX(',', @String)) = 0
        begin
                insert into @ReturnTable (StringValues) values (subString(@String,1,len(@String)));
        end 
    else
        begin 
            while (SELECT CHARINDEX(',', @String)) > 0
            begin 
                insert into @ReturnTable (StringValues) values (subString(@String,1,CHARINDEX(',', @String)-1));
                set @String  = subString(@String,CHARINDEX(',', @String)+1,len(@String));

                if (SELECT CHARINDEX(',', @String)) = 0
                begin
                        insert into @ReturnTable (StringValues) values (subString(@String,1,len(@String)));
                end         
            end 
        end

    return ;
end

and I am using this function like below

Select sum(NetSales)
from vwxsalesall
where Company = 'rs'
and storecode = (select cPrimaryStockRoomCode from CompanyMaster.CompanyProfileDetail where cCompanyNo = 'rs' and cSecondaryStockRoomCode = 'R01B')
and trandate >= '2012-01-01'
and trandate <= '2012-01-31'
and ( 
        brand in (  
                    select StringValues from  dbo.getTableFromString(
                                                                    select vIncludedBrandCodes
                                                                    from StockRoomTargetData.MonthlyTarget 
                                                                    where cCompanyNo = 'rs' 
                                                                    and cSecondaryStockRoomCode = 'R01B'
                                                                    and nYear = 2012 
                                                                    and nMonth = 8
                                                                    )  
                 ) 
    )

Unfortunately I am getting this error

Msg 156, Level 15, State 1, Line 10
Incorrect syntax near the keyword 'select'.
Msg 102, Level 15, State 1, Line 16
Incorrect syntax near ')'.

please help me

3
  • 4
    You need to put select vIncludedBrandCodes ... subquery into parenthesis to indicate to Sql Server that this is in fact a subquery. Commented Aug 8, 2012 at 12:06
  • Can you please explain little bit more please ? Commented Aug 8, 2012 at 13:07
  • 1
    Please take a look at my answer. Commented Aug 8, 2012 at 13:21

1 Answer 1

0

You need to add another set of parenthesis around subquery, for instance

select StringValues from  dbo.getTableFromString(( { your-subquery }))

                                                 ^                  ^
                                                 (parenthesis added)

First set of parenthessis syntactically belong to TVF invocation, and second denote a subquery.

And now your query:

Select sum(NetSales)
from vwxsalesall
where Company = 'rs'
and storecode = (select cPrimaryStockRoomCode from CompanyMaster.CompanyProfileDetail where cCompanyNo = 'rs' and cSecondaryStockRoomCode = 'R01B')
and trandate >= '2012-01-01'
and trandate <= '2012-01-31'
and ( 
        brand in (  
                    select StringValues from  dbo.getTableFromString((
                                                                    select vIncludedBrandCodes
                                                                    from StockRoomTargetData.MonthlyTarget 
                                                                    where cCompanyNo = 'rs' 
                                                                    and cSecondaryStockRoomCode = 'R01B'
                                                                    and nYear = 2012 
                                                                    and nMonth = 8
                                                                    )) 
                 ) 
    )
Sign up to request clarification or add additional context in comments.

9 Comments

Msg 102, Level 15, State 1, Line 10 Incorrect syntax near '('. Msg 102, Level 15, State 1, Line 17 Incorrect syntax near ')'.
@Prabhakantha It works for me, see it yourself. Which version of Sql Server are you using?
@Prabhakantha Could you please post your current query if it is still not working?
i am using sql 2005 with compatibility level 80 and i cannot change the compatibility level in my database as some of the reports goes to not working state :'(
@Nicola I Just Pasted your query since you are using the same table names it should work for me :)
|

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.