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


String manipulation with variable


String manipulation with variable

Author
Message
sqlquery-101401
sqlquery-101401
Old Hand
Old Hand (301 reputation)Old Hand (301 reputation)Old Hand (301 reputation)Old Hand (301 reputation)Old Hand (301 reputation)Old Hand (301 reputation)Old Hand (301 reputation)Old Hand (301 reputation)

Group: General Forum Members
Points: 301 Visits: 354
i am trying to use @datestamp variable in simple string , but not luck , i dont want to use another variable for full string as i am getting this kind of string input from other job, is there any way i can simple use the variable @datestamp rather using full statement in variable and then execute?

Declare @Datestamp as varchar(10)
SET @Datestamp=Convert(varchar(8),getdate(),112)
BACKUP DATABASE [ReportServer]
TO DISK = 'D:\MSSQL\BAK\ReportServer +'@Datestamp' +.BAK'
WITH FORMAT, COMPRESSION



kogutg
kogutg
Forum Newbie
Forum Newbie (5 reputation)Forum Newbie (5 reputation)Forum Newbie (5 reputation)Forum Newbie (5 reputation)Forum Newbie (5 reputation)Forum Newbie (5 reputation)Forum Newbie (5 reputation)Forum Newbie (5 reputation)

Group: General Forum Members
Points: 5 Visits: 225
You can do this in this way.


Declare @Datestamp as varchar(10)
SET @Datestamp=Convert(varchar(8),getdate(),112)

EXEC('BACKUP DATABASE [ReportServer]
TO DISK = ''D:\MSSQL\BAK\ReportServer'+ @Datestamp+'.BAK''
WITH FORMAT, COMPRESSION')


sqlquery-101401
sqlquery-101401
Old Hand
Old Hand (301 reputation)Old Hand (301 reputation)Old Hand (301 reputation)Old Hand (301 reputation)Old Hand (301 reputation)Old Hand (301 reputation)Old Hand (301 reputation)Old Hand (301 reputation)

Group: General Forum Members
Points: 301 Visits: 354
Awesome , also while trying to do the same i am stuck in string manipulation for @datestamp and with below it is always giving two quotes ,any way to get around this to get single quotes to generate correct statement?

Declare @BACKUPCMD as varchar(5000)
,@BACKUPDIR1 as varchar(5000)
,@DB as varchar (500)
,@BACKUPFILEEXT as varchar(100)
,@WITHSWITCH as varchar(1000)

select @BACKUPDIR1='d:\mssql\bak'
,@DB='reportserver'
,@BACKUPFILEEXT='.bak'
,@WITHSWITCH='WITH FORMAT, COMPRESSION'

Select @BACKUPCMD='Declare @Datestamp as varchar(10)' + char(13)
Select @BACKUPCMD=@BACKUPCMD + 'SET @Datestamp=Convert(varchar(8),getdate(),112)'+ char(13)
SELECT @BACKUPCMD = @BACKUPCMD + 'Exec(''BACKUP DATABASE [' + @DB + ']' + CHAR(13)
+ 'TO DISK = '+'''' + QUOTENAME(@BACKUPDIR1 + '\' + @DB +'''+@Datestamp+'''+@BACKUPFILEEXT , '''') + CHAR(13)
+ @WITHSWITCH +''''+')'+ CHAR(13)



sqlquery-101401
sqlquery-101401
Old Hand
Old Hand (301 reputation)Old Hand (301 reputation)Old Hand (301 reputation)Old Hand (301 reputation)Old Hand (301 reputation)Old Hand (301 reputation)Old Hand (301 reputation)Old Hand (301 reputation)

Group: General Forum Members
Points: 301 Visits: 354
nevermind ,found the issue ,it was quotename causing issue , thanks again for the help.



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