Here's another
ColdFusion 8 goodie that I just learned about yesterday ...
If you are using a database table with an identity (auto-increment) field, ColdFusion 8 can automatically return the newly created field value for you, without needing a trigger or a subsequent <CFQUERY>. All you need to do is look in the <CFQUERY> optional RESULT structure, and if available the value will just be there. This feature is very DBMS and driver specific (and unfortunately does not seem to be supported by Apache Derby), and the name of the structure member containing the generated value is different based on the DBMS being used. Here are the ones you should be aware of:
- SQL Server: result.IDENTITYCOL
- Oracle: result.ROWID
- Sybase: result.SYB_IDENTITY
- Informix: result.SERIAL_COL
- DB2: result.KEY_VALUE
- MySQL: result.GENERATED_KEY (MySQL 4 and 5 only, MySQL 3 does not support this feature)
Very nice indeed!
--- Ben
<cfquery name="qCreate" datasource="#getDatasource().getName()#">
INSERT INTO
Person(firstName, lastName, dob)
VALUES(
<cfqueryparam cfsqltype="cf_sql_varchar" value="#firstName#" />,
<cfqueryparam cfsqltype="cf_sql_varchar" value="#lastName#" />,
<cfqueryparam cfsqltype="cf_sql_datetime" value="#dob#" />
)
SELECT
SCOPE_IDENTITY() AS personId
</cfquery>
<cfscript>
personId = qCreate.personId;
</cfscript>
--- Ben
INSERT INTO test
(val1, val2, val3, val4)
(SELECT 'a1', 'a2', 'a3', 'a4')
UNION
(SELECT 'b1', 'b2', 'b3', 'b4')
UNION
(SELECT 'c1', 'c2', 'c3', 'c4')
This is cool, but.....
Has anyone mentioned the possibility of returning a newID() (mssql).
I am just saving it now before inserting it, but this would be way easier.
DECLARE newUUID CHAR(36) OUT
SET newUUID=newID()
INSERT INTO table(uuid, name,...)
VALUES(newUUID, @name,...)
You insert guid manually to tables?
It's easy to do:
insert("-", CreateUUID(), 23)
will create a compatable GUID for MSSQL
@jim
If you're use MSSQL: NEVER depend on @@Identity. Use SCOPE_IDENTITY().
That's why I was wondering if it used @@IDENTITY versus SCOPE_IDENTITY(). @@IDENTITY does not guarantee that the value returned is the last id generated from your transaction, where SCOPE_IDENTITY() does.
Cheers..
but what about an access *.mdb odbc connection?
will it return the newly created primary key?
thanks
Tell me if I'm wrong here, but as far as I know, MSAccess doesn't have support for this. In the pass the only way I knew how to get an identity from a table was to use two queries wrapped inside a cftransaction tag. One would do the insert while the other would do a MAX(PK) afterwards. Now being the DB savvy guy that I am (stop laughing), I know this isn't a guaranteed method. There are some that will claim that setting the isolation level on the driver to serializable would do the trick, but it never sat right with me.
Anywho. My suggestion is always this: If you're using MSAccess as a database, I see no reason why you couldn't just use the express version of MSSQL 2005 or MSDE.
I think that CF just adds the @@identity line to the code, what if it's allreadt there?
<cfquery name="qryIns" dsn="some_dsn">
INSERT INTO tbl_test (name)
VALUES ('my name');
SELECT @@identity as pkey
</cfquery>
<cfoutput>The primary Key is: #qryIns.pkey#</cfoutput>
Jorrit, no, no conflict. But that code is unneeded in CF8.
--- Ben
It's not that one is better than the other, they just do different things. Admittedly, yes, if you have an plain insert statement in a cfquery, you probably want to retrieve the identity without worrying of whatever trigger is out there, so scope_identity() is a better fit.
Tof
FWIW, I'm using the embedded version, and the column definition for the table in which I'm inserting a record defines the primary key as "generated always as identity".
I'm just offering this in case others (like me) find the entry and wonder about Derby.
when u passs the cfquery a sql var,the var has the ' char,the cfquery will convert to the ' to '' auto,why?
for example:
<code>
<cfset sql="select * from some_table where char_col='1'"/>
<cfquery name="testQuery" datasource="someDs">
#sql#
</cfquery>
</code>
the sql pass to db's will be: select * from some_table where char_col=''1'',so throws a db exception's
but if u code the sql inner cfquery the every work ok:
<cfquery name="testQuery" datasource="someDs">
select * from some_table where char_col='1'
</cfquery>
is a bug of coldfusion 8 cfquery?!
please help me!thanks!
I am also having an issue with CF8 and exisiting triggers..
my triggers are usualy select scope_identity() or identity@@ as 'identity' but in CF8 it is throwing an error saying undefined. confirmed the same query works in cfmx 7
Thank you
http://www.forta.com/books/032151548X/" target="_blank">http://www.forta.com/books/032151548X/
BTW, Ben, curiously it's not listed in the main page content of your books page (http://www.forta.com/books/), though it is in the left nav bar. Perhaps an oversight, just not updated since the book shipped?
result_name.SQLPARAMETERS[index of pk]
Jorritt wrote:
I was just wondering, does this conflict with the code I am using for a long time?
I think that CF just adds the @@identity line to the code, what if it's allreadt there?
<cfquery name="qryIns" dsn="some_dsn">
INSERT INTO tbl_test (name)
VALUES ('my name');
SELECT @@identity as pkey
</cfquery>
<cfoutput>The primary Key is: #qryIns.pkey#</cfoutput>
CF8 with SQl Server 2005 and JDBC 1.2. Doing an insert query as follows fails to work as advertised with [result].IDENTITYCOL. I have to use [result].GENERATED_KEYS instead. I use the code below as a workaround until it is fixed.
<!--- Create a record entry in the Database table diaDocuments and return the key that was added. --->
<cfquery name="qAppenddiadocument" datasource="DocumentImaging" result="diaresult">
insert into diadocument
(diadocument.diadocno, diadocument.diaentrydatetime, diadocument.dianoofpages, diadocument.diasuperdoctypekey)
values
(#lndiadocno#, #ldnow#, #lndianoofpages#, #lndiasuperdoctypekey#)
</cfquery>
<!--- Return the key field from the new DB Table record. This is a CF8 feature.
However there is a bug currently with SQL Server 2005, JDBC 1.2 and CF8 that I code around here.--->
<cfif IsDefined("diaresult.IDENTITYCOL") >
<CFSET lndiadocumentkey = #diaresult.IDENTITYCOL# />
<cfelse>
<CFSET lndiadocumentkey = #diaresult.GENERATED_KEYS# />
</cfif>
I tried unsuccessfully to get the Return Identity Values, the way you show us! All I get is:”KEY_VALUE” is not defined …”
Would you provide a valid example for Coldfusion 8 and MySQL? Thank you!
<CFQUERY NAME="qInsert" DATASOURCE="verb">
INSERT INTO club
(firstname, lastname)
VALUES
('#formData.firstname#', '#formData.lastname #')
</CFQUERY>
Silviu
http://www.simpy.com/user/winbill
http://www.diigo.com/user/lullabot
<cfquery datasource="#APPLICATION.dsn#" name="update_my_db" result="my_result">
INSERT INTO tbl_foo
(some_field_name)
VALUES (some_field_value)
</cfquery>
<cfoutput>My New Primary KeyID = #my_result.GENERATED_KEY#</cfoutput>