Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to find number of rows affected by INSERT INTO statement in ColdFusion And MySql Database?

I have following query in ColdFusion:

<cfquery name="test" datasource="test">
    INSERT INTO test (id,name) VALUES (1,"santy"),(2,"raj"),(3,"nil")
</cfquery> 

I want to find number of affected rows count. Is there any way to find this in ColdFusion?

like image 849
Santosh D. Avatar asked May 10 '16 11:05

Santosh D.


People also ask

How can I see how many rows affected in MySQL?

mysql_affected_rows() may be called immediately after executing a statement with mysql_real_query() or mysql_query() . It returns the number of rows changed, deleted, or inserted by the last statement if it was an UPDATE , DELETE , or INSERT . For SELECT statements, mysql_affected_rows() works like mysql_num_rows() .

How do you get the number of rows affected by a query?

MySQL ROW_COUNT() can be used to get the total number of rows affected by MySQL query. To illustrate it we are creating a procedure with the help of which we can insert records in a table and it will show us how many rows have been affected.


3 Answers

<cftransaction>
    <cfquery name="test" datasource="test">
        INSERT INTO test (id,name) VALUES (1,"santy"),(2,"raj"),(3,"nil")
    </cfquery>
    <cfquery name="test1" datasource="test">
        SELECT ROW_COUNT() AS numberOfRowsAffected
    </cfquery>
</cftransaction>
<cfdump var="#test1.numberOfRowsAffected#">

Or

You can make changes in the settings to allow multiple sql statements withing one cfquery. To make that happen, make changes as mentioned below:

In ColdFusion Admin, go to your Data Source definition form and add allowMultiQueries=true to the Connection String box. Once you do that, you can pass multiple semi-colon separated queries in a single CFQUERY tag. Just make sure you're using CFQUERYPARAM to screen for SQL Injection Attacks.

like image 97
Tushar Bhaware Avatar answered Nov 15 '22 06:11

Tushar Bhaware


Try this one.

<cfquery datasource="test" result="myResult">
  INSERT INTO test (id,name) VALUES (1,"santy"),(2,"raj"),(3,"nil")
</cfquery>
<cfset getNumberOfRecords = listLen(#myResult.generated_key#)>
<cfdump var="#getNumberOfRecords#">

myResult.generated_key contains the list of generated ids, so we can find how many rows have been inserted when we use listLen() function.

like image 36
W Gunvant Avatar answered Nov 15 '22 05:11

W Gunvant


Use the result attribute and then access the recordCount key of the result.

<cfquery result="myResult" datasource="test">
  INSERT INTO test (id,name) VALUES (1,"santy"),(2,"raj"),(3,"nil")
</cfquery> 

<cfdump var="#myResult.recordCount#">
like image 33
John Whish Avatar answered Nov 15 '22 04:11

John Whish