{"id":819,"date":"2015-04-18T15:16:47","date_gmt":"2015-04-18T09:16:47","guid":{"rendered":"http:\/\/dotnetsql.info\/net-sql\/?p=819"},"modified":"2015-07-09T14:37:24","modified_gmt":"2015-07-09T08:37:24","slug":"server-management-studio-scripting-on-tsql-commands","status":"publish","type":"post","link":"http:\/\/dotnetsql.info\/net-sql\/index.php\/server-management-studio-scripting-on-tsql-commands\/","title":{"rendered":"SQL Server Management Studio scripting on TSQL commands"},"content":{"rendered":"<p><strong>Scenario<\/strong><\/p>\n<p>With SQL Server Management Studio you have the ability to do just about everything you can do using T-SQL commands.\u00a0 One problem with using the GUI is that it is difficult to remember everything you clicked on to reproduce the behavior a second time.\u00a0 Another issue is that most of what you do through the GUI is done immediately which may not always be the best scenario.\u00a0 How can I capture what SQL Server is doing so I can reproduce the behavior or run the commands at a later time.<\/p>\n<p><strong>Solution<\/strong><\/p>\n<p>A nice addition to SQL Server Management Studio is the ability to script out most of the commands and operations that you can do through the GUI.\u00a0 Let&#8217;s take a look at doing a database backup as an example.\u00a0<!--more--><\/p>\n<p>Let&#8217;s say we want to backup the AdventureWorks database, but we don&#8217;t want to run the backup immediately we just want to generate the code.\u00a0 One option would be to use Books Online and look at the syntax and another is to let the GUI generate the code.<\/p>\n<p><strong>Letting the GUI produce the script<\/strong><\/p>\n<p>The first thing we need to do is go through the steps of creating a backup via the GUI.\u00a0 To do this we just right click on the database name and select Tasks -&gt; Back Up.\u00a0 Once we have selected all of the parameters that we want for our backup, click on the &#8220;Script&#8221; option in the top middle of the window.<\/p>\n<p>If you click on the down arrow next &#8220;Script&#8221; you will see four options.<\/p>\n<ul>\n<li><strong>Script Action to New Query Window<\/strong> &#8211; this will open a new query window and dump the T-SQL code into this new query window<\/li>\n<li><strong>Script Action to File<\/strong> &#8211; this will allow you to save the T-SQL code to a file<\/li>\n<li><strong>Script Action to Clipboard<\/strong> &#8211; this will copy the T-SQL to the clipboard so you can paste it into another application<\/li>\n<li><strong>Script Action to Job<\/strong> &#8211; this will create a SQL Agent job with the T-SQL code and allow you to schedule the job<\/li>\n<\/ul>\n<p>Here is the output we get when we select any of these options, but I used the &#8220;Script Action to New Query Window&#8221;.\u00a0 I also reformatted the output a little to make it a little easier to read.\u00a0 As you can see this is a pretty easy way to generate a lot of code quickly that you can then customize to meet your needs.<\/p>\n<table style=\"width: 100%;\" border=\"1\" width=\"100%\" cellspacing=\"0\" cellpadding=\"0\">\n<tbody>\n<tr>\n<td>BACKUP\u00a0DATABASE\u00a0[AdventureWorks]<br \/>\nTO\u00a0\u00a0DISK\u00a0=\u00a0N&#8217;C:\\SQL_Backup\\AdventureWorks_full_20080522.BAK&#8217;<br \/>\nWITH\u00a0\u00a0DESCRIPTION\u00a0=\u00a0N&#8217;Full\u00a0backup\u00a0of\u00a0the\u00a0AdventureWorks\u00a0database&#8217;,<br \/>\nNOFORMAT,<br \/>\nINIT,<br \/>\nNAME\u00a0=\u00a0N&#8217;AdventureWorks-Full\u00a0Database\u00a0Backup&#8217;,<br \/>\nSKIP,<br \/>\nNOREWIND,<br \/>\nNOUNLOAD,<br \/>\nSTATS\u00a0=\u00a010,<br \/>\nCHECKSUM<br \/>\nGO<br \/>\nDECLARE\u00a0@backupSetId\u00a0AS\u00a0INT<br \/>\nSELECT\u00a0@backupSetId\u00a0=\u00a0position<br \/>\nFROM\u00a0msdb..backupset<br \/>\nWHERE\u00a0database_name=N&#8217;AdventureWorks&#8217;<br \/>\nAND\u00a0backup_set_id=(SELECT\u00a0MAX(backup_set_id)<br \/>\nFROM\u00a0msdb..backupset<br \/>\nWHERE\u00a0database_name=N&#8217;AdventureWorks&#8217;\u00a0)<br \/>\nIF\u00a0@backupSetId\u00a0IS\u00a0NULL<br \/>\nBEGIN<br \/>\nRAISERROR(N&#8217;Verify\u00a0failed.\u00a0Backup\u00a0information\u00a0for\u00a0database\u00a0&#8221;AdventureWorks&#8221;\u00a0not\u00a0found.&#8217;,\u00a016,\u00a01)<br \/>\nEND<br \/>\nRESTORE\u00a0VERIFYONLY<br \/>\nFROM\u00a0\u00a0DISK\u00a0=\u00a0N&#8217;C:\\SQL_Backup\\AdventureWorks_full_20080522.BAK&#8217;<br \/>\nWITH\u00a0\u00a0FILE\u00a0=\u00a0@backupSetId,<br \/>\nNOUNLOAD,<br \/>\nNOREWIND<br \/>\nGO<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>&nbsp;<\/p>\n<p><strong>Thanks for reading this article,<\/strong><\/p>\n<p><strong>Next steps :<\/strong><\/p>\n<ol>\n<li><strong>Share this with your colleagues because Sharing\u00a0 is Learning<\/strong><\/li>\n<li><strong>Comment below if you need any assistance<\/strong><\/li>\n<\/ol>\n<p style=\"text-align: right;\"><em><strong>Powered by <a href=\"http:\/\/www.codereview.co\">CodeReview &#8211; Let&#8217;s make it Better!<\/a><\/strong><\/em><\/p>\n<div class=\"sharedaddy sd-sharing-enabled\"><div class=\"robots-nocontent sd-block sd-social sd-social-icon sd-sharing\"><h3 class=\"sd-title\">Share this:<\/h3><div class=\"sd-content\"><ul><li class=\"share-twitter\"><a rel=\"nofollow\" data-shared=\"sharing-twitter-819\" class=\"share-twitter sd-button share-icon no-text\" href=\"http:\/\/dotnetsql.info\/net-sql\/index.php\/server-management-studio-scripting-on-tsql-commands\/?share=twitter\" target=\"_blank\" title=\"Click to share on Twitter\"><span><\/span><span class=\"sharing-screen-reader-text\">Click to share on Twitter (Opens in new window)<\/span><\/a><\/li><li class=\"share-linkedin\"><a rel=\"nofollow\" data-shared=\"sharing-linkedin-819\" class=\"share-linkedin sd-button share-icon no-text\" href=\"http:\/\/dotnetsql.info\/net-sql\/index.php\/server-management-studio-scripting-on-tsql-commands\/?share=linkedin\" target=\"_blank\" title=\"Click to share on LinkedIn\"><span><\/span><span class=\"sharing-screen-reader-text\">Click to share on LinkedIn (Opens in new window)<\/span><\/a><\/li><li class=\"share-facebook\"><a rel=\"nofollow\" data-shared=\"sharing-facebook-819\" class=\"share-facebook sd-button share-icon no-text\" href=\"http:\/\/dotnetsql.info\/net-sql\/index.php\/server-management-studio-scripting-on-tsql-commands\/?share=facebook\" target=\"_blank\" title=\"Share on Facebook\"><span><\/span><span class=\"sharing-screen-reader-text\">Share on Facebook (Opens in new window)<\/span><\/a><\/li><li class=\"share-google-plus-1\"><a rel=\"nofollow\" data-shared=\"sharing-google-819\" class=\"share-google-plus-1 sd-button share-icon no-text\" href=\"http:\/\/dotnetsql.info\/net-sql\/index.php\/server-management-studio-scripting-on-tsql-commands\/?share=google-plus-1\" target=\"_blank\" title=\"Click to share on Google+\"><span><\/span><span class=\"sharing-screen-reader-text\">Click to share on Google+ (Opens in new window)<\/span><\/a><\/li><li class=\"share-pocket\"><a rel=\"nofollow\" data-shared=\"\" class=\"share-pocket sd-button share-icon no-text\" href=\"http:\/\/dotnetsql.info\/net-sql\/index.php\/server-management-studio-scripting-on-tsql-commands\/?share=pocket\" target=\"_blank\" title=\"Click to share on Pocket\"><span><\/span><span class=\"sharing-screen-reader-text\">Click to share on Pocket (Opens in new window)<\/span><\/a><\/li><li class=\"share-email\"><a rel=\"nofollow\" data-shared=\"\" class=\"share-email sd-button share-icon no-text\" href=\"http:\/\/dotnetsql.info\/net-sql\/index.php\/server-management-studio-scripting-on-tsql-commands\/?share=email\" target=\"_blank\" title=\"Click to email this to a friend\"><span><\/span><span class=\"sharing-screen-reader-text\">Click to email this to a friend (Opens in new window)<\/span><\/a><\/li><li class=\"share-end\"><\/li><\/ul><\/div><\/div><\/div><div class='sharedaddy sd-block sd-like jetpack-likes-widget-wrapper jetpack-likes-widget-unloaded' id='like-post-wrapper-76243027-819-69d1400b53ab0' data-src='\/\/widgets.wp.com\/likes\/#blog_id=76243027&amp;post_id=819&amp;origin=dotnetsql.info&amp;obj_id=76243027-819-69d1400b53ab0' data-name='like-post-frame-76243027-819-69d1400b53ab0'><h3 class='sd-title'>Like this:<\/h3><div class='likes-widget-placeholder post-likes-widget-placeholder' style='height:55px'><span class='button'><span>Like<\/span><\/span> <span class=\"loading\">Loading...<\/span><\/div><span class='sd-text-color'><\/span><a class='sd-link-color'><\/a><\/div>","protected":false},"excerpt":{"rendered":"<p>Scenario With SQL Server Management Studio you have the ability to do just about everything you can do using T-SQL commands.\u00a0 One problem with using the GUI is that it is difficult to remember everything you clicked on to reproduce the behavior a second time.\u00a0 Another issue is that most of what you do through &hellip; <a href=\"http:\/\/dotnetsql.info\/net-sql\/index.php\/server-management-studio-scripting-on-tsql-commands\/\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">SQL Server Management Studio scripting on TSQL commands<\/span> <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n<div class=\"sharedaddy sd-sharing-enabled\"><div class=\"robots-nocontent sd-block sd-social sd-social-icon sd-sharing\"><h3 class=\"sd-title\">Share this:<\/h3><div class=\"sd-content\"><ul><li class=\"share-twitter\"><a rel=\"nofollow\" data-shared=\"sharing-twitter-819\" class=\"share-twitter sd-button share-icon no-text\" href=\"http:\/\/dotnetsql.info\/net-sql\/index.php\/server-management-studio-scripting-on-tsql-commands\/?share=twitter\" target=\"_blank\" title=\"Click to share on Twitter\"><span><\/span><span class=\"sharing-screen-reader-text\">Click to share on Twitter (Opens in new window)<\/span><\/a><\/li><li class=\"share-linkedin\"><a rel=\"nofollow\" data-shared=\"sharing-linkedin-819\" class=\"share-linkedin sd-button share-icon no-text\" href=\"http:\/\/dotnetsql.info\/net-sql\/index.php\/server-management-studio-scripting-on-tsql-commands\/?share=linkedin\" target=\"_blank\" title=\"Click to share on LinkedIn\"><span><\/span><span class=\"sharing-screen-reader-text\">Click to share on LinkedIn (Opens in new window)<\/span><\/a><\/li><li class=\"share-facebook\"><a rel=\"nofollow\" data-shared=\"sharing-facebook-819\" class=\"share-facebook sd-button share-icon no-text\" href=\"http:\/\/dotnetsql.info\/net-sql\/index.php\/server-management-studio-scripting-on-tsql-commands\/?share=facebook\" target=\"_blank\" title=\"Share on Facebook\"><span><\/span><span class=\"sharing-screen-reader-text\">Share on Facebook (Opens in new window)<\/span><\/a><\/li><li class=\"share-google-plus-1\"><a rel=\"nofollow\" data-shared=\"sharing-google-819\" class=\"share-google-plus-1 sd-button share-icon no-text\" href=\"http:\/\/dotnetsql.info\/net-sql\/index.php\/server-management-studio-scripting-on-tsql-commands\/?share=google-plus-1\" target=\"_blank\" title=\"Click to share on Google+\"><span><\/span><span class=\"sharing-screen-reader-text\">Click to share on Google+ (Opens in new window)<\/span><\/a><\/li><li class=\"share-pocket\"><a rel=\"nofollow\" data-shared=\"\" class=\"share-pocket sd-button share-icon no-text\" href=\"http:\/\/dotnetsql.info\/net-sql\/index.php\/server-management-studio-scripting-on-tsql-commands\/?share=pocket\" target=\"_blank\" title=\"Click to share on Pocket\"><span><\/span><span class=\"sharing-screen-reader-text\">Click to share on Pocket (Opens in new window)<\/span><\/a><\/li><li class=\"share-email\"><a rel=\"nofollow\" data-shared=\"\" class=\"share-email sd-button share-icon no-text\" href=\"http:\/\/dotnetsql.info\/net-sql\/index.php\/server-management-studio-scripting-on-tsql-commands\/?share=email\" target=\"_blank\" title=\"Click to email this to a friend\"><span><\/span><span class=\"sharing-screen-reader-text\">Click to email this to a friend (Opens in new window)<\/span><\/a><\/li><li class=\"share-end\"><\/li><\/ul><\/div><\/div><\/div><div class='sharedaddy sd-block sd-like jetpack-likes-widget-wrapper jetpack-likes-widget-unloaded' id='like-post-wrapper-76243027-819-69d1400b540a7' data-src='\/\/widgets.wp.com\/likes\/#blog_id=76243027&amp;post_id=819&amp;origin=dotnetsql.info&amp;obj_id=76243027-819-69d1400b540a7' data-name='like-post-frame-76243027-819-69d1400b540a7'><h3 class='sd-title'>Like this:<\/h3><div class='likes-widget-placeholder post-likes-widget-placeholder' style='height:55px'><span class='button'><span>Like<\/span><\/span> <span class=\"loading\">Loading...<\/span><\/div><span class='sd-text-color'><\/span><a class='sd-link-color'><\/a><\/div>","protected":false},"author":3,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[25],"tags":[244,241,243,97,40,242,91],"aioseo_notices":[],"_links":{"self":[{"href":"http:\/\/dotnetsql.info\/net-sql\/index.php\/wp-json\/wp\/v2\/posts\/819"}],"collection":[{"href":"http:\/\/dotnetsql.info\/net-sql\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/dotnetsql.info\/net-sql\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/dotnetsql.info\/net-sql\/index.php\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"http:\/\/dotnetsql.info\/net-sql\/index.php\/wp-json\/wp\/v2\/comments?post=819"}],"version-history":[{"count":6,"href":"http:\/\/dotnetsql.info\/net-sql\/index.php\/wp-json\/wp\/v2\/posts\/819\/revisions"}],"predecessor-version":[{"id":984,"href":"http:\/\/dotnetsql.info\/net-sql\/index.php\/wp-json\/wp\/v2\/posts\/819\/revisions\/984"}],"wp:attachment":[{"href":"http:\/\/dotnetsql.info\/net-sql\/index.php\/wp-json\/wp\/v2\/media?parent=819"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/dotnetsql.info\/net-sql\/index.php\/wp-json\/wp\/v2\/categories?post=819"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/dotnetsql.info\/net-sql\/index.php\/wp-json\/wp\/v2\/tags?post=819"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}