{"id":82026,"date":"2007-12-05T20:50:44","date_gmt":"2007-12-05T20:50:44","guid":{"rendered":"https:\/\/www.webstaging.red-gate.com\/simple-talk\/?p=73177"},"modified":"2018-12-12T13:38:03","modified_gmt":"2018-12-12T13:38:03","slug":"2008-initializing-table-data-with-row-constructors","status":"publish","type":"post","link":"https:\/\/www.red-gate.com\/simple-talk\/blogs\/2008-initializing-table-data-with-row-constructors\/","title":{"rendered":"2008: Initializing Table Data with Row Constructors"},"content":{"rendered":"<p>Well, I am just discovering this feature, mostly because I never saw it demoed at any of the sessions I have attended so far on 2008.&nbsp; Not that it was kept particularly hidden, I have seen the title before, but I hadn&#8217;t tried it out, or seen the depth that they have &#8220;finally&#8221; implemented.<\/p>\n<p>Tonight, I am working on my chapter where I create some tables as part of a big example, and I had the code from the 2005 version of the book (and I add the primary key to the table later in the book, as well as other constraints, so don&#8217;t judge me!):<\/p>\n<p>CREATE TABLE Inventory.MovieRating (<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; MovieRatingId&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; int NOT NULL,<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Code&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; varchar(20) NOT NULL,<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Description&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; varchar(200) NULL,<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; AllowYouthRentalFlag bit NOT NULL<br \/>)<br \/>GO<\/p>\n<p>INSERT INTO Inventory.MovieRating (MovieRatingId, Code, Description, AllowYouthRentalFlag)<br \/>VALUES (0, &#8216;UR&#8217;,&#8217;Unrated&#8217;,1)<br \/>INSERT INTO Inventory.MovieRating (MovieRatingId, Code, Description, AllowYouthRentalFlag)<br \/>VALUE&nbsp;&nbsp; (1, &#8216;G&#8217;,&#8217;General Audiences&#8217;,1),<br \/>INSERT INTO Inventory.MovieRating (MovieRatingId, Code, Description, AllowYouthRentalFlag)<br \/>VALUES (2, &#8216;PG&#8217;,&#8217;Parental Guidance&#8217;,1),<br \/>INSERT INTO Inventory.MovieRating(MovieRatingId, Code, Description, AllowYouthRentalFlag)<br \/>VALUES (3, &#8216;PG-13&#8242;,&#8217;Parental Guidance for Children Under 13&#8217;,1),<br \/>INSERT INTO Inventory.MovieRating(MovieRatingId, Code, Description, AllowYouthRentalFlag)<br \/>VALUES (4, &#8216;R&#8217;,&#8217;Restricted, No Children Under 17 without Parent&#8217;,0)<\/p>\n<p>(Another variety is to use:<\/p>\n<p>INSERT INTO Inventory.MovieRating (MovieRatingId, Code, Description, AllowYouthRentalFlag)<br \/>SELECT 0, &#8216;UR&#8217;,&#8217;Unrated&#8217;,1<br \/>UNION ALL<br \/>SELECT 1, &#8216;G&#8217;,&#8217;General Audiences&#8217;,1<br \/>UNION ALL<br \/>SELECT 3, &#8216;PG-13&#8242;,&#8217;Parental Guidance for Children Under 13&#8217;,1<br \/>UNION ALL<br \/>SELECT 4, &#8216;R&#8217;,&#8217;Restricted, No Children Under 17 without Parent&#8217;,0<\/p>\n<p>But that is not that much better (certainly a little better).&nbsp; I felt for the book that using VALUES was the more &#8220;proper&#8221; way to do it.&nbsp; However, now, in the 2008 edition, I obviously have to change <em>all <\/em>of the code to use the latest and greatest syntax, so I use row constructors, and this turns into:<\/p>\n<p>INSERT INTO Inventory.MovieRating (MovieRatingId, Code, Description, AllowYouthRentalFlag)<br \/>VALUES (0, &#8216;UR&#8217;,&#8217;Unrated&#8217;,1),<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; (1, &#8216;G&#8217;,&#8217;General Audiences&#8217;,1),<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; (2, &#8216;PG&#8217;,&#8217;Parental Guidance&#8217;,1),<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; (3, &#8216;PG-13&#8242;,&#8217;Parental Guidance for Children Under 13&#8217;,1),<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; (4, &#8216;R&#8217;,&#8217;Restricted, No Children Under 17 without Parent&#8217;,0)<\/p>\n<p>And it just strikes me at how&#8230;simple this is, and how readable this is.&nbsp; I know I have typed the UNION ALL stuff hundreds, possibly thousands (well hundreds more like) of times in the forums over the past year and a half dummying up data for someone who was asking for help but couldn&#8217;t take the time to do it for us.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Well, I am just discovering this feature, mostly because I never saw it demoed at any of the sessions I have attended so far on 2008.&nbsp; Not that it was kept particularly hidden, I have seen the title before, but I hadn&#8217;t tried it out, or seen the depth that they have &#8220;finally&#8221; implemented. Tonight,&#8230;&hellip;<\/p>\n","protected":false},"author":56085,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[2],"tags":[],"coauthors":[19684],"class_list":["post-82026","post","type-post","status-publish","format-standard","hentry","category-blogs"],"acf":[],"_links":{"self":[{"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/posts\/82026","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/users\/56085"}],"replies":[{"embeddable":true,"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/comments?post=82026"}],"version-history":[{"count":1,"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/posts\/82026\/revisions"}],"predecessor-version":[{"id":82374,"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/posts\/82026\/revisions\/82374"}],"wp:attachment":[{"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/media?parent=82026"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/categories?post=82026"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/tags?post=82026"},{"taxonomy":"author","embeddable":true,"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/coauthors?post=82026"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}