上贴地址: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.但这里实际的查询结果是什么都差不出来
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.但这里实际的查询结果是什么都差不出来
--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
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
当@SuperiorDeptName传''值时,@SuperiorDeptIndex会为NULL,NULL和字符串拼接。最后结果也是NULL`。所以会有问题`
我把你那个代码在查询分析器里面运行了一下,返回一个只有一个字段,且值为NULL的行
convert(nvarchar(10),@SuperiorDeptIndex)
--改为
convert(nvarchar(10),isnull(@SuperiorDeptIndex,0))
试试
@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表明这里截断了,这个是为什么?
最后的
isnull(convert(nvarchar(10),@SuperiorDeptIndex),'df_SuperiorDeptIndex')
改为;
isnull(convert(nvarchar(50),@SuperiorDeptIndex),'df_SuperiorDeptIndex')
里面有三个字段
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"
--改成这样:
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