11

I have a query like below

declare @str_CustomerID int
Insert into IMDECONP38.[Customer].dbo.CustomerMaster
( CustomerName , CustomerAddress , CustomerEmail , CustomerPhone )
values ( ‘werw12e’ , ‘jkj12kj’ , ‘3212423sdf’ , ‘1212121′
)

select @str_CustomerID= scope_identity()

After execution it returns null in my parameter.

I want to get the value of identity. How can I do that?

The main issue over here is "IMDECONP38" - the server name that I used. If I remove this I can get the value of identity in my parameter.

Peter Mortensen
  • 30,030
  • 21
  • 100
  • 124
Pranay Rana
  • 170,430
  • 35
  • 234
  • 261

6 Answers6

9

See this old question for a similar problem: You cannot retrieve a scoped variable like SCOPE_IDENTITY() from another server. Instead, you should use a stored procedure on the remote server to achieve this.

Community
  • 1
  • 1
Prutswonder
  • 9,476
  • 3
  • 26
  • 38
5

When you use "IMDECONP38" then you break SCOPE_IDENTITY because

  • the INSERT scope is now on the IMDECONP38 linked server
  • SCOPE_IDENTITY runs on the local server, not IMDECONP38

If on SQL Server 2005, try the OUTPUT clause but I'm not sure how it works for a linked server call

Insert into IMDECONP38.[Customer].dbo.CustomerMaster
OUTPUT INSERTED.ID   --change as needed
( CustomerName , CustomerAddress , CustomerEmail , CustomerPhone )
values ( ‘werw12e’ , ‘jkj12kj’ , ‘3212423sdf’ , ‘1212121′
)

Edit: Prutswonder said it first: use a stored proc on the linked server

gbn
  • 408,740
  • 77
  • 567
  • 659
  • 1
    Sql server giving error : Remote tables are not allowed to be the target of a DML statement with an OUTPUT clause, or the target of the INTO clause. – Pranay Rana Apr 12 '10 at 12:24
3

Use a stored procedure in the remote database.

CREATE PROCEDURE InsertCustomer (@name varchar(100), @address varchar(100), 
    @email varchar(100), @phone varchar(100), @id int OUT)
AS
    INSERT INTO dbo.CustomerMaster 
    (CustomerName , CustomerAddress , CustomerEmail , CustomerPhone ) 
    VALUES (@name, @address, @email, @phone)

    SET @id = SCOPE_IDENTITY()
GO

DECLARE @id int
EXEC IMDECONP38.Customer.dbo.InsertCustomer 'Fred','Bedrock','a@b','5',@id OUT
GO
Anthony Faull
  • 16,953
  • 5
  • 53
  • 71
0

For sql server 2012, to get the name of the identity column

select col_name(sys.all_objects.object_id, column_id) as id from    sys.identity_columns 
join sys.all_objects on sys.identity_columns.object_id = sys.all_objects.object_id
where sys.all_objects.object_id = object_id('system_files')

If you have a Linked server you can use this:

select iden.name as id 
from [LINKED_SERVER_NAME].[DATABASE_NAME].sys.identity_columns as iden
join [LINKED_SERVER_NAME].[DATABASE_NAME].sys.all_objects allobj
    on iden.object_id = allobj.object_id
where allobj.name = 'TABLE NAME HERE'
Tschallacka
  • 26,440
  • 12
  • 87
  • 129
-2
select @@IDENTITY AS 'variablename'
gbn
  • 408,740
  • 77
  • 567
  • 659
David Fox
  • 10,382
  • 8
  • 47
  • 76
-2

Please check if there are any triggers set for your table. This will cause a problem when you use SCOPE_IDENTITY() to get the last inserted identity field.

HTH

Pranay Rana
  • 170,430
  • 35
  • 234
  • 261
Raja
  • 3,568
  • 3
  • 26
  • 39
  • SET NOCOUNT ON does not affect SCOPE_IDENTITY. Or do you have an MSDN reference to back this up. As my question notes, only DataAdaptors can be affected by SET NOCOUNT ON here http://stackoverflow.com/questions/1483732/set-nocount-on-usage – gbn Apr 12 '10 at 12:22
  • @gbn: I had this same problem and SET NoCount OFF did the trick for me. Anyways I have taken that out of my answer :-). – Raja Apr 12 '10 at 12:39
  • 1
    Triggers affect the value of @@Identity not scope_identity() – HLGEM Apr 12 '10 at 13:51