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%'' '
调试欢乐多
---------------------
''%''+@CustomerID+''%''
两个参数的写法不同,结果也不一样!
第二句是查询CustomerID有没有类似%A%这样的数据。第一句改为
exec sp_executesql N' SELECT * FROM CUSTOMERS Where CustomerID like ''%''+@CustomerID+''%'' ' , N'@CustomerID char(1)', @CustomerID = N'A'才和第二句相同。
SELECT * FROM CUSTOMERS Where CustomerID like '%A%'
应该改为
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%'' '的结果是一样的了
应该改为
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%'' '的结果是一样的了
(
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
*/