{"id":1730,"date":"2014-12-17T22:17:16","date_gmt":"2014-12-17T20:17:16","guid":{"rendered":"http:\/\/www.unicoda.com\/?p=1730"},"modified":"2014-12-17T22:17:26","modified_gmt":"2014-12-17T20:17:26","slug":"sql-count-result-from-stored-procedure-without-return","status":"publish","type":"post","link":"https:\/\/www.unicoda.com\/?p=1730","title":{"rendered":"[SQL] Count results from stored procedure (without return)"},"content":{"rendered":"<p>It&rsquo;s been a while since I haven&rsquo;t used SQL. Now that I&rsquo;m using it on a daily basis at work, I may write a little more about SQL than before. So here is an article the aim of which is to show how to find the number of results returned by a stored procedure. It&rsquo;s in fact a problem which can be easily solved.<\/p>\n<p>So let&rsquo;s say we have a table containing animals names. Nothing fancy, just something like this:<\/p>\n<pre>name\r\n----\r\nCat\r\nDog\r\nRabbit<\/pre>\n<p>That&rsquo;s it for the table. Now we need a stored procedure, so let&rsquo;s assume we have one : <em>findNameLike<\/em> which take letters as parameter and returns animals names beginning with these letters.<\/p>\n<pre>EXEC findNameLike @beginWith = N'C'<\/pre>\n<p>Cat.<br \/>\nYes, the above call will return Cat.<\/p>\n<p>So, if we want to know how much results were found, we will just write the following code:<\/p>\n<pre>DECLARE @numberOfMatch INT\r\nEXEC findNameLike @beginWith = N'C'\r\nSELECT @numberOfMatch = @@ROWCOUNT<\/pre>\n<p>The number of results is now available in variable <em>numberOfMatch<\/em>.<\/p>\n<p>That&rsquo;s it.<br \/>\nBut maybe you also don&rsquo;t want the stored procedure to return results when you call it. So we need to modify our code a little so that results will be stored in a temporary table and thus not returned, but rather stored in the table.<\/p>\n<pre>DECLARE @numberOfMatch INT\r\nDECLARE @tmpTable TABLE (\r\n\u00a0\u00a0 \u00a0name VARCHAR(25)\r\n)\r\n\r\nINSERT INTO @tmpTable \r\nEXEC findNameLike @beginWith = N'C'\r\nSELECT @numberOfMatch = @@ROWCOUNT<\/pre>\n<p>I think we&rsquo;re done here. In a few lines, we&rsquo;re able to count the results from a stored procedure as well as suppressing its return.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>It&rsquo;s been a while since I haven&rsquo;t used SQL. Now that I&rsquo;m using it on a daily basis at work, I may write a little more about SQL than before. So here is an article the aim of which is to show how to find the number of results returned by a stored procedure. It&rsquo;s &hellip; <a href=\"https:\/\/www.unicoda.com\/?p=1730\" class=\"more-link\">Continuer la lecture<span class=\"screen-reader-text\"> de &laquo;&nbsp;[SQL] Count results from stored procedure (without return)&nbsp;&raquo;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3],"tags":[207,206,208],"class_list":["post-1730","post","type-post","status-publish","format-standard","hentry","category-code","tag-count","tag-sql","tag-stored-procedure"],"_links":{"self":[{"href":"https:\/\/www.unicoda.com\/index.php?rest_route=\/wp\/v2\/posts\/1730","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.unicoda.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.unicoda.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.unicoda.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.unicoda.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1730"}],"version-history":[{"count":2,"href":"https:\/\/www.unicoda.com\/index.php?rest_route=\/wp\/v2\/posts\/1730\/revisions"}],"predecessor-version":[{"id":1732,"href":"https:\/\/www.unicoda.com\/index.php?rest_route=\/wp\/v2\/posts\/1730\/revisions\/1732"}],"wp:attachment":[{"href":"https:\/\/www.unicoda.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1730"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.unicoda.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1730"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.unicoda.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1730"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}