SQL Server 2008存储过程执行问题

时间:2016-10-14 06:04:39

标签: sql-server tsql stored-procedures

我将尝试详细解释这个问题。我写了一个SP并成功执行了它。这是:

SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO

ALTER PROCEDURE [dbo].[InvoiceReference] 

@StartDate DateTime = NULL,
@EndDate DateTime = NULL,
@DocumentType nvarchar(50) = NULL,
@Partners nvarchar(MAX) = NULL,
@PriceFrom numeric(19,6) = NULL,
@PriceTo numeric(19,6) = NULL,
@VATFrom numeric(19,6) = NULL,
@VATTo numeric(19,6) = NULL,

@PageNumber INT,
@PageSize INT

AS
BEGIN   SET NOCOUNT ON;

    DECLARE @StartPage as int
    DECLARE @EndPage as int

    SET @StartPage = ((@PageNumber-1) * @PageSize) + 1;
    SET @EndPage = @StartPage + (@PageSize) - 1;


    WITH ResultSet As (select 

    ROW_NUMBER() OVER (order by d.DocumentID) AS 'RowNumber',
    d.DocNumber, dt.Name as DocumentTypesName, d.Date, p.Name as PartnersName, dd.SalePrice, dd.VAT, (dd.SalePrice + dd.VAT) as TotalSum

    from [dbo].[Documents] d join [dbo].[DocumentTypes] dt on d.TypeID = dt.TypeID
                             join [dbo].[Partners] p on d.PartnerID = p.PartnerID
                             join [dbo].[DocumentDetails] dd on d.DocumentID = dd.DocumentID


    where ((@StartDate is null) or (d.Date >= @StartDate))
    and ((@EndDate is null) or (d.Date <= @EndDate))
    and ((@DocumentType is null) or (dt.Name = @DocumentType))
    --and ((@Partners is null) or (p.Name = @Partners))
    and ((@Partners is null) or (p.Name in (select * from dbo.fnSplitString(@Partners, ','))))
    and ((@PriceFrom is null) or (dd.SalePrice >= @PriceFrom))
    and ((@PriceTo is null) or (dd.SalePrice <= @PriceTo))
    and ((@VATFrom is null) or (dd.VAT >= @VATFrom))
    and ((@VATTo is null) or (dd.VAT <= @VATTo))

    )

    Select * from ResultSet rs WHERE RowNumber between @StartPage and @EndPage

    ORDER BY rs.Date ASC

END

但后来我发现我必须通过多个“合作伙伴”进行过滤(例如:Partner1,Partner2,......)。然后我编写了一个函数,它将SP参数@Partners拆分为单独的字符串。但当我评论“where”子句中的旧部分并推出新部分(调用函数iwtrote)时,它给了我这个错误消息:“无法解决”Cyrillic_General_CI_AS“和”Cyrillic_General_CS_AS“之间的校对冲突等于操作“。当我试图执行SP时。错误在第26行,即: SET @StartPage =((@ PageNumber-1)* @PageSize)+ 1; 我没有改变任何东西。我读了一些我在这里找到关于整理的matirials,但我仍然无法自己解决问题。我写的函数是:

 ALTER FUNCTION [dbo].[fnSplitString] 
( 
    @string NVARCHAR(MAX), 
    @delimiter CHAR(1) 
) 
RETURNS @output TABLE(splitdata NVARCHAR(200) 
) 
BEGIN 
    DECLARE @start INT, @end INT 
    SELECT @start = 1, @end = CHARINDEX(@delimiter, @string) 
    WHILE @start < LEN(@string) + 1 BEGIN 
        IF @end = 0  
            SET @end = LEN(@string) + 1

        INSERT INTO @output (splitdata)  
        VALUES(SUBSTRING(@string, @start, @end - @start)) 
        SET @start = @end + 1 
        SET @end = CHARINDEX(@delimiter, @string, @start)

    END 
    RETURN 
END

任何帮助将不胜感激。

1 个答案:

答案 0 :(得分:0)

您必须在比较IN语句之前的结果OR之前设置默认排序规则。

尝试以下条件:

and ((@Partners is null) or (p.Name COLLATE DATABASE_DEFAULT in (select * from dbo.fnSplitString(@Partners, ','))))

Set datatype to NVARCHAR of related column