簡體   English   中英

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

[英]Using special characters in SQL as string

使用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個單引號代替''

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' ,所以我可以將字符串跨多行

替代解決方案:

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個單引號。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM