{"id":182526,"date":"2024-03-19T10:41:00","date_gmt":"2024-03-19T09:41:00","guid":{"rendered":"https:\/\/liora.io\/en\/?p=182526"},"modified":"2026-02-12T16:47:51","modified_gmt":"2026-02-12T15:47:51","slug":"sql-rowcount-everything-you-need-to-know-about-sql-formulas","status":"publish","type":"post","link":"https:\/\/liora.io\/en\/sql-rowcount-everything-you-need-to-know-about-sql-formulas","title":{"rendered":"SQL ROWCOUNT: Everything you need to know about SQL formulas"},"content":{"rendered":"\n<p><strong>SQL databases usually contain several hundred or even several thousand lines. But very often, modifications, selections, updates and other instructions only affect a few rows. To find out exactly how many rows have been affected by your various operations, you can use SQL ROWCOUNT. Deciphering.<\/strong><\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"h-sql-rowcount-to-count-rows\">SQL ROWCOUNT to count rows<\/h2>\n\n\n\n<p>@ROWCOUNT is an SQL query designed to return the number of rows affected by the last statement executed. This can be any SQL statement, such as INSERT, UPDATE, DELETE, SELECT&#8230;<\/p>\n\n\n\n<p>For SQL ROWCOUNT to function correctly, it is imperative that both queries (ROWCOUNT and the statement affecting the rows) are in the same execution. If they are executed separately, the value returned will be 1, which does not correspond to the number of rows affected by the previously executed statement.<\/p>\n\n\n\n<p>The aim of @@ROWCOUNT is to avoid infinite loops when a database is analyzed in a loop. When all rows have been processed, SQL ROWCOUNT automatically stops the analysis process.<\/p>\n\n\n\n<p>In addition to processing data in a loop, this query can also be used to check the number of rows allocated in a statement. In this way, ROWCOUNT facilitates error handling.<\/p>\n\n\n\n<p>?Related articles:<\/p>\n\n\n\n<p><a href=\"https:\/\/liora.io\/en\/sql-insert-into-insert-data-with-this-sql-query\">SQL INSERT INTO: Insert data with this SQL query<\/a><\/p>\n\n\n\n<p><a href=\"https:\/\/liora.io\/en\/sql-delete-how-do-i-use-this-query\">SQL DELETE: How do I use this query?<\/a><\/p>\n\n\n\n<p><a href=\"https:\/\/liora.io\/en\/sql-developer-what-is-it-whats-it-for\">SQL Developer: What is it? What&#8217;s it for?<\/a><\/p>\n\n\n\n<p><a href=\"https:\/\/liora.io\/en\/sql-tutorial-top-5-most-useful-methods\">SQL Tutorial: Top 5 Most Useful Methods<\/a><\/p>\n\n\n\n<p><a href=\"https:\/\/liora.io\/en\/sql-joins-everything-you-need-to-know-about\">SQL joins: everything you need to know about table associations<\/a><\/p>\n\n\n\n<p><a href=\"https:\/\/liora.io\/en\/sql-vs-nosql\">SQL vs NoSQL: differences, uses, advantages and disadvantages<\/a><\/p>\n\n\n\n<figure class=\"wp-block-image aligncenter size-large\"><img decoding=\"async\" src=\"https:\/\/liora.io\/app\/uploads\/2023\/10\/ROWCOUNT_SQL1.jpg\" alt=\"\" \/><\/figure>\n\n\n\n<div class=\"wp-block-buttons is-layout-flex wp-block-buttons-is-layout-flex is-content-justification-center\">\n<div class=\"wp-block-button\"><a class=\"wp-block-button__link wp-element-button\" href=\"https:\/\/liora.io\/en\/courses\/data-ai\/\">Learn to use SQL ROWCOUNT<\/a><\/div>\n<\/div>\n\n\n<h2 class=\"wp-block-heading\" id=\"5-examples-of-using-sql-rowcount\">5 examples of using SQL ROWCOUNT<\/h2>\n\n\n\n<p>SQL ROWCOUNT can be executed with any type of instruction. To help you understand, here are a few examples.<\/p>\n\n\n<h3 class=\"wp-block-heading\" id=\"rowcount-and-select\">ROWCOUNT and SELECT<\/h3>\n\n\n\n<p>Simultaneous use of SQL ROWCOUNT and SELECT identifies the number of rows selected by the initial query.<br>Here&#8217;s how it manifests itself:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><b>SELECT TOP (100)<\/b><\/pre>\n\n\n\n<p><b>FROM [table_name]<\/b><br><b>SELECT @@ROWCOUNT<\/b><br>The number of rows affected by the SELECT query will then appear at the bottom of the table.<br>It is also possible to call @@ROWCOUNT after executing two SELECT commands during the same operation.<br>For example:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><b>SELECT TOP (100)<\/b><\/pre>\n\n\n\n<p><b>FROM [table_1_name]<\/b><br><b>SELECT top 100<\/b><br><b>FROM [table_name_1] &#8211; [table_name_2]<\/b><br>In this case, the SQL query ROWCOUNT only considers the second statement.<\/p>\n\n\n<h3 class=\"wp-block-heading\" id=\"rowcount-and-update\">ROWCOUNT and UPDATE<\/h3>\n\n\n\n<p>By calling SQL ROWCOUNT after using a<a href=\"https:\/\/liora.io\/en\/sql-update-update-data-with-this-command\"> UPDATE<\/a> query, you&#8217;ll be able to identify the number of rows updated during this operation.<\/p>\n\n\n\n<p>Here&#8217;s how this combination looks:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><b>UPDATE [table_name]<\/b><\/pre>\n\n\n\n<p><b>SET [column_name] = &#8216;new value&#8217;<\/b><\/p>\n\n\n\n<p><b>WHERE condition<\/b><\/p>\n\n\n\n<p><b>SELECT @@ROWCOUNT<\/b><\/p>\n\n\n\n<p>Here again, the number of updated rows appears below the table.<\/p>\n\n\n\n<figure class=\"wp-block-image is-resized\"><img decoding=\"async\" src=\"https:\/\/liora.io\/app\/uploads\/2023\/10\/ROWCOUNT_SQL2.jpg\" alt=\"\" style=\"width:1000px;height:auto\" title=\"\" \/><\/figure>\n\n\n\n<div class=\"wp-block-buttons is-layout-flex wp-block-buttons-is-layout-flex is-content-justification-center\">\n<div class=\"wp-block-button\"><a class=\"wp-block-button__link wp-element-button\" href=\"https:\/\/liora.io\/en\/courses\/data-ai\/\">Master SQL ROWCOUNT<\/a><\/div>\n<\/div>\n\n\n<h3 class=\"wp-block-heading\" id=\"rowcount-and-delete\">ROWCOUNT and DELETE<\/h3>\n\n\n\n<p>The combination of ROWCOUNT and <a href=\"https:\/\/liora.io\/en\/sql-delete-how-do-i-use-this-query\">DELETE<\/a> identifies the number of lines deleted.<\/p>\n\n\n\n<p>The syntax is as follows:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><b>DELETE [table_name]<\/b><\/pre>\n\n\n\n<p><b>WHERE condition<\/b><\/p>\n\n\n\n<p><b>SELECT @@ROWCOUNT<\/b><\/p>\n\n\n<h3 class=\"wp-block-heading\" id=\"rowcount-and-insert\">ROWCOUNT and INSERT<\/h3>\n\n\n\n<p>With the INSERT query, the SQL command ROWCOUNT indicates the number of rows added.<\/p>\n\n\n\n<p>Here&#8217;s the basic syntax:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><b>INSERT INTO table <\/b>\n<b>VALUES ('value 1', 'value 2', ...)<\/b>\n<b>SELECT @@ROWCOUNT<\/b><\/pre>\n\n\n\n<p>It&#8217;s also possible to run batch queries by adding the GO command.<\/p>\n\n\n\n<p>For example:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><b>INSERT INTO table <\/b><\/pre>\n\n\n\n<p><b>VALUES (&#8216;value 1&#8217;, &#8216;value 2&#8217;, &#8216;value 3&#8217;)<\/b><\/p>\n\n\n\n<p><b>GO 10<\/b><\/p>\n\n\n\n<p><b>SELECT @@ROWCOUNT<\/b><\/p>\n\n\n\n<p>When there are several simultaneous queries in a single operation, SQL ROWCOUNT only takes the last query into account. In this example, even if the INSERT query is repeated 10 times, the ROWCOUNT command will only show 3 modifications, since this is the number of values modified by the last query.<\/p>\n\n\n\n<p>This rule applies to all statements, whether INSERT. SELECT, UPDATE, DELETE, etc.<\/p>\n\n\n<h3 class=\"wp-block-heading\" id=\"rowcount-and-if\">ROWCOUNT and IF<\/h3>\n\n\n\n<p>SQL ROWCOUNT can also be used with IF to control the flow of the instruction. Depending on the number of lines affected, it will be possible to add a specific message.<br>For example:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><b>SELECT TOP (100)<\/b><\/pre>\n\n\n\n<p><b>FROM [table_name]<\/b><br><b>IF @@ROWCOUNT &gt; 10 <\/b><br><b>PRINT &#8220;message_1&#8221;<\/b><br><b>ELSE<\/b><br><b>PRINT &#8220;message_2&#8221;. <\/b><br>In this example, this means: select the top 100 from [table_name]. If the number of rows is greater than 10, write message_1, otherwise, write message_2. In this case, as we&#8217;re selecting a top 100, the number of rows will indeed be greater than 10, so message_1 will appear.<\/p>\n\n\n<h2 class=\"wp-block-heading\" id=\"join-liora-to-master-the-sql-language\">Join Liora to master the SQL language<\/h2>\n\n\n\n<p><strong>SQL ROWCOUNT<\/strong> is just one of the many possibilities offered by the SQL language. There are also updates, deletions, additions, conditions, triggers&#8230;<\/p>\n\n\n\n<p>Mastering this language is a fundamental skill for any professional who needs to learn how to manage <strong>relational databases<\/strong> with fluidity. To do this, you need to combine theory (to know all the available queries) and practice (to apply them on a case-by-case basis). This is precisely what we offer at Liora with our numerous data-related training courses. Find out more!<\/p>\n\n\n\n<figure class=\"wp-block-image aligncenter\"><img decoding=\"async\" src=\"https:\/\/liora.io\/app\/uploads\/2023\/10\/ROWCOUNT_SQL3.jpg\" alt=\"\" style=\"object-fit:cover\" title=\"\" \/><\/figure>\n\n\n\n<div class=\"wp-block-buttons is-layout-flex wp-block-buttons-is-layout-flex is-content-justification-center\">\n<div class=\"wp-block-button\"><a class=\"wp-block-button__link wp-element-button\" href=\"https:\/\/liora.io\/en\/courses\/data-ai\/\">Discover our SQL training<\/a><\/div>\n<\/div>\n\n\n\n<script type=\"application\/ld+json\">\n{\n  \"@context\": \"https:\/\/schema.org\",\n  \"@type\": \"FAQPage\",\n  \"mainEntity\": [\n    {\n      \"@type\": \"Question\",\n      \"name\": \"What is SQL ROWCOUNT?\",\n      \"acceptedAnswer\": {\n        \"@type\": \"Answer\",\n        \"text\": \"SQL ROWCOUNT is a function that returns the number of rows affected by the last executed SQL statement, such as SELECT, INSERT, UPDATE, or DELETE.\"\n      }\n    },\n    {\n      \"@type\": \"Question\",\n      \"name\": \"How does SQL ROWCOUNT work?\",\n      \"acceptedAnswer\": {\n        \"@type\": \"Answer\",\n        \"text\": \"SQL ROWCOUNT works by returning the number of rows that were affected by the last query executed. It should be used within the same execution context to reflect accurate results.\"\n      }\n    },\n    {\n      \"@type\": \"Question\",\n      \"name\": \"How can I use SQL ROWCOUNT with SELECT?\",\n      \"acceptedAnswer\": {\n        \"@type\": \"Answer\",\n        \"text\": \"You can use SQL ROWCOUNT with SELECT to return the number of rows selected by the query. For example: SELECT TOP 100 FROM [table_name]; SELECT @@ROWCOUNT;\"\n      }\n    },\n    {\n      \"@type\": \"Question\",\n      \"name\": \"Can I use SQL ROWCOUNT with UPDATE or DELETE?\",\n      \"acceptedAnswer\": {\n        \"@type\": \"Answer\",\n        \"text\": \"Yes, SQL ROWCOUNT can be used with UPDATE or DELETE to determine how many rows were updated or deleted, respectively, by using the command SELECT @@ROWCOUNT after the operation.\"\n      }\n    },\n    {\n      \"@type\": \"Question\",\n      \"name\": \"How does SQL ROWCOUNT handle multiple queries?\",\n      \"acceptedAnswer\": {\n        \"@type\": \"Answer\",\n        \"text\": \"SQL ROWCOUNT only considers the last executed query in a batch. Even if multiple queries are executed, it will return the row count of the last query.\"\n      }\n    }\n  ]\n}\n<\/script>\n","protected":false},"excerpt":{"rendered":"<p>SQL databases usually contain several hundred or even several thousand lines. But very often, modifications, selections, updates and other instructions only affect a few rows. To find out exactly how many rows have been affected by your various operations, you can use SQL ROWCOUNT. Deciphering. SQL ROWCOUNT to count rows @ROWCOUNT is an SQL query [&hellip;]<\/p>\n","protected":false},"author":82,"featured_media":182536,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"editor_notices":[],"footnotes":""},"categories":[2434],"class_list":["post-182526","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-cloud-dev"],"acf":[],"_links":{"self":[{"href":"https:\/\/liora.io\/en\/wp-json\/wp\/v2\/posts\/182526","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/liora.io\/en\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/liora.io\/en\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/liora.io\/en\/wp-json\/wp\/v2\/users\/82"}],"replies":[{"embeddable":true,"href":"https:\/\/liora.io\/en\/wp-json\/wp\/v2\/comments?post=182526"}],"version-history":[{"count":5,"href":"https:\/\/liora.io\/en\/wp-json\/wp\/v2\/posts\/182526\/revisions"}],"predecessor-version":[{"id":206720,"href":"https:\/\/liora.io\/en\/wp-json\/wp\/v2\/posts\/182526\/revisions\/206720"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/liora.io\/en\/wp-json\/wp\/v2\/media\/182536"}],"wp:attachment":[{"href":"https:\/\/liora.io\/en\/wp-json\/wp\/v2\/media?parent=182526"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/liora.io\/en\/wp-json\/wp\/v2\/categories?post=182526"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}