SQL Server:表值函数不适用于子查询

SQL Server:表值函数不适用于子查询,sql,sql-server-2005,subquery,user-defined-functions,Sql,Sql Server 2005,Subquery,User Defined Functions,我有一个SQL Server表值函数,它是由我创建的。我已将创建脚本粘贴到下面 CREATE FUNCTION [dbo].[getTableFromString] ( @String AS nvarchar(max) ) RETURNS @ReturnTable TABLE ( StringValues nvarchar(10) ) AS begin if (SELECT CHARINDEX(',', @String)) = 0 be

我有一个SQL Server表值函数,它是由我创建的。我已将创建脚本粘贴到下面

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
我使用这个函数,如下所示

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
                                                                    )  
                 ) 
    )
不幸的是,我得到了这个错误

Msg 156,第15级,状态1,第10行 关键字“select”附近的语法不正确。
Msg 102,15级,状态1,第16行 附近的语法不正确


请帮助我,例如,您需要在子查询周围添加另一组括号

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

                                                 ^                  ^
                                                 (parenthesis added)
第一组括号在语法上属于TVF调用,第二组表示子查询

现在您的问题是:

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
                                                                    )) 
                 ) 
    )

例如,您需要在子查询周围添加另一组括号

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

                                                 ^                  ^
                                                 (parenthesis added)
第一组括号在语法上属于TVF调用,第二组表示子查询

现在您的问题是:

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
                                                                    )) 
                 ) 
    )

您需要将选定的VincludeBrandCodes。。。括号中的子查询向Sql Server表明这实际上是一个子查询。请您再解释一下好吗?请看我的答案。您需要将选定的vIncludedBrandCodes。。。括号中的子查询向Sql Server表明这实际上是一个子查询。请您再解释一下好吗?请看我的答案。Msg 102,15级,状态1,第10行附近的语法不正确。Msg 102,15级,状态1,第17行附近语法不正确。@Prabhakantha它对我有效。您使用的是哪个版本的Sql Server?@Prabhakantha如果当前的查询仍然不起作用,您可以发布它吗?我使用的是兼容级别为80的Sql 2005,我无法更改数据库中的兼容级别,因为一些报告变为不起作用状态:'@Nicola我刚刚粘贴了您的查询,因为您使用的是同一个表名称它应该为我工作:味精102,级别15,状态1,第10行不正确的语法附近。Msg 102,15级,状态1,第17行附近语法不正确。@Prabhakantha它对我有效。您使用的是哪个版本的Sql Server?@Prabhakantha如果当前的查询仍然不起作用,您可以发布它吗?我使用的是兼容级别为80的Sql 2005,我无法更改数据库中的兼容级别,因为一些报告变为不起作用状态:'@Nicola我刚刚粘贴了您的查询,因为您使用的是同一个表它应该适合我的名字: