如何在CSVToTable函数中传递null或空参数

时间:2014-08-12 08:06:46

标签: c# sql-server

我必须根据选中的复选框做一个过滤器。我有5个搜索条件,我刚刚发布了2.我会选择样式并将solid_yarn留空(Mean null->将不会选择)。那么基于所选值的sql函数CSVToTable无法正常工作?

if (style != null)
    {
        for (int i = 0; i < style.Length; i++)
        {
            parameterList.Append(style[i] + ",");
        }
        var parameters = parameterList.ToString().TrimEnd(',');
        cmd.Parameters.AddWithValue("@Liststyle", parameters);
    }

if (solid_yarn != null)
    {
        var parameterList1 = new StringBuilder();
        for (int i = 0; i < solid_yarn.Length; i++)
        {
            parameterList1.Append(solid_yarn[i] + ",");
        }
        var parameters1 = parameterList1.ToString().TrimEnd(',');
        cmd.Parameters.AddWithValue("@Listsolidyarn", parameters1);
    }

我的CSVToTable功能

    ALTER FUNCTION [dbo].[CSVToTableStyle] (@InStr VARCHAR(MAX))
RETURNS @TempTab TABLE
   (Style varchar(MAX) not null)
AS
BEGIN
    ;-- Ensure input ends with comma
    SET @InStr = REPLACE(@InStr + ',', ',,', ',')
    DECLARE @SP INT
DECLARE @VALUE VARCHAR(1000)
WHILE PATINDEX('%,%', @INSTR ) <> 0 
BEGIN
   SELECT  @SP = PATINDEX('%,%',@INSTR)
   SELECT  @VALUE = LEFT(@INSTR , @SP - 1)
   SELECT  @INSTR = STUFF(@INSTR, 1, @SP, '')
   INSERT INTO @TempTab(Style) VALUES (@VALUE)
END
    RETURN
END

和我在存储过程中的sql查询如下..

@Liststyle varchar(MAX)=null,
@Listsolidyarn varchar(MAX)=null,
select * from tbl_Pgm_Preview where (Brand=@brand) and (Department=@department) and (Season=@season) and (Group_Name=@groupname)
and (Style in ( Select * from dbo.[CSVToTableStyle](@Liststyle)))
and (Solid_yarn in ( Select * from dbo.[CSVToTableSolidYarn](@Listsolidyarn)))

0 个答案:

没有答案