{"id":5681,"date":"2014-10-02T11:00:26","date_gmt":"2014-10-02T15:00:26","guid":{"rendered":"https:\/\/www.caskeys.com\/dc\/?p=5681"},"modified":"2017-01-22T10:07:28","modified_gmt":"2017-01-22T15:07:28","slug":"quick-tip-copy-table-with-transact-sql","status":"publish","type":"post","link":"https:\/\/www.caskeys.com\/dc\/quick-tip-copy-table-with-transact-sql\/","title":{"rendered":"Quick Tip &#8211; Copy Table With Transact SQL"},"content":{"rendered":"<p>Here&#8217;s an easy one, but sometimes still troublesome for individuals testing the waters of SQL: How to quickly copy a table. There are lots of cases where you need to duplicate a table in all but name. Maybe you want the data as well, maybe not. Unfortunately this seemingly simple task is lacking as a functionality in most development tools &#8211; including the big boys like MS SQL Server Management Studio.<\/p>\n<p>The good news is there&#8217;s a perfectly logical reason such a feature isn&#8217;t apparent: It&#8217;s part of SQL already, and super simple to boot. Here&#8217;s what to do:<\/p>\n<pre class=\"brush: sql; title: Code:; notranslate\" title=\"Code:\">\r\nSELECT * INTO schema.new_table FROM schema.existing_table\r\n<\/pre>\n<p>Execute this as a query and voila! You now have a duplicate of the old table, complete with data. Want to make a copy sans data? Just query for a non existent key value.<\/p>\n<pre class=\"brush: sql; title: Code:; notranslate\" title=\"Code:\">\r\nSELECT * INTO schema.new_table FROM schema.existing_table WHERE some_field = non_existing_value\r\n<\/pre>\n<p>It&#8217;s really that simple. No plug ins or complex write ups required.<\/p>\n<h2>Extra Credit<\/h2>\n<ul>\n<li>You needn&#8217;t copy every field. Swap out * with a list of desired fields, just like any other query.<\/li>\n<li>Play with the WHERE cause a bit and you can copy portions of data rather than all or nothing. Again, to your SQL engine it&#8217;s just another query, so use your imagination.<\/li>\n<\/ul>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>How to quickly make table duplicates, with or without data.<\/p>\n","protected":false},"author":1,"featured_media":6206,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":true,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":false,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2}},"categories":[71],"tags":[253,254,298],"class_list":["post-5681","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-technology-temerity","tag-coding-database","tag-coding-sql","tag-technology-temerity"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"https:\/\/www.caskeys.com\/dc\/wp-content\/uploads\/2017\/01\/logo-sql.png","jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/p5lNM5-1tD","jetpack_likes_enabled":true,"_links":{"self":[{"href":"https:\/\/www.caskeys.com\/dc\/wp-json\/wp\/v2\/posts\/5681","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.caskeys.com\/dc\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.caskeys.com\/dc\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.caskeys.com\/dc\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.caskeys.com\/dc\/wp-json\/wp\/v2\/comments?post=5681"}],"version-history":[{"count":2,"href":"https:\/\/www.caskeys.com\/dc\/wp-json\/wp\/v2\/posts\/5681\/revisions"}],"predecessor-version":[{"id":5683,"href":"https:\/\/www.caskeys.com\/dc\/wp-json\/wp\/v2\/posts\/5681\/revisions\/5683"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.caskeys.com\/dc\/wp-json\/wp\/v2\/media\/6206"}],"wp:attachment":[{"href":"https:\/\/www.caskeys.com\/dc\/wp-json\/wp\/v2\/media?parent=5681"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.caskeys.com\/dc\/wp-json\/wp\/v2\/categories?post=5681"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.caskeys.com\/dc\/wp-json\/wp\/v2\/tags?post=5681"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}