SQL 2000情况下
declare @NAME
set @NAME='我是'
select *from a
where (name like '%@NAME%' or @NAME='')怎么实现WHERE 条件下的模糊查询

解决方案 »

  1.   

    declare @NAME varchar(20)
    set @NAME='我是'
    select *from a
    where (name like '%'+@NAME+'%' or @NAME='')
      

  2.   

    示例:declare @city table (cCode varchar(20),cPCode varchar(20),cCityName varchar(20))
    Insert into @city(cCode,cPCode,cCityName)
     Select '110100','110000','区'
     Union all Select '110200','110000','县'
     Union all Select '120100','120000','区'
     Union all Select '120200','120000','县'
     Union all Select '230900','230000','七台河市'
     Union all Select '231000','230000','牡丹江市'
     Union all Select '231100','230000','黑河市'
     Union all Select '231200','230000','绥化市'
     Union all Select '232223','110000','3234'
     Union all Select '232700','230000','大兴安岭地区'
     Union all Select '310100','310000','区'
     Union all Select '310200','310000','县'
     Union all Select '320100','320000','南京市'
     Union all Select '421200','420000','咸宁市'
     Union all Select '421300','420000','随州市'
     Union all Select '422800','420000','恩施土家族苗族自治州'
     Union all Select '429000','420000','省直辖行政单位'
     Union all Select '430100','430000','长沙市'
     Union all Select '430200','430000','株洲市'
     Union all Select '430300','430000','湘潭市'
     Union all Select '430400','430000','衡阳市'
     Union all Select '430500','430000','邵阳市'
     Union all Select '530800','530000','思茅市'
     Union all Select '530900','530000','临沧市'
     Union all Select '532300','530000','楚雄彝族自治州'
     Union all Select '532500','530000','红河哈尼族彝族自治州'
     Union all Select '532600','530000','文山壮族苗族自治州'
     Union all Select '532800','530000','西双版纳傣族自治州'
     Union all Select '653100','650000','喀什地区'
     Union all Select '653200','650000','和田地区'
     Union all Select '654000','650000','伊犁哈萨克自治州'
     Union all Select '654200','650000','塔城地区'
     Union all Select '654300','650000','阿勒泰地区'
     Union all Select '659000','650000','省直辖行政单位'declare @name varchar(20)
    set @name='县'
    select cCode, cPcode, cCityName from @City where (cCityName like '%'+@name+'%')查询结果:
    /*
    cCode   cPCode  cCityName
    110200 110000 县
    120200 120000 县
    310200 310000 县
    */
    declare @name varchar(20)
    set @name='自治州'
    select cCode, cPcode, cCityName from @City where (cCityName like '%'+@name+'%')查询结果:
    /*
    cCode   cPCode  cCityName
    422800 420000 恩施土家族苗族自治州
    532300 530000 楚雄彝族自治州
    532500 530000 红河哈尼族彝族自治州
    532600 530000 文山壮族苗族自治州
    532800 530000 西双版纳傣族自治州
    654000 650000 伊犁哈萨克自治州*/
    自己看看吧
      

  3.   

    declare @NAME
    set @NAME='我是'
    select *from a
    where name like '%@NAME%' or @NAME = ''
    这样就行了啊
      

  4.   

    declare @NAME
    set @NAME='我是'
    select *from a
    where name like '%@NAME%' or @NAME = ''
    这样是查 @name吧? @name被当成字符来查询.