日期:2014-05-19  浏览次数:20487 次

关于一个单个参数SQL函数接受来至应用程序传递过来的值的问题
函数:
f_intob(@dbso)
函数里面和@dbso有关的语句(简化):

select   *   from   table   t
where   t.dbso   in   (@dbso)

应用程序给@dbno传递的值类似于;
= '01 '
= '01 ', '02 '
....
= '01 ', '02 ', '03 '....... '20 '

传递 '01 '是没有关系的,
但是如果传递 '01 '下面的多值的话,就出现问题的(相当于多个参数了),如何修改单个参数的函数可以接受类似= '01 ', '02 ', '03 '....... '20 '的参数值

------解决方案--------------------
改用CharIndex

select * from [table] t where CharIndex( ', ' + t.dbso + ', ', ', ' + @dbso + ', ') > 0
------解决方案--------------------
或者Like

select * from [table] t where ', ' + @dbso + ', ' Like '%, ' + t.dbso + ',% '
------解决方案--------------------
declare @dbno varchar(1000)
set @dbno= '01,02,03 '

select * from tb where charindex( ', '+rtrim(dbso)+ ', ' , ', '+@dbno+ ', ')> 0
------解决方案--------------------
成都的网速太差了...呵呵
------解决方案--------------------
楼主为什么不试试上面两位回复的方法呢?那已经是正确的解决之道了.
----创建测试数据
if object_id( 'tbTest ') is not null
drop table tbTest
if object_id( 'fnTest ') is not null
drop function fnTest
GO
create table tbTest(dbso varchar(10))
insert tbTest
select '01 ' union all
select '02 ' union all
select '03 ' union all
select '04 ' union all
select '05 '
GO
----创建函数
create function fnTest(@dbso varchar(100))
returns @t table(dbso varchar(10))
as
begin
insert @t
select * from tbTest where charindex( ', ' + dbso + ', ', ', ' + @dbso + ', ') > 0
return
end
GO
----查询
declare @dbso varchar(100)
set @dbso = '01,02,03 '
select * from dbo.fnTest(@dbso)

----清除测试环境
drop table tbTest
drop function fnTest

/*结果
dbso
----------
01
02
03
*/

------解决方案--------------------
传递的时候把这个引起来就可以传过去 " '01 ', '02 ', '03 ' ",在存储过程中用一个变量接收就可以