Is it possible to perform 2 insert or Update statements using cfquery?
Probably yes. But whether it is possible to run several statements is determined by the type of your database and the driver / connection. For example, when you create an MS SQL data source, multiple IIRC statements are enabled by default. While MySQL drivers often disable several statements by default. This will help avoid sql injection. Therefore, in this case, you must explicitly enable several operators in the connection settings. Otherwise, you cannot use multiple operators. There are also some databases (usually desktop ones like MS Access) that do not support multiple statements at all. Therefore, I do not think that there is a general answer to this question.
If the two insert / update statements are related to each other, you should definitely use cftransaction, as Sam suggested. This ensures that statements are treated as a whole: i.e. either they all succeed, or they all fail. Thus, you are not left with partial or inconsistent data. To achieve this, a single connection will be used for both queries in a transaction.
I think every time we call cfquery we open a new DB connection
As Sam said, it depends on your settings and whether you use cftransaction. If you enable “Liaison” (in the “Data source settings in CF-administrator” section), CF will support the open connection pool. Therefore, when you run a query, CF just grabs an open connection from the pool, rather than opening every new one. When using cftransaction, the same connection should be used for all requests. Regardless of whether connection support is enabled or not.
Leigh source share