Click here to monitor SSC
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


single quote in sql statement


single quote in sql statement

Author
Message
sqlfriends
sqlfriends
SSCrazy
SSCrazy (2K reputation)SSCrazy (2K reputation)SSCrazy (2K reputation)SSCrazy (2K reputation)SSCrazy (2K reputation)SSCrazy (2K reputation)SSCrazy (2K reputation)SSCrazy (2K reputation)

Group: General Forum Members
Points: 2041 Visits: 3853
I would like to set a variable that is a sql statement, but the syntax about the quotes is so confusing to me, I cannot make it right:

What I have is this:

SET @sqlcmd = 'BACKUP DATABASE '+@dbname+' TO DISK = ''H:\Preupgrade\ Backups\'+@dbname+'_'+@filedate +' WITH NOFORMAT, NOINIT,' +
' NAME = '''+@dbname+@filedate+''', SKIP, REWIND, NOUNLOAD, STATS = 10'

then EXEC sp_executesql @sqlcmd

How can I use single quotes in statement like above?
Thanks
Lynn Pettis
Lynn Pettis
SSC-Insane
SSC-Insane (24K reputation)SSC-Insane (24K reputation)SSC-Insane (24K reputation)SSC-Insane (24K reputation)SSC-Insane (24K reputation)SSC-Insane (24K reputation)SSC-Insane (24K reputation)SSC-Insane (24K reputation)

Group: General Forum Members
Points: 24171 Visits: 37936
This is what you are trying to accomplish:



declare @dbname varchar(128) = 'TestDB',
@filedate varchar(64) = '20121214210000',
@sqlcmd varchar(max);

SET @sqlcmd = 'BACKUP DATABASE ' + @dbname + ' TO DISK = ''H:\Preupgrade\ Backups\' +
@dbname + '_' + @filedate + '''' + ' WITH NOFORMAT, NOINIT, ' +
'NAME = ' + '''' + @dbname + @filedate + '''' + ', SKIP, REWIND, NOUNLOAD, STATS = 10';

print @sqlcmd;




Cool
Lynn Pettis

For better assistance in answering your questions, click here
For tips to get better help with Performance Problems, click here
For Running Totals and its variations, click here or when working with partitioned tables
For more about Tally Tables, click here
For more about Cross Tabs and Pivots, click here and here
Managing Transaction Logs

SQL Musings from the Desert Fountain Valley SQL (My Mirror Blog)
vinu512
vinu512
Ten Centuries
Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)

Group: General Forum Members
Points: 1149 Visits: 1618
If the no. of quotes are still confusing for you in the solution provided by Lynn......then you can also use the CHAR() function in sql server to apply quotes by using the ASCII value of a single quote(39) as shown below :


declare @dbname varchar(128) = 'TestDB',
@filedate varchar(64) = '20121214210000',
@sqlcmd varchar(max);

SET @sqlcmd = 'BACKUP DATABASE ' + @dbname + ' TO DISK = ' + CHAR(39) + 'H:\Preupgrade\
Backups\' + @dbname + '_' + @filedate + CHAR(39) + ' WITH NOFORMAT, NOINIT, ' +
'NAME = ' + CHAR(39) + @dbname + @filedate + CHAR(39) + ', SKIP, REWIND, NOUNLOAD,
STATS = 10';

print @sqlcmd;



Vinu Vijayan

For better and faster solutions please check..."How to post data/code on a forum to get the best help" - Jeff Moden ;-)
sqlfriends
sqlfriends
SSCrazy
SSCrazy (2K reputation)SSCrazy (2K reputation)SSCrazy (2K reputation)SSCrazy (2K reputation)SSCrazy (2K reputation)SSCrazy (2K reputation)SSCrazy (2K reputation)SSCrazy (2K reputation)

Group: General Forum Members
Points: 2041 Visits: 3853
Thank you both for this, this makes much clearer to understand!
Lynn Pettis
Lynn Pettis
SSC-Insane
SSC-Insane (24K reputation)SSC-Insane (24K reputation)SSC-Insane (24K reputation)SSC-Insane (24K reputation)SSC-Insane (24K reputation)SSC-Insane (24K reputation)SSC-Insane (24K reputation)SSC-Insane (24K reputation)

Group: General Forum Members
Points: 24171 Visits: 37936
Since this is a backup you are trying to run, here is another option:



declare @dbname varchar(128),
@filedate varchar(64);
declare @diskfile varchar(256) = 'H:\Preupgrade\Backups\' + @dbname + '_' + @filedate;
declare @filename varchar(256) = @dbname + @filedate;

declare @SQLCmd varchar(max) =
'BACKUP DATABASE @dbname
TO DISK = @diskfile
WITH NOFORMAT,
NAME = @filename,
SKIP,
REWIND,
NOUNLOAD,
STATS = 10;';
declare @Params @varchar(max) = '@dbname varchar(128), @diskfile varchar(64), @filename varchar(256)';

exec sp_executesql
@SQLCmd,
@Params,
@dbname = @dbname,
@diskfile = @diskfile,
@filename = @filename;




Cool
Lynn Pettis

For better assistance in answering your questions, click here
For tips to get better help with Performance Problems, click here
For Running Totals and its variations, click here or when working with partitioned tables
For more about Tally Tables, click here
For more about Cross Tabs and Pivots, click here and here
Managing Transaction Logs

SQL Musings from the Desert Fountain Valley SQL (My Mirror Blog)
sqlfriends
sqlfriends
SSCrazy
SSCrazy (2K reputation)SSCrazy (2K reputation)SSCrazy (2K reputation)SSCrazy (2K reputation)SSCrazy (2K reputation)SSCrazy (2K reputation)SSCrazy (2K reputation)SSCrazy (2K reputation)

Group: General Forum Members
Points: 2041 Visits: 3853
Thanks, that is helpful!
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search