Coldfusion blog - by phil porter
   

fearlessFusion.com is my Blog and Online portfolio; I am currently the Director of Visualweb Ltd. Read More


Multiple SQL inserts: Oracle, Mysql, Mssql

Found a useful technique for inserting multiple rows into Oracle, MYSQL and MS SQL databases.

Mysql supports this:
INSERT INTO tblName (column1,column2)
VALUES
('Value1','Fernando'),
('Value2','Carlos'),
('Value3','Vincent')
Oracle does not, but can be achieved with:
INSERT INTO tblName (column1,column2)
select ('Value1','Fernando') from dual
union all
select ('Value2','Carlos') from dual
union all
select ('Value3','Vincent') from dual

DUAL is a table which is created by oracle along with the data dictionary. It consists of exactly one column whose name is dummy and one record.

In MS SQL the union all technique also works:
INSERT INTO [TABLE] (column1,column2)
select ('Value1','Fernando')
UNION ALL
select ('Value2','Carlos')
UNION ALL
select ('Value3','Vincent')

This technique is probably a lot faster than using multiple inserts in coldfusion and certainly helps reduce the round-trips that would need to be done.



Comments
Anon's Gravatar
# Posted By Anon | 10/12/07 10:27

Thanks for that - really helped save my pages from timing out

 


JD's Gravatar
# Posted By JD | 15/04/08 14:01

I found with MSSQL, I couldn't use parentheses in my select statements for insert. So, from your example, I had to do: select 'value1','Fernando' union all select 'value2','Carlos' etc. Thanks for the reference though!

 


JAYESH TALSANIYA's Gravatar
# Posted By JAYESH TALSANIYA | 08/05/08 20:00

in mssql u cannot use parentheses in select statement. use as below: INSERT INTO [TABLE] (column1,column2) select 'Value1','Fernando' UNION ALL select 'Value2','Carlos' UNION ALL select 'Value3','Vincent'

 



Sponsored Links

 

 




Calendar

« May 2008 »
M T W T F S S
      1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30 31  


Recent Comments

Multiple SQL inserts: Oracle, Mysql, Mssql

JAYESH TALSANIYA said: in mssql u cannot use parentheses in select statement. use as below: INSERT INTO [TABLE] (column1,... [More]

Multiple SQL inserts: Oracle, Mysql, Mssql

JD said: I found with MSSQL, I couldn't use parentheses in my select statements for insert. So, from your ex... [More]

BlogCFC ping list and timeouts

Will Wilson said: Nice :) that will come in handy, thanks! [More]

Is this a Coldfusion 8 BUG

Barb said: I'm having the same problem . . . when I start CF8, it seems to hang during or just after the user M... [More]

Is this a Coldfusion 8 BUG

João Fernandes said: Phil, when you install CF8 they give you an option to install LCDS within CF. The messageBrokerServl... [More]





W3C Design Gallery

Richard Deverell, illustrator Illustrations & Web Design
CROSSING TV CSSaddict
Charlotte Cann Interiors Rastercafe