Today I learned that there is a way to obtain the auto generated ID when a record is INSERTed into SQL Server. The following code executes in the SQL Server Management Studio:
Which has output of:
I am finding many examples of how to use ADODB.Parameter objects with stored procedures, however I am not finding an example of how to use OUTPUT with an INSERT statement.
I have seen examples that show accessing the ADODB.Command object to deal with returned values from Stored Procedires. One had to name the Parameter(s) within the Stored Procedure, and that is the name you access them via after you execute the Stored Procedure.
However one does not define special names for the OUTPUT command of a SQL INSERT statement. I have tried the column name with and without leading @ character. Neither of those attempts worked.
So what is the correct Rx to using OUTPUT Paramaters with non Stored Procedure SQL code? Thanks!
Code:
INSERT INTO dbo.auth (authid, logtimestamp, active, userid, username, permmask)
OUTPUT Inserted.id
VALUES (1, CURRENT_TIMESTAMP, 1, 'foo', 'Foo User', 31);
Code:
id
12
I have seen examples that show accessing the ADODB.Command object to deal with returned values from Stored Procedires. One had to name the Parameter(s) within the Stored Procedure, and that is the name you access them via after you execute the Stored Procedure.
However one does not define special names for the OUTPUT command of a SQL INSERT statement. I have tried the column name with and without leading @ character. Neither of those attempts worked.
So what is the correct Rx to using OUTPUT Paramaters with non Stored Procedure SQL code? Thanks!