exec sp_executesql N' SELECT * FROM CUSTOMERS  Where CustomerID like ''%''+@CustomerID+''%'' ' , N'@CustomerID char(1)', @CustomerID = N'A'==exec sp_executesql N' SELECT * FROM CUSTOMERS  Where CustomerID like ''%A%'' '

解决方案 »

  1.   

    ''%@CustomerID%'' 
    ---------------------
    ''%''+@CustomerID+''%'' 
    两个参数的写法不同,结果也不一样!
      

  2.   

    两句差远了。第一句实际是查询CustomerID有没有类似%@CustomerID%这样的数据。
    第二句是查询CustomerID有没有类似%A%这样的数据。第一句改为
    exec sp_executesql N' SELECT * FROM CUSTOMERS  Where CustomerID like ''%''+@CustomerID+''%'' ' , N'@CustomerID char(1)', @CustomerID = N'A'才和第二句相同。
      

  3.   

    怎么可能会一样呢,实际执行的两条语句是这样的,你自己也看得出来为什么结果不一样吧?SELECT * FROM CUSTOMERS  Where CustomerID like '%@CustomerID%'
    SELECT * FROM CUSTOMERS  Where CustomerID like '%A%'
      

  4.   

    (邹建) 说的对,第一句你把@customerid看作是一个常量了所以不对
    应该改为
    exec sp_executesql
    N'SELECT * FROM CUSTOMERS  Where CustomerID like ''%''+@CustomerID+''%'''
    N'@CustomerID nvarchar(10)',
    @CustomerID='A'与
    exec sp_executesql N' SELECT * FROM CUSTOMERS  Where CustomerID like ''%A%'' '的结果是一样的了
      

  5.   

    (邹建) 说的对,第一句你把@customerid看作是一个常量了所以不对
    应该改为
    exec sp_executesql
    N'SELECT * FROM CUSTOMERS  Where CustomerID like ''%''+@CustomerID+''%'''
    N'@CustomerID nvarchar(10)',
    @CustomerID='A'与
    exec sp_executesql N' SELECT * FROM CUSTOMERS  Where CustomerID like ''%A%'' '的结果是一样的了
      

  6.   

    create table t
    (
      IDD int not null,
      姓名 varchar(30) not null
    )insert into t
    select '1', 'AB,ABC'
    union all
    select '2', 'ABC,AAA,BBB'
    union all
    select '3', 'ABA,AB'
    union all
    select '3', 'ABA,AAA'
    union all
    select '3', 'ABA,AB,ABCD'
    union all
    select '3', 'ABA,AAA'select * from t where substring(姓名,1,3)='AB,'  --所有AB开头的
    union all
    select * from t where substring(姓名,len(姓名)-2,len(姓名))=',AB' --所有AB结束的
    union all
    select * from t where 姓名 like'%,AB,%' --中间的drop table t/*结果
    1 AB,ABC
    3 ABA,AB
    3 ABA,AB,ABCD
    */