在SQL中使用特殊字符作为字符串

时间:2013-01-27 07:07:34

标签: sql sql-server-2008

使用SQL Server 2008

DECLARE @myVariable nvarchar (500)

SET @myVariable = 'select distinct b.*,v.vertrag_id,v.VersicherungsscheinNummer 
from CRM_Wifo_GmbH.dbo.vertrag_168 v,temp_universa b 
where v.VersicherungsscheinNummer like '%' + b.vsnr + '% 
and v.gesellschaft_id in('59','66')'

我必须在变量中设置此类型的值。我怎么能这样做?可能吗?使用''签署字符串?

2 个答案:

答案 0 :(得分:4)

您只需使用2个单引号转义单引号'而不是''

DECLARE @myVariable nvarchar (500)
SET @myVariable = 
N'select distinct b.*,v.vertrag_id,v.VersicherungsscheinNummer 
  from CRM_Wifo_GmbH.dbo.vertrag_168 v,temp_universa b 
  where v.VersicherungsscheinNummer like ''%'' + b.vsnr + ''% 
  and v.gesellschaft_id in(''59'',''66'')'

我也在使用N',因此我可以在多行上跨越字符串

答案 1 :(得分:0)

替代解决方案:

DECLARE @myVariable nvarchar (500)
SET @myVariable = 'select distinct b.*,v.vertrag_id,v.VersicherungsscheinNummer from CRM_Wifo_GmbH.dbo.vertrag_168 v,temp_universa b where v.VersicherungsscheinNummer like ' + char(39) + '%' + char(39) + ' + b.vsnr + ' + char(39) + '% and v.gesellschaft_id in(' + char(39) + '59' + char(39) + ',' + char(39) + '66' + char(39) + ')'

但我建议你使用2个单引号。

相关问题