作者 : Kevin

eg: table customer
columns :
 customerCode string
 customerName string
 customerGroup string
 customerRouteNum string
 ...........

现在为了查处选定的cusotmerCode的Customer的全部信息并显示在界面上(总共的customer数量为11029),每次读一个,显然很慢,因为可以多选,可能1个,可能是所有,所以读出所有然后剔出未选的,效率也不高。

因此开始采用存储过程:

CREATE PROCEDURE TS_GetCustomersByNames
( @custCodes nvarchar(3700)
) AS

select * from customer
where customerName in (@custNames)

一直得不到正确结果,发现不论传入参数 @custNames =  N'''Taste Of Punjab (Tsim Sha Tsui)'',''Lily Food Wholesales (Tuen Mun)'''
还是 @custNames =  N'Taste Of Punjab (Tsim Sha Tsui),Lily Food Wholesales (Tuen Mun)'(注:其实这种明显不对,试验一下而已)

但如果用select * from customer
where customerName in (''Taste Of Punjab (Tsim Sha Tsui)'',''Lily Food Wholesales (Tuen Mun)')----  (X)   
当然是有正确结果的。

那原因是什么呢?
原因是: @CustCodes作为参数传入时,编译处理导致实际执行的不同于语句(X)。

但我们就是要得到语句(X)的结果,怎么解决呢?办法是使用Exec执行,如下:
declare @sql nvarchar(3800)
set @sql = 'select * from customer where customerCode in ( '+ @custCodes + ')'
exec ( @sql )
GO


相关文章:

  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-09-26
  • 2022-12-23
  • 2021-08-29
  • 2022-12-23
猜你喜欢
  • 2021-06-22
  • 2022-12-23
  • 2021-11-16
  • 2022-12-23
  • 2022-12-23
  • 2022-03-10
  • 2021-05-20
相关资源
相似解决方案