This does not seem to work with an UPDATE statement. You are right
that @@IDENTITY can be used in the VALUES clause of an INSERT query,
but when I try it in an UPDATE statement, I get the same syntax
error as the OP.
Bob
Michel Walsh wrote:
Hi,
Should be a matter of where you used it. If you used it in the query
designer, it won't work, if you use it with DAO, it won't work. You
have to use ADO and the same connection you used to append the data.
The following work, in Access, in the immediate (debug) window
(Access 2000, or more recent, Access the application, maintains a
permanent ADO connection to the database, Jet or MS SQL Server,
called CurrentProject.Connection) :
===========
CurrentProject.Connection.Execute "CREATE TABLE demo( f1 COUNTER, f2
varchar(50));"
CurrentProject.Connection.Execute "INSERT INTO demo (f2) VALUES (
'aaa') ; " CurrentProject.Connection.Execute "INSERT INTO demo (f2)
VALUES ( @@Identity ) ; "
? CurrentProject.COnnection.Execute("SELECT * FROM
demo").GetString() 1 aaa
2 1
===========
Hoping it may help,
Vanderghast, Access MVP
It didn't work.
UPDATE tblBlogs SET link_ID = blog_ID
WHERE blog_ID = @@IDENTITY
I get this error when trying to save an Access query:
syntax error (missing operator) in
query expression blog_ID = @@IDENTITY
Note: blog_ID is an autonumber key for tblBlogs and link_ID is a
Long Integer field in the same table. The Access 2002 database had
SQL Server Compatible Syntax (ANSI 92) checked in Options.
The INSERT statement does worked OK. So there was an @@IDENTITY
created in that session.
========== ========== ==========
ASP Code:
========== ========== ==========
Set conn = getAdoConn("Blog")
sSQL = "INSERT INTO tblBlogs(title, body, display, author_ID) " &_
"VALUES('" & sTitle & "','" & sBody & "'," &_
bDisplay & "," & iAuthor & ")"
doAdoSqlCmd conn, sSQL
sSQL = "UPDATE tblBlogs SET link_ID = blog_ID " &_
"WHERE blog_ID = @@IDENTITY"
doAdoSqlCmd conn, sSQL
KillConn conn
Function getAdoConn(db)
Dim conn
Set conn = Server.CreateObject("ADODB.Connection")
conn.ConnectionTimeout = 15
conn.CommandTimeout = 30
conn.Open "Provider=Microsoft.Jet.OLEDB.4.0; Data Source=" &_
Server.MapPath("..\database\" & db & ".mdb;") &_
"User ID=Admin;Password=;"
Set getAdoConn = conn
End Function
Sub doAdoSqlCmd(conn, sqlcmd)
Dim cmd
Set cmd = Server.CreateObject("ADODB.Command")
Set cmd.ActiveConnection = conn
cmd.CommandType = 1
cmd.CommandText = sqlcmd
cmd.Execute
Set cmd = Nothing
End Sub
Sub KillConn(cn)
cn.Close
Set cn = Nothing
End Sub
========== ========== ==========
End Code.
========== ========== ==========
On Fri, 12 Sep 2003 06:58:54 -0400, "Michel Walsh"
Hi,
You don't need to "select" it. If you use ADO ( not DAO, not
the query designer), the following should work:
CurrentProject.Connection.Execute "UPDATE tableName SET f1 = f2
WHERE f3=@@IDENTITY"
Note that @@IDENTITY is maintained by connection. You have to
use the same connection object that you did something to "assign"
the @@IDENTITY to something, that to get it back. As a trivial
example, but worth of mention, @@IDENTITY of the
currentProject.Connection won't be aware of the latest DAO
operations.
Hoping it may help,
Vanderghast, Access MVP
Is it possible to use a SELECT @@IDENTITY statement as a
sub-query in an UPDATE ?
For instance I wanted to run the following query but Access would
not allow me to enter it:
I've tried each of the following:
UPDATE tblBlogs SET link_ID = blog_ID
WHERE blog_ID=(SELECT @@IDENTITY AS iBlogID);
UPDATE tblBlogs SET tblBlogs.link_ID = tblBlogs.blog_ID
WHERE tblBlogs.blog_ID = (SELECT @@IDENTITY);
UPDATE tblBlogs SET tblBlogs.link_ID = tblBlogs.blog_ID
WHERE tblBlogs.blog_ID = (SELECT @@IDENTITY AS iBlogID);
UPDATE tblBlogs SET tblBlogs.link_ID = tblBlogs.blog_ID
WHERE tblBlogs.blog_ID = SOME (SELECT @@IDENTITY AS
iBlogID);
UPDATE tblBlogs SET tblBlogs.link_ID = tblBlogs.blog_ID
WHERE tblBlogs.blog_ID = ANY (SELECT @@IDENTITY);
Why is it not possible to do this?
The following does work:
UPDATE tblBlogs SET tblBlogs.link_ID = tblBlogs.blog_ID
WHERE tblBlogs.blog_ID = (SELECT MAX(blog_ID) FROM
tblBlogs);