上贴地址:http://topic.csdn.net/u/20081014/08/0b523ed6-bb60-412f-b652-fffa1a750943.html?seed=1281062988
create procedure udsp_SelectDepartment 
@Name  nvarchar(16), 
@SuperiorDeptName nvarchar(16) 
as 
declare @SuperiorDeptIndex smallint 
if datalength(rtrim(ltrim(@SuperiorDeptName)))=0 
set @SuperiorDeptIndex=null 
else 
set @SuperiorDeptIndex=isnull((select df_Index from udfdt_Department where df_Name=@SuperiorDeptName),0) 
        declare @SelectStatement nvarchar(512) 
set @SelectStatement='select df_Index 流水号,df_Name 名称,(select df_Name from udfdt_Department where df_Index='+convert(nvarchar(10),@SuperiorDeptIndex)+') 上级部门 
                                    from udfdt_Department where df_Name='+isnull('''' + @Name + '''','df_name')+' and df_SuperiorDeptIndex='+isnull(convert(nvarchar(10),@SuperiorDeptIndex),'df_SuperiorDeptIndex') 
execute(@SelectStatement) go exec udsp_selectdepartment "1"," " 这个是我的测试代码,我重新写了一下内容,用datalength来判断rtrim(ltrim(@SuperiorDeptName))是否为0,如果为0说明客户在界面上并没有选择相应的查询条件,这个时候把@SuperiorDeptIndex设置为null。在进行最后的查询的时候,通过事先把@SuperiorDeptIndex设置为null而让当前这个查询条件为恒等 如果datalength(rtrim(ltrim(@SuperiorDeptName)))不为0,说明用户选择或输入了查询的条件,我界面上使用的是一个可以直接输入的combobox控件。这个时候,来对输入内容进行事先的select查询,如果什么都查补出来则把@SuperiorDeptIndex设置为null。最后页通过最后的查询设置为恒等 我最后测试了一下,使用语句  exec udsp_selectdepartment “1”,“ ” 第二个参数带了一个包含一个空格的字符。按道理rtrim(ltrim(@SuperiorDeptName))后第二个参数就成了空字符串,就是什么都没有。这个时候datalength应该是返回0.但这里实际的查询结果是什么都差不出来 

