Trying to make Drop and Restore dynamic for variable database nameHidden Features of SQL ServerSQL Server: Database stuck in “Restoring” stateChecking If Database In Restoring StateIs it possible to restore a SQL Server database from a virtual drive?Copy database from one server to another using c#Restore database timeout?how can i restore my database with new namePersist Some Data Between Database Backup and RestoreT-SQL Restore Databases with script and variablesTrying to restore SQL Server 2012 Express to 2014

How predictable is $RANDOM really?

Groups where no elements commute except for the trivial cases

Why does this function pointer assignment work when assigned directly but not with the conditional operator?

How to play a D major chord lower than the open E major chord on guitar?

How do resistors generate different heat if we make the current fixed and changed the voltage and resistance? Notice the flow of charge is constant

Is it acceptable that I plot a time-series figure with years increasing from right to left?

What happens if the limit of 4 billion files was exceeded in an ext4 partition?

How do I talk to my wife about unrealistic expectations?

What is the maximum amount of diamond in one Minecraft game?

How to reclaim personal item I've lent to the office without burning bridges?

What is the shape of the upper boundary of water hitting a screen?

What is the Russian diminutive of mouse?

Machine Learning Golf: Multiplication

Can the Four Elements monk's Shape the Flowing River elemental discipline create stairs by expending a single ki point?

Any way to meet code with 40.7% or 40.44% conduit fill?

Does the Milky Way orbit around anything?

Can a USB hub be used to access a drive from two devices?

Who goes first? Person disembarking bus or the bicycle?

Is a lowball salary then a part-time offer standard Japanese employment negotiations?

Why isn't 10.0.0.0/8 used instead of 192.168.0.0/16 for private addresses?

How did the IEC decide to create kibibytes?

How did Einstein know the speed of light was constant?

Taking my Ph.D. advisor out for dinner after graduation

Park the computer



Trying to make Drop and Restore dynamic for variable database name


Hidden Features of SQL ServerSQL Server: Database stuck in “Restoring” stateChecking If Database In Restoring StateIs it possible to restore a SQL Server database from a virtual drive?Copy database from one server to another using c#Restore database timeout?how can i restore my database with new namePersist Some Data Between Database Backup and RestoreT-SQL Restore Databases with script and variablesTrying to restore SQL Server 2012 Express to 2014






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;








0















You know how in SQL Server you can right click a table and choose to generate a drop and create sql and generate a restore sql as well? That's what I have listed below. The first list is hard-coded to work with one database. And the second list is my attempt which isn't working. It has a syntax error near the first + symbol.



Works



EXEC msdb.dbo.sp_delete_database_backuphistory @database_name = N'pubs'
GO
use [pubs]

GO
use [master]

GO
USE [master]
GO
ALTER DATABASE [pubs] SET SINGLE_USER WITH ROLLBACK IMMEDIATE
GO
USE [master]
GO
/****** Object: Database [pubs] Script Date: 3/25/2019 2:58:04 PM ******/
DROP DATABASE [pubs]
GO
RESTORE DATABASE [pubs] FROM DISK = N'C:UsersRSantiapubs.bak' WITH FILE = 1, NOUNLOAD, REPLACE, STATS = 5
GO


My Attempt



DECLARE @lib nvarchar(500) = 'pubs'

EXECUTE sp_executesql

N'EXEC msdb.dbo.sp_delete_database_backuphistory @database_name = ' + '''' + @lib + '''' +
'GO
use ' + @lib +
'GO
use [master]
GO
USE [master]
GO
ALTER DATABASE ' + @lib + ' SET SINGLE_USER WITH ROLLBACK IMMEDIATE
GO
USE [master]
GO
/****** Object: Database [rstestdb100] Script Date: 3/25/2019 2:58:04 PM ******/
DROP DATABASE ' + @lib +
'GO
RESTORE DATABASE ' + @lib + ' FROM DISK = C:UsersRSantia' + @lib + '.bak WITH FILE = 1, NOUNLOAD, REPLACE, STATS = 5
GO'









