日期:2014-05-17 浏览次数:20559 次
----------------------------------------------------------------
-- Author :DBA_Huangzj(發糞塗牆)
-- Date :2014-02-19 13:40:48
-- Version:
-- Microsoft SQL Server 2012 (SP1) - 11.0.3128.0 (X64)
-- Dec 28 2012 20:23:12
-- Copyright (c) Microsoft Corporation
-- Enterprise Edition (64-bit) on Windows NT 6.2 <X64> (Build 9200: )
--
----------------------------------------------------------------
--> 测试数据:[table]
if object_id('[table]') is not null drop table [table]
go
create table [table]([aa] varchar(14))
insert [table]
select '1412,1231,1234' union ALL
select '1412,1231' union all
select '1412,1231' union all
select '1'
--------------开始查询--------------------------
select 'select * from table where id='+REPLACE(aa,',',' or id=')
from [table]
WHERE CHARINDEX(',',aa,1)>0
----------------结果----------------------------
/*
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
select * from table where id=1412 or id=1231 or id=1234
select * from table where id=1412 or id=1231
select * from table where id=1412 or id=1231
*/