{"id":1022,"date":"2013-06-03T11:09:24","date_gmt":"2013-06-03T01:09:24","guid":{"rendered":"http:\/\/ntsblog.homedev.com.au\/?p=1022"},"modified":"2013-06-03T11:10:42","modified_gmt":"2013-06-03T01:10:42","slug":"ssdt-generate-script-throws-object-set-instance-object-error","status":"publish","type":"post","link":"https:\/\/ntsblog.homedev.com.au\/index.php\/2013\/06\/03\/ssdt-generate-script-throws-object-set-instance-object-error\/","title":{"rendered":"SSDT generate script throws an &#8220;object not set to an instance of an object&#8221; error"},"content":{"rendered":"<div id=\"ntsbl-2900242723\" class=\"ntsbl-before-content ntsbl-entity-placement\"><script async src=\"\/\/pagead2.googlesyndication.com\/pagead\/js\/adsbygoogle.js?client=ca-pub-6288941070289539\" crossorigin=\"anonymous\"><\/script><ins class=\"adsbygoogle\" style=\"display:inline-block;width:728px;height:90px;\" \ndata-ad-client=\"ca-pub-6288941070289539\" \ndata-ad-slot=\"9356781486\"><\/ins> \n<script> \n(adsbygoogle = window.adsbygoogle || []).push({}); \n<\/script>\n<\/div><h2>The Issue<\/h2>\n<p>I created a Visual Studio Database project by scripting the code directly off the server in question.<\/p>\n<p>When I then try to publish the project clicking Deploy &#8211;> Generate Script, I get an &#8220;Object not set to the instance of an object&#8221; error.<\/p>\n<p>I get no other information about the problem<\/p>\n<h2>The Investigation<\/h2>\n<p>So to work out what was going on I excluded all the stored procedure and function logic from the project and tried to generate the script, and it worked.<\/p>\n<p>\nThe next step was to include scripts back into the project, trying to &#8220;Generate Script&#8221; every time I added scripts back until it failed. <\/p>\n<h2>The problem<\/h2>\n<p>[sql]<br \/>\nCREATE FUNCTION [dbo].[GetSomething]<br \/>\n(<br \/>\n@date DATETIME<br \/>\n)<br \/>\nRETURNS TABLE<br \/>\nAS<br \/>\nRETURN<br \/>\n(<br \/>\nSELECT 1 as column1, 2 as column2<br \/>\n);<br \/>\n[\/sql]<br \/>\nA script like the one above causes it to fail to generate successfully.<\/p>\n<h2>The solution<\/h2>\n<p>The problem is that it does not like the function returning a select statement directly. <\/p>\n<p>\nChange the code to use a table variable, like so:<\/p>\n<p>[sql]<br \/>\nCREATE FUNCTION [dbo].[GetSomething]<br \/>\n(<br \/>\n@date DATETIME<br \/>\n)<br \/>\nRETURNS @returntable TABLE<br \/>\n(<br \/>\n  column1 int,<br \/>\n  column2 int<br \/>\n)<br \/>\nAS<br \/>\nBEGIN<br \/>\n    Insert Into @returntable<br \/>\n    SELECT 1 as column1, 2 as column2<\/p>\n<p>    return;<br \/>\nEND<br \/>\n[\/sql]<\/p>\n<p>The project will now build <\/p>\n","protected":false},"excerpt":{"rendered":"<p>The Issue I created a Visual Studio Database project by scripting the code directly off the server in question. When I then try to publish [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"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":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[13],"tags":[],"class_list":["post-1022","post","type-post","status-publish","format-standard","hentry","category-sql-server"],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/ntsblog.homedev.com.au\/index.php\/wp-json\/wp\/v2\/posts\/1022","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/ntsblog.homedev.com.au\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/ntsblog.homedev.com.au\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/ntsblog.homedev.com.au\/index.php\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/ntsblog.homedev.com.au\/index.php\/wp-json\/wp\/v2\/comments?post=1022"}],"version-history":[{"count":0,"href":"https:\/\/ntsblog.homedev.com.au\/index.php\/wp-json\/wp\/v2\/posts\/1022\/revisions"}],"wp:attachment":[{"href":"https:\/\/ntsblog.homedev.com.au\/index.php\/wp-json\/wp\/v2\/media?parent=1022"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ntsblog.homedev.com.au\/index.php\/wp-json\/wp\/v2\/categories?post=1022"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ntsblog.homedev.com.au\/index.php\/wp-json\/wp\/v2\/tags?post=1022"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}