none
Parameter replace and add quotes RRS feed

  • Question

  • Declare @tragetdb Varchar(max)
    SET @tragetdb='xyz1'
     Declare @RestoreCmd Varchar(max)
    SET @RestoreCmd= 'RESTORE DATABASE XYZ FROM DATABASE_SNAPSHOT='+''+ ' @targetdb'+ ''
     print @RestoreCmd

    O/p:

    RESTORE DATABASE XYZ FROM DATABASE_SNAPSHOT= @targetdb

    but i am looking for 

    RESTORE DATABASE XYZ FROM DATABASE_SNAPSHOT= 'xyz1'

     i tried using "" + "" , ' + ' , no lcuk...can some one suggest please

    Friday, September 25, 2015 6:13 PM

Answers

  • Declare @tragetdb Varchar(max)
    SET @tragetdb='xyz1'
     Declare @RestoreCmd Varchar(max)
    SET @RestoreCmd= 'RESTORE DATABASE XYZ FROM DATABASE_SNAPSHOT='''+    @tragetdb +  ''''
     print @RestoreCmd
    

    • Marked as answer by coolguy123SQL Friday, September 25, 2015 6:38 PM
    Friday, September 25, 2015 6:20 PM
    Moderator
  • You can try

    Declare @snapshotDB sysname
    SET @snapshotDB='xyz1'
     Declare @RestoreCmd Varchar(max)
    SET @RestoreCmd= 'RESTORE DATABASE XYZ FROM DATABASE_SNAPSHOT='+ quotename(@snapshotDB, '''')
     print @RestoreCmd

    However, double check if you can run this command as is:

    RESTORE DATABASE XYZ FROM DATABASE_SNAPSHOT = @snapshotDB


    For every expert, there is an equal and opposite expert. - Becker's Law


    My blog


    My TechNet articles

    • Marked as answer by coolguy123SQL Friday, September 25, 2015 6:38 PM
    Friday, September 25, 2015 6:22 PM
    Moderator

All replies

  • Declare @tragetdb Varchar(max)
    SET @tragetdb='xyz1'
     Declare @RestoreCmd Varchar(max)
    SET @RestoreCmd= 'RESTORE DATABASE XYZ FROM DATABASE_SNAPSHOT='''+    @tragetdb +  ''''
     print @RestoreCmd
    

    • Marked as answer by coolguy123SQL Friday, September 25, 2015 6:38 PM
    Friday, September 25, 2015 6:20 PM
    Moderator
  • You can try

    Declare @snapshotDB sysname
    SET @snapshotDB='xyz1'
     Declare @RestoreCmd Varchar(max)
    SET @RestoreCmd= 'RESTORE DATABASE XYZ FROM DATABASE_SNAPSHOT='+ quotename(@snapshotDB, '''')
     print @RestoreCmd

    However, double check if you can run this command as is:

    RESTORE DATABASE XYZ FROM DATABASE_SNAPSHOT = @snapshotDB


    For every expert, there is an equal and opposite expert. - Becker's Law


    My blog


    My TechNet articles

    • Marked as answer by coolguy123SQL Friday, September 25, 2015 6:38 PM
    Friday, September 25, 2015 6:22 PM
    Moderator
  • You misspelled your variable name. And you need to use two single quote to get one single quote (escape).

    Check the code I posted.

    Friday, September 25, 2015 6:23 PM
    Moderator
  • i did check and thanks all for your help..both worked
    Friday, September 25, 2015 6:49 PM