解决方案 »

  1.   


    --try:
    create procedure udsp_SelectDepartment 
    @Name nvarchar(16)=null, 
    @SuperiorDeptName nvarchar(16)=null 
    as 
    declare @SuperiorDeptIndex smallint 
    if datalength(rtrim(ltrim(@SuperiorDeptName)))=0 
    set @SuperiorDeptIndex=null 
    else 
    set @SuperiorDeptIndex=isnull((select df_Index from udfdt_Department where df_Name=@SuperiorDeptName),0) 
            declare @SelectStatement nvarchar(512) 
    set @SelectStatement='select df_Index 流水号,df_Name 名称,(select df_Name from udfdt_Department where df_Index='+isnull(convert(nvarchar(10),@SuperiorDeptIndex),0)+') 上级部门 
                                        from udfdt_Department where df_Name='+isnull('''' + @Name + '''','df_name')+' and df_SuperiorDeptIndex='+isnull(convert(nvarchar(10),@SuperiorDeptIndex),'df_SuperiorDeptIndex') 
    execute(@SelectStatement) 
    go 
      

  2.   

    DECLARE @Name nvarchar(16), @SuperiorDeptName nvarchar(16) 
    SELECT @Name='1',@SuperiorDeptName=' 'declare @SuperiorDeptIndex smallint 
    declare @SelectStatement nvarchar(512) 
    if datalength(rtrim(ltrim(@SuperiorDeptName)))=0 
    set @SuperiorDeptIndex=null 
    else 
    set @SuperiorDeptIndex=isnull((select df_Index from udfdt_Department where df_Name=@SuperiorDeptName),0) SELECT @SuperiorDeptIndexset @SelectStatement='select df_Index 流水号,df_Name 名称,(select df_Name from udfdt_Department where df_Index='+isnull(ltrim(@SuperiorDeptIndex),'df_SuperiorDeptIndex')+') 上级部门 
                          from udfdt_Department where df_Name='+isnull(''''+@Name+'''','df_name')+' and df_SuperiorDeptIndex='+isnull(ltrim(@SuperiorDeptIndex),'df_SuperiorDeptIndex') PRINT @SelectStatement
      

  3.   

    主要是这个地方df_Index='+convert(nvarchar(10),@SuperiorDeptIndex)+') 上级部门 
    当@SuperiorDeptName传''值时,@SuperiorDeptIndex会为NULL,NULL和字符串拼接。最后结果也是NULL`。所以会有问题`
      

  4.   

    to pt1314917  我这个存储过程是想接受界面上的两个combobox窜地过来的参数,用户可以在combobox中直接输入查询的条件,也可以在里面选择。如果这里把参数在申明的时候就设置为初始值为null是不是会有问题。还请赐教
      

  5.   

    to pt314917这个问题怎么解决。SQL中的字符串和空NULL拼接结果也为NULL?如果是这个原因,请赐教怎么可以解决这个问题。
      

  6.   

    to chuifengde
      我把你那个代码在查询分析器里面运行了一下,返回一个只有一个字段,且值为NULL的行
      

  7.   


    convert(nvarchar(10),@SuperiorDeptIndex)
    --改为
    convert(nvarchar(10),isnull(@SuperiorDeptIndex,0))
    试试
      

  8.   

    我实验了一下,在我的存储过程最后加一个print(@SelectStatement),然后传递一个空的@SuperiorDeptName进去,的确是打印不出任何东西,说明我的@SelectStatement最后内容的确是空
      

  9.   

    create procedure udsp_SelectDepartment
    @Name nvarchar(16)=null,
    @SuperiorDeptName nvarchar(16)=null
    as
    declare @SuperiorDeptIndex smallint
    if datalength(rtrim(ltrim(@SuperiorDeptName)))=0
    set @SuperiorDeptIndex=null
    else
    set @SuperiorDeptIndex=isnull((select df_Index from udfdt_Department where df_Name=@SuperiorDeptName),0)
            declare @SelectStatement nvarchar(4000)
    set @SelectStatement='select df_Index 流水号,
             df_Name 名称,
         (select df_Name from udfdt_Department where df_Index=a.df_SuperiorDeptIndex) 上级部门
         from udfdt_Department a where df_Name='+isnull('''' + @Name + '''','df_name')+
               ' and df_SuperiorDeptIndex='+isnull(convert(nvarchar(10),@SuperiorDeptIndex),'df_SuperiorDeptIndex')
    execute(@SelectStatement)
    print(@SelectStatement)goexec udsp_selectdepartment "11"," "报告错误如下:服务器: 消息 207,级别 16,状态 3,行 1
    列名 'df_Superio' 无效。
    select df_Index 流水号,
             df_Name 名称,
         (select df_Name from udfdt_Department where df_Index=a.df_SuperiorDeptIndex) 上级部门
         from udfdt_Department a where df_Name='11' and df_SuperiorDeptIndex=df_Superio表明这里截断了,这个是为什么?
      

  10.   


    最后的
    isnull(convert(nvarchar(10),@SuperiorDeptIndex),'df_SuperiorDeptIndex') 
    改为;
    isnull(convert(nvarchar(50),@SuperiorDeptIndex),'df_SuperiorDeptIndex') 
      

  11.   

    谢谢,你看下我的所有代码我现在说白了就是有一个表udfdt_Department
    里面有三个字段
    df_Index
    df_Name
    df_SuperiorIndex 其中此字段引用到当前表的主键df_Index我现在有一个C#写的界面,上面放两个combobox,其中一个代表df_Name查询条件,另外一个代表df_SuperiorIndex查询条件 现在客户操作界面,可以选择combobox下拉内容中的项目,也可以只输入一个空格或多个空格,甚至什么都不选择和输入现在通过一个存储过程想实现的就是无论用户采用上面哪种操作,最后都可以查出东西同时,用户两个条件都不选择就把所有表中的内容查询出来。下面是我的存储过程和调用测试语句。现在的问题是还是查不出来create procedure udsp_SelectDepartment
    @Name nvarchar(16)=null,
    @SuperiorDeptName nvarchar(16)=null
    as
    declare @SuperiorDeptIndex smallint
    if datalength(rtrim(ltrim(@SuperiorDeptName)))=0
    set @SuperiorDeptIndex=null
    else
    set @SuperiorDeptIndex=isnull((select df_Index from udfdt_Department where df_Name=@SuperiorDeptName),0)
            declare @SelectStatement nvarchar(4000)
    set @SelectStatement='select df_Index 流水号,
             df_Name 名称,
         (select df_Name from udfdt_Department where df_Index=a.df_SuperiorDeptIndex) 上级部门
         from udfdt_Department a where df_Name='+isnull('''' + @Name + '''','df_name')+
               ' and df_SuperiorDeptIndex='+isnull(convert(nvarchar(20),@SuperiorDeptIndex),'df_SuperiorDeptIndex')
    execute(@SelectStatement)
    print(@SelectStatement)goexec udsp_selectdepartment " ","22"
      

  12.   

    现在表中有三个df_SuperiorDeptIndex对应名称为22的记录,但用上面的存储过程什么都查不出来
      

  13.   


    --改成这样:
    create procedure udsp_SelectDepartment 
    @Name nvarchar(16)=null, 
    @SuperiorDeptName nvarchar(16)=null 
    as 
    declare @SuperiorDeptIndex smallint 
    if datalength(rtrim(ltrim(@SuperiorDeptName)))=0 
    set @SuperiorDeptIndex=null 
    else 
    set @SuperiorDeptIndex=isnull((select df_Index from udfdt_Department where df_Name=@SuperiorDeptName),0) 
    declare @SelectStatement nvarchar(4000) 
    set @SelectStatement='select df_Index 流水号, 
          df_Name 名称, 
        (select df_Name from udfdt_Department where df_Index=a.df_SuperiorDeptIndex) 上级部门 
        from udfdt_Department a where df_Name='+isnull('''' + nullif(ltrim(rtrim(@Name)),'') + '''','df_name')+ 
              ' and df_SuperiorDeptIndex='+isnull(convert(nvarchar(20),@SuperiorDeptIndex),'df_SuperiorDeptIndex') 
    execute(@SelectStatement) 
    go