2

我有一个由我创建的 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
                                                                    )  
                 ) 
    )

不幸的是我收到了这个错误

消息 156,级别 15,状态 1,第 10 行
关键字“选择”附近的语法不正确。
消息 102,级别 15,状态 1,第 16 行
')' 附近的语法不正确。

请帮我

4

1 回答 1

0

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

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
                                                                    )) 
                 ) 
    )
于 2012-08-08T13:12:09.587 回答