我想从这个动态sql中删除引号,所以它只是一个脚本。
set @sqlDNB= '
SELECT DISTINCT
Null CustomerID
,EntityID MCVID
,business_name CustomerName
,physical_City + ',' + physical_state + '' + cast(physical_zip as varchar(7)) Address
,'' RM
,'' SubGroup
,'' HierarchyType
,'' status
,DNB.dunsnum DunsNumber
,'DNB' Source
from SOR.T_DunAndBradstreet DNB with (nolock)
left outer join SOR.T_McvDuns MCVDUNS with (nolock)
on DNB.dunsnum = MCVDUNS.DunsNumber
+
case
when @p_SearchMethod ='BeginsWith' and @p_CustomerName !='' then 'and business_name like ''' + @p_CustomerName + ''' '
when @p_SearchMethod ='Contains' and @p_CustomerName !='' then 'and Contains (business_name,''"' + @p_CustomerName + '"'')'
when @p_SearchMethod ='Exact' and @p_CustomerName !='' then 'and business_name =''' + @p_CustomerName + ''' '
else
''
end
+ ' where not exists
(select dunsnumber from sor.T_Customer customer where customer.DUNSnumber = DNB.dunsnum '
+
case
when @p_SearchMethod ='BeginsWith' and @p_CustomerName !='' then 'and customer.Name like ''' + @p_CustomerName + ''' '
when @p_SearchMethod ='Contains' and @p_CustomerName !='' then 'and Contains (customer.Name,''"' + @p_CustomerName + '"'')'
when @p_SearchMethod ='Exact' and @p_CustomerName !='' then 'and customer.Name =''' + @p_CustomerName + ''' '
else
''
end
if @p_TIN_SSN !=''
begin
set @sqlDNB = @sqlDNB + ' and customer.TINorSSN =''' + @p_TIN_SSN + ''' '
end
if @p_CustomerID !=''
begin
set @sqlDNB = @sqlDNB + ' and customer.[MCVID] =''' + cast(@p_CustomerID as varchar(20)) + ''' '
end
set @sqlDNB = @sqlDNB + ')'
答案 0 :(得分:1)
从以下代码中删除任何单引号都会导致错误:
'and business_name like ''' + @p_CustomerName + ''' '
当你在SQL中的字符串中看到''
时,它代表一个'
。例如
declare @test varchar(12)
set @test = 'don''t'
select @test
将返回don't
。
因此,从您提到的代码块中删除任何单引号都会导致like
中断。
希望这是有道理的。
答案 1 :(得分:1)
在SQL Server Management Studio中“调试”此类脚本的一种方便方法如下:
DECLARE @sqlDNB nvarchar(max);
set @sqlDNB = <your script here>;
PRINT @sqlDNB;
如果您运行此操作,您将获得可用于修改脚本的特定错误消息。最后,打印的字符串是SQL脚本,您也可以复制和测试它。