0
declare @CustField6 nvarchar(10)
declare @sample nvarchar(10)
set @CustField6 = 'sf'

select customer_id  from csu_customer_policy
    inner join csu_policy on csu_policy.policy_number = csu_customer_policy.policy_number
    and csu_policy.data_source = csu_customer_policy.data_source
    where csu_policy.vehicle_no like  + '''' + @CustField6 + '%''' and csu_customer_policy.delete_status = 0 and csu_customer_policy.site_id = 1

我想要所有csu_policy.vehicle_nosf%

如果我sf直接给出值而不是 @CustField6给出我的所有记录但是当我使用@CustField6它执行它时不会给出任何错误但没有检索到任何记录,即使我打印了这段代码来检查查询是否正确被框定,并且它正在工作美好的。但我不知道为什么它没有检索任何记录?

4

6 回答 6

2

你的语法不正确。它应该是:

where csu_policy.vehicle_no like @CustField6 + '%'

假设@CustField6等于sf,这等价于

where csu_policy.vehicle_no like 'sf%'

如果你这样做:

where csu_policy.vehicle_no like ''''  + @CustField6 + '%''''

然后你实际上是在这样做:

where csu_policy.vehicle_no like '''sf%'''

换句话说,您正在查找匹配的'sf%'记录 - 以引号开头,然后是sf,然后是任何内容,然后以引号结尾的记录。

于 2013-06-21T04:04:43.087 回答
1

将您的标准更改为:

WHERE csu_policy.vehicle_no LIKE @CustField6 + '%' 

演示: SQL 小提琴

于 2013-06-21T04:09:28.897 回答
0

我认为您的查询的问题在于您有几个到多个'并且+在您的 where 条件下:它应该where csu_policy.data_source like @custField6 + '%'而不是csu_policy.vehicle_no like + '''' + @CustField6 + '%'''

declare @CustField6 nvarchar(10) 
declare @sample nvarchar(10) 
set @CustField6 = 'sf'

select customer_id 
from csu_customer_policy 
inner join csu_policy on csu_policy.policy_number = csu_customer_policy.policy_number 
      and csu_policy.data_source = csu_customer_policy.data_source 
where csu_policy.vehicle_no like @CustField6 + '%' 
and csu_customer_policy.delete_status = 0 and csu_customer_policy.site_id = 1
于 2013-06-21T04:04:58.073 回答
0

尝试摆脱 +

from
 csu_policy.vehicle_no like + '''' + @CustField6 + '%'''

To
 csu_policy.vehicle_no like  ''''+ @CustField6 + '%'''
于 2013-06-21T04:07:39.700 回答
0

删除不需要的引号.. 试试这个。

declare @CustField6 nvarchar(10)
declare @sample nvarchar(10)
set @CustField6 = 'sf'

select customer_id  from csu_customer_policy
    inner join csu_policy on csu_policy.policy_number = csu_customer_policy.policy_number
    and csu_policy.data_source = csu_customer_policy.data_source
    where (csu_policy.vehicle_no like  + '' + @CustField6 + '%') and    
           csu_customer_policy.delete_status = 0 and 
           csu_customer_policy.site_id = 1
于 2013-06-21T04:24:28.357 回答
0

请使用单引号,如下所示:

declare @CustField6 nvarchar(10)
declare @sample nvarchar(10)
set @CustField6 = 'sf'

select customer_id  from csu_customer_policy
    inner join csu_policy on csu_policy.policy_number = csu_customer_policy.policy_number
    and csu_policy.data_source = csu_customer_policy.data_source
    where csu_policy.vehicle_no like  '' + @CustField6 + '%' and csu_customer_policy.delete_status = 0 and csu_customer_policy.site_id = 1
于 2013-06-21T04:33:32.970 回答