zoukankan      html  css  js  c++  java
  • Identity相關知識

    对于SQL Server 2000来说,它提供了两个全新的函数(IDENT_CURRENT,SCOPE_IDENTITY),并且改进了@@IDENTITY的不足.当你插入新记录后,可以调用函数:
    PRINT IDENT_CURRENT('table') '这将获得新的IDENTITY值,不管数据库中是不是有记录添加(这就避免了@@IDENTITY的连接限制)
    或者:PRINT SCOPE_IDENTITY() '这将获得在当前存储过程,触发器等其它程序创建的最新记录的IDENTITY值.
    而全局变量@@IDENTITY有一个问题,当对一张表执行insert时,如果该表有触发器程序在执行插入操作,然后,接着在另一张表中插入记录,这样返回@@IDENTITY值就是第二张表的IDENTITY值。
    如果你用的不是SQL Server 2000,你最好一个简单的存储过程来解决这个问题。
    use master
    go
    if object_id('identity_t1') is not null
    begin
    drop table identity_t1
    end
    if object_id('identity_t2') is not null
    begin
    drop table identity_t2
    end
    create table identity_t1(c1 int identity,c2 datetime default(getdate()))
    create table identity_t2(c1 int identity,c2 datetime default(getdate()))
    insert into identity_t1 values (getdate())
    insert into identity_t2 values (getdate())
    insert into identity_t2 values (getdate())
    select @@identity as identity_value_for_all_tables
    select scope_identity() as scope_identity_value_for_all_tables
    select ident_current('identity_t1') as ident_current_value_for_identity_t1
    go
    if object_id('identity_t1') is not null
    begin
    drop table identity_t1
    end
    if object_id('identity_t2') is not null
    begin
    drop table identity_t2
    end
    ----注:IDENT_CURRENT('table') 是全局变量,如果Insert过程太长时间,有可能就会拿了别人-----Insert这个Table的ID,而SCOPE_IDENTITY()是同一进程内取得的值。最确保正确的办法:----select @@IDENTITY 和 Select SCOPE_IDENTITY()或 Select IDENT_CURRENT('table')相比较。

  • 相关阅读:
    Swap Nodes in Pairs
    Remove Nth Node From End of List
    Rotate list
    历届试题_DNA比对
    斐波那契字符串_KMP
    字符串的模式匹配
    理解图像Garbor和HOG特征的提取方法及实例应用
    人眼定位和基于人眼的人脸姿态矫正_转载
    借助百度云API进行人脸识别
    { "result": null, "log_id": 304592860300941982, "error_msg": "image check fail", "cached": 0, "error_code": 222203, "timestamp": 1556030094 }
  • 原文地址:https://www.cnblogs.com/guyuehuanhuan/p/1942281.html
Copyright © 2011-2022 走看看