如何使用Scope_Identity()获取字符串键 [英] How to use Scope_Identity() to get string key

查看:185
本文介绍了如何使用Scope_Identity()获取字符串键的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

仅当表具有int键值时Scope_Identity()才有效.如果表具有字符串键,我该如何使用它来获取上次更新/添加的键.

Is Scope_Identity() only valid if table has int key value. If table has string key how can I use this to take last updated/added key.

推荐答案

Scope_Identity仅适用于数据库中的标识字段.我认为您对主要字段和身份字段感到困惑. Identity字段仅由相关的RDMS以其自己的特定方式更新(大多数实现AutoNumber字段,某些实现GUID字段),而Primary键可以是任何数据类型,并且通常由开发人员/存储的proc等进行更新.字段也可以是一个Identity字段,不一定要这样.因此,Scope_Identity仅适用于身份"列.
Scope_Identity only works on identity fields in a database. I think you are being confused with Primary Fields and Identity Fields. An Identity field is only updated by the pertaining RDMS in their own particular fashion(most implement AutoNumber fields, some implement GUID fields), while Primary keys can be any datatype, and are usually updated by the developer/stored proc etc. Whilst a Primary field can also be an Identity field, it is not necessarily so. Scope_Identity will therefore only work on Identity columns.



试试这个
INSERT INTO mySchoolRecord(StudentId,Name,[Address],Contact)OUTPUT INSERTED.StudentId VALUES(``abcdesfg'',``彼得先生'',``美国'',256)

如果您使用的是Guid,请尝试以下

DECLARE @ClientID uniqueidentifier
DECLARE @MyTableVar表(ClientID uniqueidentifier)

插入客户端(客户端名称,地址1,地址2)
输出已插入.ClientID进入@MyTableVar
值("AAAA",街道",地点")
选择@ClientID = ClientID FROM @MyTableVar

try this
INSERT INTO mySchoolRecord (StudentId,Name,[Address],Contact) OUTPUT INSERTED.StudentId VALUES (''abcdesfg'',''Mr. Peter'',''United States'', 256 )

if your are using Guid then try this

DECLARE @ClientID uniqueidentifier
DECLARE @MyTableVar table ( ClientID uniqueidentifier )

INSERT INTO Client (ClientName, Address1, Address2)
OUTPUT INSERTED.ClientID INTO @MyTableVar
VALUES (''AAAA'', ''Street'', ''place'')
SELECT @ClientID = ClientID FROM @MyTableVar


这篇关于如何使用Scope_Identity()获取字符串键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