share|improve this question




























    0















    You know how in SQL Server you can right click a table and choose to generate a drop and create sql and generate a restore sql as well? That's what I have listed below. The first list is hard-coded to work with one database. And the second list is my attempt which isn't working. It has a syntax error near the first + symbol.



    Works



    EXEC msdb.dbo.sp_delete_database_backuphistory @database_name = N'pubs'
    GO
    use [pubs]

    GO
    use [master]

    GO
    USE [master]
    GO
    ALTER DATABASE [pubs] SET SINGLE_USER WITH ROLLBACK IMMEDIATE
    GO
    USE [master]
    GO
    /****** Object: Database [pubs] Script Date: 3/25/2019 2:58:04 PM ******/
    DROP DATABASE [pubs]
    GO
    RESTORE DATABASE [pubs] FROM DISK = N'C:UsersRSantiapubs.bak' WITH FILE = 1, NOUNLOAD, REPLACE, STATS = 5
    GO


    My Attempt



    DECLARE @lib nvarchar(500) = 'pubs'

    EXECUTE sp_executesql

    N'EXEC msdb.dbo.sp_delete_database_backuphistory @database_name = ' + '''' + @lib + '''' +
    'GO
    use ' + @lib +
    'GO
    use [master]
    GO
    USE [master]
    GO
    ALTER DATABASE ' + @lib + ' SET SINGLE_USER WITH ROLLBACK IMMEDIATE
    GO
    USE [master]
    GO
    /****** Object: Database [rstestdb100] Script Date: 3/25/2019 2:58:04 PM ******/
    DROP DATABASE ' + @lib +
    'GO
    RESTORE DATABASE ' + @lib + ' FROM DISK = C:UsersRSantia' + @lib + '.bak WITH FILE = 1, NOUNLOAD, REPLACE, STATS = 5
    GO'









    share|improve this question
























      0












      0








      0








      You know how in SQL Server you can right click a table and choose to generate a drop and create sql and generate a restore sql as well? That's what I have listed below. The first list is hard-coded to work with one database. And the second list is my attempt which isn't working. It has a syntax error near the first + symbol.



      Works



      EXEC msdb.dbo.sp_delete_database_backuphistory @database_name = N'pubs'
      GO
      use [pubs]

      GO
      use [master]

      GO
      USE [master]
      GO
      ALTER DATABASE [pubs] SET SINGLE_USER WITH ROLLBACK IMMEDIATE
      GO
      USE [master]
      GO
      /****** Object: Database [pubs] Script Date: 3/25/2019 2:58:04 PM ******/
      DROP DATABASE [pubs]
      GO
      RESTORE DATABASE [pubs] FROM DISK = N'C:UsersRSantiapubs.bak' WITH FILE = 1, NOUNLOAD, REPLACE, STATS = 5
      GO


      My Attempt



      DECLARE @lib nvarchar(500) = 'pubs'

      EXECUTE sp_executesql

      N'EXEC msdb.dbo.sp_delete_database_backuphistory @database_name = ' + '''' + @lib + '''' +
      'GO
      use ' + @lib +
      'GO
      use [master]
      GO
      USE [master]
      GO
      ALTER DATABASE ' + @lib + ' SET SINGLE_USER WITH ROLLBACK IMMEDIATE
      GO
      USE [master]
      GO
      /****** Object: Database [rstestdb100] Script Date: 3/25/2019 2:58:04 PM ******/
      DROP DATABASE ' + @lib +
      'GO
      RESTORE DATABASE ' + @lib + ' FROM DISK = C:UsersRSantia' + @lib + '.bak WITH FILE = 1, NOUNLOAD, REPLACE, STATS = 5
      GO'









      share|improve this question














      You know how in SQL Server you can right click a table and choose to generate a drop and create sql and generate a restore sql as well? That's what I have listed below. The first list is hard-coded to work with one database. And the second list is my attempt which isn't working. It has a syntax error near the first + symbol.



      Works



      EXEC msdb.dbo.sp_delete_database_backuphistory @database_name = N'pubs'
      GO
      use [pubs]

      GO
      use [master]

      GO
      USE [master]
      GO
      ALTER DATABASE [pubs] SET SINGLE_USER WITH ROLLBACK IMMEDIATE
      GO
      USE [master]
      GO
      /****** Object: Database [pubs] Script Date: 3/25/2019 2:58:04 PM ******/
      DROP DATABASE [pubs]
      GO
      RESTORE DATABASE [pubs] FROM DISK = N'C:UsersRSantiapubs.bak' WITH FILE = 1, NOUNLOAD, REPLACE, STATS = 5
      GO


      My Attempt



      DECLARE @lib nvarchar(500) = 'pubs'

      EXECUTE sp_executesql

      N'EXEC msdb.dbo.sp_delete_database_backuphistory @database_name = ' + '''' + @lib + '''' +
      'GO
      use ' + @lib +
      'GO
      use [master]
      GO
      USE [master]
      GO
      ALTER DATABASE ' + @lib + ' SET SINGLE_USER WITH ROLLBACK IMMEDIATE
      GO
      USE [master]
      GO
      /****** Object: Database [rstestdb100] Script Date: 3/25/2019 2:58:04 PM ******/
      DROP DATABASE ' + @lib +
      'GO
      RESTORE DATABASE ' + @lib + ' FROM DISK = C:UsersRSantia' + @lib + '.bak WITH FILE = 1, NOUNLOAD, REPLACE, STATS = 5
      GO'






      sql-server tsql






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Mar 25 at 20:45









      RodRod

      5,81518 gold badges82 silver badges146 bronze badges




      5,81518 gold badges82 silver badges146 bronze badges






















          1 Answer
          1






          active

          oldest

          votes


















          2














          When I do larger dynamic SQL statements such as this I store the generated statement in an NVARCHAR(MAX) variable and then PRINT it before trying to execute it. You can then parse the generated statement to look for syntax errors which will lead you to where your dynamic SQL is falling down. Like this:



          DECLARE @lib nvarchar(500) = 'pubs'

          DECLARE @SQL NVARCHAR(MAX) =

          N'EXEC msdb.dbo.sp_delete_database_backuphistory @database_name = ' + '''' + @lib + '''' +
          'GO
          use ' + @lib +
          'GO
          use [master]
          GO
          USE [master]
          GO
          ALTER DATABASE ' + @lib + ' SET SINGLE_USER WITH ROLLBACK IMMEDIATE
          GO
          USE [master]
          GO
          /****** Object: Database [rstestdb100] Script Date: 3/25/2019 2:58:04 PM ******/
          DROP DATABASE ' + @lib +
          'GO
          RESTORE DATABASE ' + @lib + ' FROM DISK = C:UsersRSantia' + @lib + '.bak WITH FILE = 1, NOUNLOAD, REPLACE, STATS = 5
          GO'

          PRINT @SQL


          Your generated SQL statement looks like this:



          EXEC msdb.dbo.sp_delete_database_backuphistory @database_name = 'pubs'GO
          use pubsGO
          use [master]
          GO
          USE [master]
          GO
          ALTER DATABASE pubs SET SINGLE_USER WITH ROLLBACK IMMEDIATE
          GO
          USE [master]
          GO
          /****** Object: Database [rstestdb100] Script Date: 3/25/2019 2:58:04 PM ******/
          DROP DATABASE pubsGO
          RESTORE DATABASE pubs FROM DISK = C:UsersRSantiapubs.bak WITH FILE = 1, NOUNLOAD, REPLACE, STATS = 5
          GO


          You can easily see there that your generated statements have some errors. Remember that just because your script has carriage returns doesn't mean that the generate SQL will also have them.



          I think this is probably what you want:



          DECLARE @lib nvarchar(500) = 'pubs'

          DECLARE @SQL NVARCHAR(MAX) =

          N'EXEC msdb.dbo.sp_delete_database_backuphistory @database_name = ' + '''' + @lib + '''
          GO
          use ' + @lib + '
          GO
          use [master]
          GO
          USE [master]
          GO
          ALTER DATABASE ' + @lib + ' SET SINGLE_USER WITH ROLLBACK IMMEDIATE
          GO
          USE [master]
          GO
          /****** Object: Database [rstestdb100] Script Date: 3/25/2019 2:58:04 PM ******/
          DROP DATABASE ' + @lib +
          'GO
          RESTORE DATABASE ' + @lib + ' FROM DISK = C:UsersRSantia' + @lib + '.bak WITH FILE = 1, NOUNLOAD, REPLACE, STATS = 5
          GO'

          EXEC sys.sp_executesql @SQL





          share|improve this answer























          • Thank you so much for the tip about printing and the insight!

            – Rod
            Mar 26 at 17:48










          Your Answer






          StackExchange.ifUsing("editor", function ()
          StackExchange.using("externalEditor", function ()
          StackExchange.using("snippets", function ()
          StackExchange.snippets.init();
          );
          );
          , "code-snippets");

          StackExchange.ready(function()
          var channelOptions =
          tags: "".split(" "),
          id: "1"
          ;
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function()
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled)
          StackExchange.using("snippets", function()
          createEditor();
          );

          else
          createEditor();

          );

          function createEditor()
          StackExchange.prepareEditor(
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader:
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          ,
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          );



          );













          draft saved

          draft discarded


















          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55346146%2ftrying-to-make-drop-and-restore-dynamic-for-variable-database-name%23new-answer', 'question_page');

          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          2














          When I do larger dynamic SQL statements such as this I store the generated statement in an NVARCHAR(MAX) variable and then PRINT it before trying to execute it. You can then parse the generated statement to look for syntax errors which will lead you to where your dynamic SQL is falling down. Like this:



          DECLARE @lib nvarchar(500) = 'pubs'

          DECLARE @SQL NVARCHAR(MAX) =

          N'EXEC msdb.dbo.sp_delete_database_backuphistory @database_name = ' + '''' + @lib + '''' +
          'GO
          use ' + @lib +
          'GO
          use [master]
          GO
          USE [master]
          GO
          ALTER DATABASE ' + @lib + ' SET SINGLE_USER WITH ROLLBACK IMMEDIATE
          GO
          USE [master]
          GO
          /****** Object: Database [rstestdb100] Script Date: 3/25/2019 2:58:04 PM ******/
          DROP DATABASE ' + @lib +
          'GO
          RESTORE DATABASE ' + @lib + ' FROM DISK = C:UsersRSantia' + @lib + '.bak WITH FILE = 1, NOUNLOAD, REPLACE, STATS = 5
          GO'

          PRINT @SQL


          Your generated SQL statement looks like this:



          EXEC msdb.dbo.sp_delete_database_backuphistory @database_name = 'pubs'GO
          use pubsGO
          use [master]
          GO
          USE [master]
          GO
          ALTER DATABASE pubs SET SINGLE_USER WITH ROLLBACK IMMEDIATE
          GO
          USE [master]
          GO
          /****** Object: Database [rstestdb100] Script Date: 3/25/2019 2:58:04 PM ******/
          DROP DATABASE pubsGO
          RESTORE DATABASE pubs FROM DISK = C:UsersRSantiapubs.bak WITH FILE = 1, NOUNLOAD, REPLACE, STATS = 5
          GO


          You can easily see there that your generated statements have some errors. Remember that just because your script has carriage returns doesn't mean that the generate SQL will also have them.



          I think this is probably what you want:



          DECLARE @lib nvarchar(500) = 'pubs'

          DECLARE @SQL NVARCHAR(MAX) =

          N'EXEC msdb.dbo.sp_delete_database_backuphistory @database_name = ' + '''' + @lib + '''
          GO
          use ' + @lib + '
          GO
          use [master]
          GO
          USE [master]
          GO
          ALTER DATABASE ' + @lib + ' SET SINGLE_USER WITH ROLLBACK IMMEDIATE
          GO
          USE [master]
          GO
          /****** Object: Database [rstestdb100] Script Date: 3/25/2019 2:58:04 PM ******/
          DROP DATABASE ' + @lib +
          'GO
          RESTORE DATABASE ' + @lib + ' FROM DISK = C:UsersRSantia' + @lib + '.bak WITH FILE = 1, NOUNLOAD, REPLACE, STATS = 5
          GO'

          EXEC sys.sp_executesql @SQL





          share|improve this answer























          • Thank you so much for the tip about printing and the insight!

            – Rod
            Mar 26 at 17:48















          2














          When I do larger dynamic SQL statements such as this I store the generated statement in an NVARCHAR(MAX) variable and then PRINT it before trying to execute it. You can then parse the generated statement to look for syntax errors which will lead you to where your dynamic SQL is falling down. Like this:



          DECLARE @lib nvarchar(500) = 'pubs'

          DECLARE @SQL NVARCHAR(MAX) =

          N'EXEC msdb.dbo.sp_delete_database_backuphistory @database_name = ' + '''' + @lib + '''' +
          'GO
          use ' + @lib +
          'GO
          use [master]
          GO
          USE [master]
          GO
          ALTER DATABASE ' + @lib + ' SET SINGLE_USER WITH ROLLBACK IMMEDIATE
          GO
          USE [master]
          GO
          /****** Object: Database [rstestdb100] Script Date: 3/25/2019 2:58:04 PM ******/
          DROP DATABASE ' + @lib +
          'GO
          RESTORE DATABASE ' + @lib + ' FROM DISK = C:UsersRSantia' + @lib + '.bak WITH FILE = 1, NOUNLOAD, REPLACE, STATS = 5
          GO'

          PRINT @SQL


          Your generated SQL statement looks like this:



          EXEC msdb.dbo.sp_delete_database_backuphistory @database_name = 'pubs'GO
          use pubsGO
          use [master]
          GO
          USE [master]
          GO
          ALTER DATABASE pubs SET SINGLE_USER WITH ROLLBACK IMMEDIATE
          GO
          USE [master]
          GO
          /****** Object: Database [rstestdb100] Script Date: 3/25/2019 2:58:04 PM ******/
          DROP DATABASE pubsGO
          RESTORE DATABASE pubs FROM DISK = C:UsersRSantiapubs.bak WITH FILE = 1, NOUNLOAD, REPLACE, STATS = 5
          GO


          You can easily see there that your generated statements have some errors. Remember that just because your script has carriage returns doesn't mean that the generate SQL will also have them.



          I think this is probably what you want:



          DECLARE @lib nvarchar(500) = 'pubs'

          DECLARE @SQL NVARCHAR(MAX) =

          N'EXEC msdb.dbo.sp_delete_database_backuphistory @database_name = ' + '''' + @lib + '''
          GO
          use ' + @lib + '
          GO
          use [master]
          GO
          USE [master]
          GO
          ALTER DATABASE ' + @lib + ' SET SINGLE_USER WITH ROLLBACK IMMEDIATE
          GO
          USE [master]
          GO
          /****** Object: Database [rstestdb100] Script Date: 3/25/2019 2:58:04 PM ******/
          DROP DATABASE ' + @lib +
          'GO
          RESTORE DATABASE ' + @lib + ' FROM DISK = C:UsersRSantia' + @lib + '.bak WITH FILE = 1, NOUNLOAD, REPLACE, STATS = 5
          GO'

          EXEC sys.sp_executesql @SQL





          share|improve this answer























          • Thank you so much for the tip about printing and the insight!

            – Rod
            Mar 26 at 17:48













          2












          2








          2







          When I do larger dynamic SQL statements such as this I store the generated statement in an NVARCHAR(MAX) variable and then PRINT it before trying to execute it. You can then parse the generated statement to look for syntax errors which will lead you to where your dynamic SQL is falling down. Like this:



          DECLARE @lib nvarchar(500) = 'pubs'

          DECLARE @SQL NVARCHAR(MAX) =

          N'EXEC msdb.dbo.sp_delete_database_backuphistory @database_name = ' + '''' + @lib + '''' +
          'GO
          use ' + @lib +
          'GO
          use [master]
          GO
          USE [master]
          GO
          ALTER DATABASE ' + @lib + ' SET SINGLE_USER WITH ROLLBACK IMMEDIATE
          GO
          USE [master]
          GO
          /****** Object: Database [rstestdb100] Script Date: 3/25/2019 2:58:04 PM ******/
          DROP DATABASE ' + @lib +
          'GO
          RESTORE DATABASE ' + @lib + ' FROM DISK = C:UsersRSantia' + @lib + '.bak WITH FILE = 1, NOUNLOAD, REPLACE, STATS = 5
          GO'

          PRINT @SQL


          Your generated SQL statement looks like this:



          EXEC msdb.dbo.sp_delete_database_backuphistory @database_name = 'pubs'GO
          use pubsGO
          use [master]
          GO
          USE [master]
          GO
          ALTER DATABASE pubs SET SINGLE_USER WITH ROLLBACK IMMEDIATE
          GO
          USE [master]
          GO
          /****** Object: Database [rstestdb100] Script Date: 3/25/2019 2:58:04 PM ******/
          DROP DATABASE pubsGO
          RESTORE DATABASE pubs FROM DISK = C:UsersRSantiapubs.bak WITH FILE = 1, NOUNLOAD, REPLACE, STATS = 5
          GO


          You can easily see there that your generated statements have some errors. Remember that just because your script has carriage returns doesn't mean that the generate SQL will also have them.



          I think this is probably what you want:



          DECLARE @lib nvarchar(500) = 'pubs'

          DECLARE @SQL NVARCHAR(MAX) =

          N'EXEC msdb.dbo.sp_delete_database_backuphistory @database_name = ' + '''' + @lib + '''
          GO
          use ' + @lib + '
          GO
          use [master]
          GO
          USE [master]
          GO
          ALTER DATABASE ' + @lib + ' SET SINGLE_USER WITH ROLLBACK IMMEDIATE
          GO
          USE [master]
          GO
          /****** Object: Database [rstestdb100] Script Date: 3/25/2019 2:58:04 PM ******/
          DROP DATABASE ' + @lib +
          'GO
          RESTORE DATABASE ' + @lib + ' FROM DISK = C:UsersRSantia' + @lib + '.bak WITH FILE = 1, NOUNLOAD, REPLACE, STATS = 5
          GO'

          EXEC sys.sp_executesql @SQL





          share|improve this answer













          When I do larger dynamic SQL statements such as this I store the generated statement in an NVARCHAR(MAX) variable and then PRINT it before trying to execute it. You can then parse the generated statement to look for syntax errors which will lead you to where your dynamic SQL is falling down. Like this:



          DECLARE @lib nvarchar(500) = 'pubs'

          DECLARE @SQL NVARCHAR(MAX) =

          N'EXEC msdb.dbo.sp_delete_database_backuphistory @database_name = ' + '''' + @lib + '''' +
          'GO
          use ' + @lib +
          'GO
          use [master]
          GO
          USE [master]
          GO
          ALTER DATABASE ' + @lib + ' SET SINGLE_USER WITH ROLLBACK IMMEDIATE
          GO
          USE [master]
          GO
          /****** Object: Database [rstestdb100] Script Date: 3/25/2019 2:58:04 PM ******/
          DROP DATABASE ' + @lib +
          'GO
          RESTORE DATABASE ' + @lib + ' FROM DISK = C:UsersRSantia' + @lib + '.bak WITH FILE = 1, NOUNLOAD, REPLACE, STATS = 5
          GO'

          PRINT @SQL


          Your generated SQL statement looks like this:



          EXEC msdb.dbo.sp_delete_database_backuphistory @database_name = 'pubs'GO
          use pubsGO
          use [master]
          GO
          USE [master]
          GO
          ALTER DATABASE pubs SET SINGLE_USER WITH ROLLBACK IMMEDIATE
          GO
          USE [master]
          GO
          /****** Object: Database [rstestdb100] Script Date: 3/25/2019 2:58:04 PM ******/
          DROP DATABASE pubsGO
          RESTORE DATABASE pubs FROM DISK = C:UsersRSantiapubs.bak WITH FILE = 1, NOUNLOAD, REPLACE, STATS = 5
          GO


          You can easily see there that your generated statements have some errors. Remember that just because your script has carriage returns doesn't mean that the generate SQL will also have them.



          I think this is probably what you want:



          DECLARE @lib nvarchar(500) = 'pubs'

          DECLARE @SQL NVARCHAR(MAX) =

          N'EXEC msdb.dbo.sp_delete_database_backuphistory @database_name = ' + '''' + @lib + '''
          GO
          use ' + @lib + '
          GO
          use [master]
          GO
          USE [master]
          GO
          ALTER DATABASE ' + @lib + ' SET SINGLE_USER WITH ROLLBACK IMMEDIATE
          GO
          USE [master]
          GO
          /****** Object: Database [rstestdb100] Script Date: 3/25/2019 2:58:04 PM ******/
          DROP DATABASE ' + @lib +
          'GO
          RESTORE DATABASE ' + @lib + ' FROM DISK = C:UsersRSantia' + @lib + '.bak WITH FILE = 1, NOUNLOAD, REPLACE, STATS = 5
          GO'

          EXEC sys.sp_executesql @SQL






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Mar 25 at 20:58









          squillmansquillman

          9,3093 gold badges29 silver badges50 bronze badges




          9,3093 gold badges29 silver badges50 bronze badges












          • Thank you so much for the tip about printing and the insight!

            – Rod
            Mar 26 at 17:48

















          • Thank you so much for the tip about printing and the insight!

            – Rod
            Mar 26 at 17:48
















          Thank you so much for the tip about printing and the insight!

          – Rod
          Mar 26 at 17:48





          Thank you so much for the tip about printing and the insight!

          – Rod
          Mar 26 at 17:48








          Got a question that you can’t ask on public Stack Overflow? Learn more about sharing private information with Stack Overflow for Teams.







          Got a question that you can’t ask on public Stack Overflow? Learn more about sharing private information with Stack Overflow for Teams.



















          draft saved

          draft discarded
















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid


          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.

          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55346146%2ftrying-to-make-drop-and-restore-dynamic-for-variable-database-name%23new-answer', 'question_page');

          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          Kamusi Yaliyomo Aina za kamusi | Muundo wa kamusi | Faida za kamusi | Dhima ya picha katika kamusi | Marejeo | Tazama pia | Viungo vya nje | UrambazajiKuhusu kamusiGo-SwahiliWiki-KamusiKamusi ya Kiswahili na Kiingerezakuihariri na kuongeza habari

          SQL error code 1064 with creating Laravel foreign keysForeign key constraints: When to use ON UPDATE and ON DELETEDropping column with foreign key Laravel error: General error: 1025 Error on renameLaravel SQL Can't create tableLaravel Migration foreign key errorLaravel php artisan migrate:refresh giving a syntax errorSQLSTATE[42S01]: Base table or view already exists or Base table or view already exists: 1050 Tableerror in migrating laravel file to xampp serverSyntax error or access violation: 1064:syntax to use near 'unsigned not null, modelName varchar(191) not null, title varchar(191) not nLaravel cannot create new table field in mysqlLaravel 5.7:Last migration creates table but is not registered in the migration table

          은진 송씨 목차 역사 본관 분파 인물 조선 왕실과의 인척 관계 집성촌 항렬자 인구 같이 보기 각주 둘러보기 메뉴은진 송씨세종실록 149권, 지리지 충청도 공주목 은진현