---
title: "In的使用"
author: "devilhand"
date: "2010-07-29T20:50:23Z"
tags: []
categories: ["数据库"]
slug: "in"
draft: false
lastmod: "2010-07-29T20:50:23Z"
id: 724747
url: "https://devilhand.iteye.com/blog/724747"
---

In的使用

```
declare @id int
declare @l int
declare @s varchar(50)
declare @r varchar(1000)
set @id=1
set @s=(select cids from A where id=@id)
set @l=1
set @r=''
while (@l<=len(@s))
begin 
if(@l<len(@s))
begin
set @r=@r+'select cid,[name] from B where cid in('+substring(@s,@l,1)+') union all '
end
else
begin
set @r=@r+'select cid,[name] from B where cid in('+substring(@s,@l,1)+')'
end
set @l=@l+2
end
exec(@r)
```