情况是这样的
比如某个字段aa里数据是(1412,1231,1234)(1412,1231)(1412,1231)(1)数值长度不一定,几个逗号也不一定。
表:
字段:aa
数据:1412,1231,1234
1412,1231
1412,1231
1
请问如何自动生成相关语句
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 = 112 or ID = 115
没有逗号的不生成语句
不知道这样描述说的能不能明白
谢谢帮助
------解决方案--------------------
修改一下
----------------------------------------------------------------
-- 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
*/