2

Using 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?

2 Answers 2

4

You just need to escape the single quote ' using 2 single quotes instead ''

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

Sign up to request clarification or add additional context in comments.

1 Comment

WOW!! Great!!! this worked. and thanks a lot. i will accept it as right answere after 11 mins :)
0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.