How to execute multiple cfquery one after another. First execute and completion the first cfquery then proceed to next cfquery execution and move to another cfquery. All the below cfquery are in one single page.
//--------first cfquery--------//
<cfquery name="first" datasource="test">
Delete from tab where id='123'
</cfquery>
//--------second cfquery--------//
<cfquery name="first" datasource="test">
insert into tab (id,name,age) values ('123','xxx','37')
</cfquery>
//--------Third cfquery--------//
<cfquery name="first" datasource="test">
select * from tab where id='123'
</cfquery>
Queries are executed subsequently in ColdFusion. Your issue is rather related to the script being executed several times in parallel.
Executing the script multiple times in parallel causes concurrency issues. That means, the queries get sent to the database server from different script calls at the same time and there is no guaranteed order in which they are executed.
There are two things you should do to avoid these concurrency problems.
Put all of them in one
<cfquery>Putting them all in one
<cfquery>the queries get sent to the server all at once. This puts the database server in charge of executing them. And it executes them in order.Wrap them in a
<cftransaction>Wrapping your queries in a
<cftransaction>block allows the database engine to execute all of them as a single transaction. You can imagine that as an "all or nothing" action.Besides that it allows you to catch errors and revert your changes if something goes wrong.