Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ColdFusion: Multiple SQL statements in a query?

Apparently ColdFusion doesn't like multiple SQL statements within a single query... so what once was this:

SET @sender_user_id = 3, @recipient_user_id = 5;

INSERT INTO messages (message_type, sender_id, message_title, message_content) 
  VALUES(3, @sender_user_id, 'One more thing...', 'I am testing this message');

SET @saved_message_id = LAST_INSERT_ID();

INSERT INTO message_recipient (message_id, user_id) 
  VALUES(@saved_message_id, @recipient_user_id);

INSERT INTO message_status (message_id, user_id, is_read, read_datetime, is_deleted, deleted_datetime)
  VALUES (@saved_message_id, @recipient_user_id, 0, NULL, 0, NULL);

Get's turned into this:

<cftransaction>

    <cfquery name="insertMessage" dataSource="mySource">

        SET @sender_user_id = 3, @recipient_user_id = 5;

    </cfquery>
    <cfquery name="insertMessage2" dataSource="mySource">

        INSERT INTO messages (message_type, sender_id, message_title, message_content) 
        VALUES(3, @sender_user_id, '#params.message_title#', '#params.message_content#');

    </cfquery>
    <cfquery name="insertMessage3" dataSource="mySource">

        SET @saved_message_id = LAST_INSERT_ID();

    </cfquery>
    <cfquery name="insertMessage4" dataSource="mySource">   

        INSERT INTO message_recipient (message_id, user_id) 
        VALUES(@saved_message_id, @recipient_user_id);

    </cfquery>
    <cfquery name="insertMessage5" dataSource="mySource">   

        INSERT INTO message_status (message_id, user_id, is_read, read_datetime, is_deleted, deleted_datetime)
        VALUES (@saved_message_id, @recipient_user_id, 0, NULL, 0, NULL);

    </cfquery>

</cftransaction>

This works... but I'm wondering if there is some ColdFusion thing that I don't know about. Otherwise, this works.

like image 546
dcolumbus Avatar asked Jun 09 '11 21:06

dcolumbus


People also ask

How do I run multiple SQL statements in one query?

To run a query with multiple statements, ensure that each statement is separated by a semicolon; then set the DSQEC_RUN_MQ global variable to 1 and run the query. When the variable is set to zero, all statements after the first semicolon are ignored.

How do I run multiple queries in SQL Workbench?

How to run Multiple SQL Queries in MySQL Workbench explains how you can run multiple statements in single query window by separating them with semicolon ; You can have different types of statements such as delete, update or insert in single query window and all can be executed with single click.

How do you run multiple SQL statements in snowflakes?

The Snowflake stored procedure below will: Accept a string parameter that is a SQL statement designed to generate rows of SQL statements to execute. Execute the input SQL statement to generate a list of SQL statements to run. Run all statements identified by the “SQL_COMMAND” column one at a time.

What is a multiple query in SQL?

What are SQL multiple joins? Multiple joins can be described as follows; multiple join is a query that contains the same or different join types, which are used more than once. Thus, we gain the ability to combine multiple tables of data in order to overcome relational database issues.


2 Answers

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 183
Adrian J. Moreno Avatar answered Oct 19 '22 21:10

Adrian J. Moreno


In case anyone is finding this and putting the allowMultiQueries=true in the connection box is not working, I finally tried appending it to the actual JDBC url like this jdbc:mysql://127.0.0.1:3306/mydbname?allowMultiQueries=true. It worked on the first try afterwards.

like image 1
Djcard Avatar answered Oct 19 '22 22:10

Djcard