当前位置: 代码迷 >> Sql Server >> 请教 存储过程中参数是 uniqueidentifier 类型, 但是条件是 in 的情况下怎么处理
  详细解决方案

请教 存储过程中参数是 uniqueidentifier 类型, 但是条件是 in 的情况下怎么处理

热度:31   发布时间:2016-04-27 12:26:31.0
请问 存储过程中参数是 uniqueidentifier 类型, 但是条件是 in 的情况下怎么办?
如题,现有存储过程 sp_a [email protected]

执行的时候需要输入"ae6810f5-c1f4-4f0e-a772-bf9a6ff8fd65,1699367c-2c5a-409e-b123-eb5d00be8d81" 2个参数。 单个的可以进行转化。 多个的情况下怎么办呢? [email protected] 已经定义为 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))goinsert into tb select 'ae6810f5-c1f4-4f0e-a772-bf9a6ff8fd65','a'union all select '1699367c-2c5a-409e-b123-eb5d00be8d81','b'union all select newid(),'c'gocreate proc sp_a @ids varchar(8000)asbegin  select * from tb where charindex(cast(id as varchar(40)),@ids)>0endgoexec sp_a 'ae6810f5-c1f4-4f0e-a772-bf9a6ff8fd65,1699367c-2c5a-409e-b123-eb5d00be8d81'/**id                                   name------------------------------------ ----------AE6810F5-C1F4-4F0E-A772-BF9A6FF8FD65 a1699367C-2C5A-409E-B123-EB5D00BE8D81 b(2 行受影响)**/--drop table tb--drop proc sp_a
------解决方案--------------------
SQL code
if OBJECT_ID('test') is not nulldrop table testgocreate table test(id uniqueidentifier,value int)goinsert testselect newid(),1 union allselect newid(),2 union allselect newid(),3 union allselect newid(),4 union allselect newid(),5 union allselect newid(),6goselect * 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    28A7C1009-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 转换一下即可。