Could not find any solution how to use the last inserted ID of a certain table and use this ID to select another field of the same table.
I tried this but always get syntax errors:
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE FUNCTION dbo.sfLastCode ()
AS
BEGIN
SET NOCOUNT ON;
-- Declare the return variable here
declare @LastCode nvarchar(2)
-- Add the T-SQL statements to compute the return value here
@LastCode=select tblevents.CODE from tblEvents where Evid=(select IDENT_CURRENT('dbo.tblEvents'))
returns @LastCode
GO
For example I would like to get as aresult the code '123' form the record with the ID 90 . For sure the syntax is wrong at all, but I do not know where and what. I am not sure if this should be done in a SP or in a scalar function. I start the code form Access by a PT-query.
EDIT: Meanwhile I tried
select ident_Current('dbo.tblEvents') as LastEvID
and got a STRANGE result, because the number is 96, although the highest EvID is 90. 6 records have not been saved due to tests and errors in the code. Why does IDENT_Current('dbo.tblEvents') does not give me number 90?
As I read Max(EvID) is not the best choice I tried IDENT_Current('dbo.tblEvents'), but when IDENT_Current also counts not saved records it is of no use for me.
Thanks a lot Michael