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.



Oct 07
25

SQL SELECT TOP N equivalent in ORACLE and MySQL

Something I needed today... I wish this was standarized, but it is not, so here are some examples:

SQL Server:
SELECT TOP 10 product, descr, email
FROM products
ORACLE:
SELECT product, descr, email
FROM products
WHERE ROWNUM <= 10
MySQL:
SELECT product, descr, email
FROM products
LIMIT 10
 
.......More »

Feb 07
05

SQL - getting table info

Sometimes I've had the need to use SQL to go and get table information i.e is a field varchar or int etc.

For MS SQL server you can use the following to get info on a table

SELECT RIGHT(syscolumns.name, LEN(syscolumns.name)) AS dbFieldName, systypes.name AS typename, syscolumns.length AS maxlength, syscolumns.xprec AS [precision], syscolumns.xscale AS scale, syscolumns.isnullable AS isnullable
FROM syscolumns INNER JOIN
sysobjects ON sysobjects.id = syscolumns.id INNER JOIN
systypes ON syscolumns.xtype = systypes.xtype
WHERE (sysobjects.name = THE_TABLE_NAME)
ORDER BY syscolumns.colorder
 
.......More »

Oct 06
27

SQL - The case for CASE

The Transact SQL Case statement is a particularly useful piece of syntax to know as it can be used to solve many potentially tricky SQL Server programming tasks without resorting to more computationally intensive procedures such as using cursors, temporary tables or processing of the results set in a programming. One such use is to create a column in a results set with differing values depending on the value of a particular column. For example, the pubs SQL Server sample database contains a publishers table. In this table is a country column which specifies the name of the country in which the publisher is based. If you wanted to write a SQL query to return the details of the publishers plus a shipping column that would specify whether the publisher was based in the USA or overseas then the following query could be used:
select *,
case when country = 'USA' then 'Domestic' else 'Overseas' end Shipping
from publishers
order by Shipping
This query will return a value of Domestic in the Shipping column for publishers based in the USA, or Overseas for all the non-USA based publishers. The Case statement can also be used to identify rows containing null values for particular columns. For example, the following query will show which rows contain a null value for the country column:
select *,
case when country is null then 'Yes' else 'No' end MissingCountry
from publishers
It is also possible to specify multiple When statements:
select *,
case when country = 'USA' then 'Domestic'
when country is null then 'Unknown'
else 'Overseas' end Shipping
from publishers
order by Shipping
The Case statement can also be used to specify the selection of data from different columns depending on the value of other columns. In the SQL statement below, the Address column will contain the city and state details for USA publishers, but for overseas publishers the Address column will contain the city followed by the country:
select *,
case when country = 'USA' then city + ', ' + state
when country is null then 'Unknown Address'
else city + ', ' + country end Address
from publishers
 
.......More »


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

Illustrations & Web Design CROSSING TV
CSSaddict Charlotte Cann Interiors
Rastercafe Skis Direct