Published on InterSystems Developer Community (https://community.intersystems.com)

Home > Getting SCOPE_IDENTITY() from SQL SERVER

Question
Yakov Berger · Nov 21, 2021

Getting SCOPE_IDENTITY() from SQL SERVER

Hi,

In a Business Operation i have the following code.

I need the identity of the latest row inserted in a table in MSSQL.

i Have the following code:

---------------------------------------------------------

set insertmaster= "INSERT INTO dbo.dataMasterArchive(deviceToPatientLogID) values (?)"

set tSC = ..Adapter.ExecuteUpdate(.nrows1,insertmaster,pRequest.deviceToPatientLogID)                                
$$$ThrowOnError(tSC)

set sql ="SELECT SCOPE_IDENTITY()"
set tSC = ..Adapter.ExecuteQuery(.tResult,sql)
$$$ThrowOnError(tSC)

set maxID = tResult.Get(1)

------------------------------------------------------------

the result of maxID  after the above is NULL.

what am i doing wrong?.

 

Thanks

#InterSystems IRIS
Product version: IRIS 2019.1

Source URL:https://community.intersystems.com/post/getting-scopeidentity-sql-server