日期:2014-05-17 浏览次数:20650 次
----------------------------------------------------------------
-- Author :DBA_Huangzj(發糞塗牆)
-- Date :2013-10-25 11:31:09
-- 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]([Code] int,[Name] varchar(3))
insert [table]
select 201412,'ABC' union all
select 203413,'BCD' union all
select 201333,'CDE' union all
select 203244,'DEF' union all
select 209416,'FEG' union all
select 207417,'EGH' union all
select 205418,'GHI' union all
select 202419,'HIJ' union all
select 202420,'IJK' union all
select 301412,'JKL' union all
select 303413,'FZD' union all
select 301333,'EFH' union all
select 403244,'WTY' union all
select 509416,'GYU' union all
select 507417,'YTJ' union all
select 605418,'EGP' union all
select 602419,'DGU' union all
select 602420,'QRE'
--------------开始查询--------------------------
declare @Code1 as nvarchar(20)=null
declare @Code2 as nvarchar(20)=null
declare @code3 as nvarchar(20)=NULL
DECLARE @sql VARCHAR(max)
SET @sql='
select * from [table]'
--IF @Code1 IS NOT NULL
--BEGIN
-- SET @sql=@sql+'where code like '+'''%'+@code1+'%'''
--END
IF @code2 is NOT NULL AND @code1 is NOT NULL AND @code3 IS NOT NULL
begin
SET @sql=@sql+'where code like '+'''%'+@code1+'%'''&