简体   繁体   English

SQL Server身份问题

[英]SQL Server identity issue

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. 执行后,它在我的参数中返回null。

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. 这里的主要问题是“IMDECONP38” - 我使用的服务器名称。 If I remove this I can get the value of identity in my parameter. 如果我删除它,我可以在我的参数中获取身份的值。

See this old question for a similar problem: You cannot retrieve a scoped variable like SCOPE_IDENTITY() from another server. 针对类似问题,请参阅此旧问题:您无法从其他服务器检索SCOPE_IDENTITY()等范围变量。 Instead, you should use a stored procedure on the remote server to achieve this. 相反,您应该使用远程服务器上的存储过程来实现此目的。

When you use "IMDECONP38" then you break SCOPE_IDENTITY because 当你使用“IMDECONP38”然后你打破SCOPE_IDENTITY因为

  • the INSERT scope is now on the IMDECONP38 linked server INSERT范围现在位于IMDECONP38链接服务器上
  • SCOPE_IDENTITY runs on the local server, not IMDECONP38 SCOPE_IDENTITY在本地服务器上运行,而不是在IMDECONP38上运行

If on SQL Server 2005, try the OUTPUT clause but I'm not sure how it works for a linked server call 如果在SQL Server 2005上,请尝试OUTPUT子句,但我不确定它如何用于链接服务器调用

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 编辑:Prutswonder首先说:在链接服务器上使用存储过程

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

For sql server 2012, to get the name of the identity column 对于sql server 2012,获取标识列的名称

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: 如果您有Linked服务器,可以使用:

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'
select @@IDENTITY AS 'variablename'

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. 当您使用SCOPE_IDENTITY()获取最后插入的标识字段时,这将导致问题。

HTH HTH

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM