简体   繁体   English

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

[英]Using special characters in SQL as string

Using 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')'

I have to set the value of this type in a variable. 我必须在变量中设置此类型的值。 How could I do this? 我该怎么办? Is it possible? 可能吗? USING ' ' sign in a string? 使用''登录字符串?

You just need to escape the single quote ' using 2 single quotes instead '' 你只需要逃避单引号'使用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'')'

I am also using N' , so that I can span the string on multiple lines 我也在使用N' ,所以我可以将字符串跨多行

Alternative solutions : 替代解决方案:

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) + ')'

But i suggesst you, using 2 single quotes. 但我建议您使用2个单引号。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM