日期:2014-05-18  浏览次数:20508 次

请问 存储过程中参数是 uniqueidentifier 类型, 但是条件是 in 的情况下怎么办?
如题,现有存储过程 sp_a 参数@ids

执行的时候需要输入"ae6810f5-c1f4-4f0e-a772-bf9a6ff8fd65,1699367c-2c5a-409e-b123-eb5d00be8d81" 2个参数。 单个的可以进行转化。 多个的情况下怎么办呢? 参数@ids 已经定义为 varchar(8000)

但是 where id in (@ids) 后 只有第一条数据。 好郁闷。 谢谢了。

------解决方案--------------------
SQL code

declare @var varchar(100)
    set @var='D6B183B5-CE92-4EAE-B13C-17F20B864C1F,606AF7DC-EBAD-48A0-B81C-AB8C9862E45C'
with cte as
(
    select 'D6B183B5-CE92-4EAE-B13C-17F20B864C1F' as VGUID union all
    select '606AF7DC-EBAD-48A0-B81C-AB8C9862E45C' union all 
    select '9E0DE149-D12D-4956-935C-A58528AC488D' 
)    
select * from cte where @var like '%'+VGUID+'%' 

--VGUID
--------------------------------------
--D6B183B5-CE92-4EAE-B13C-17F20B864C1F
--606AF7DC-EBAD-48A0-B81C-AB8C9862E45C

--(2 row(s) affected)

------解决方案--------------------
SQL code
create table tb(
  id uniqueidentifier,
  name varchar(10)
)
go

insert into tb select 'ae6810f5-c1f4-4f0e-a772-bf9a6ff8fd65','a'
union all select '1699367c-2c5a-409e-b123-eb5d00be8d81','b'
union all select newid(),'c'
go

create proc sp_a @ids varchar(8000)
as
begin
  select * from tb where charindex(cast(id as varchar(40)),@ids)>0
end
go

exec sp_a 'ae6810f5-c1f4-4f0e-a772-bf9a6ff8fd65,1699367c-2c5a-409e-b123-eb5d00be8d81'
/**
id                                   name
------------------------------------ ----------
AE6810F5-C1F4-4F0E-A772-BF9A6FF8FD65 a
1699367C-2C5A-409E-B123-EB5D00BE8D81 b

(2 行受影响)
**/

--drop table tb
--drop proc sp_a

------解决方案--------------------
SQL code

if OBJECT_ID('test') is not null
drop table test
go
create table test(
id uniqueidentifier,
value int
)
go
insert test
select newid(),1 union all
select newid(),2 union all
select newid(),3 union all
select newid(),4 union all
select newid(),5 union all
select newid(),6
go

select * from test 
where cast(id as varchar(50)) 
in('8A7C1009-9F97-4091-A0D1-28AA3D4ECCCB','E9A35845-99E0-4899-ACE2-213064117893')
/*
id    value
--------------------------------------------------
E9A35845-99E0-4899-ACE2-213064117893    2
8A7C1009-9F97-4091-A0D1-28AA3D4ECCCB    4
*/

--直接转换就好了

------解决方案--------------------
探讨
SQL code

create table tb(
id uniqueidentifier,
name varchar(10)
)
go

insert into tb select 'ae6810f5-c1f4-4f0e-a772-bf9a6ff8fd65','a'
union all select '1699367c-2c5a-409e-b123-eb5d00be8……

------解决方案--------------------
用cast或是convert 或是ltrim 转换一下即可。