sql - How can I stop a mysql insert statement from executing when a select statement returns nothing? -


consider select statement:

    select course_id courses          course_id = (       select course_id courses        course_id = 3       , max_size > students_registered); 
  • the values inserted not coming select. submitted form user. select tells me if there slot available make insert.

currently check on front end 1 ajax call, i'm trying move away front end. suggestions appreciated.

  • the insert statement inserts 8 values table. nothing extraordinary value question. i've researched extensively. option moving stored procedure?

  • i need insert fail when select returns no rows or null can return right view/jsp application.

please let me know details provide help.

you can use insert ... select judiciously control this.

for example,

insert mytable (course_id, col2, col3) select a.course_id col1, 'value' col2, 'another_value' col3   courses  course_id = 3    , max_size > students_registered  limit 1 

the trick write select query returns values want insert, , returns either 1 row or none depending on conditions. write query , debug it. make sure works.

then put insert clause right before it.

if select comes no rows, insert insert nothing. if select comes 1 row, that's inserted.

this great way this; don't have worry race conditions or transactions.

edit. notice select statement contains mixture of values derived table , constant values. if don't use values table, this:

 select ?param1 col1, ?param2 col2, ?param3 col3 

you did't mention host language, ?param items above guess @ appropriate placeholders data coming form.

this form of select still want; if where clause returns no rows nothing inserted.


Comments

Popular posts from this blog

Delphi XE2 Indy10 udp client-server interchange using SendBuffer-ReceiveBuffer -

Qt ActiveX WMI QAxBase::dynamicCallHelper: ItemIndex(int): No such property in -

Enable autocomplete or intellisense in Atom editor for PHP -