select @@Identity 返回自動遞增字段的值


select @@Identity 返回自動遞增字段的值。

1. IDENTITY 列不能由用戶直接更新,它是由系統自動維護的。 

2.該列數據類型必須為數值型:int, smallint, tinyint, decimal or numeric with scale 0。 

3.該列不能為 null。 

4.不能在該列上設置缺省值。 

5.遞增量只能為整形(比如:1,2,-3)。不能為小數,也不能為0。 

6.基值(種子值 seed)可以由用戶設置,缺省值為1。 

理解 @@IDENTITY 

@@IDENTITY 返回最后一個插入 IDENTITY 的值,這些操作包括:INSERT, SELECT INTO,或者 bulk copy。如果在給沒有 IDENTITY 列的其他表插入記錄,系統將其置為 null。如果有多行記錄插入到 IDENTITY 表中,@@IDENTITY 表示最后一個產生的值。如果觸發了某個觸發器,並且這個觸發器執行向另一個帶有 IDENTITY 列的表的插入操作,@@IDENTITY 將返回這個由觸發器產生的值。如果這個觸發器插入的表中不包含 IDENTITY 列,那么 @@IDENTITY 將為 null。如果插入操作失敗,@@IDENTITY 值依然會增加,所以 IDENTITY 不保證數據的連續性。 

@@IDENTITY 是當前連接的全局變量,只對當前連接有效。也就是說,如果斷開連接再重新連接后,@@IDENTITY 為 null。以 ADO 來說,@@IDENTITY 在 Connection 對象打開和關閉期間是有意義的,即在 Connection 對象的存在范圍內有效。在 MTS 組件中,從打開連接到顯式的關閉連接(Connection.Close)或者到調用了 SetAbort,SetComplete之前,在這期間,@@IDENTITY 有意義。 

使用 Truncate table 語句會使 IDENTITY 列重新開始計算。 

得到 @@IDENTITY 的值 

有三種方法(以下代碼均使用 VBScript) 

方法一: 

Dim Conn, strSQL, Rs 
Set Conn = CreateObject("ADODB.Connection") 
' Open a connection to the database 
Conn.Open("DSN=myDSN;UID=myUID;PWD=myPWD;") 

' Insert a new record into the table 
strSQL = "INSERT INTO mtTable (columnName) VALUES ('something')" 

' Execute the SQL statement 
Conn.Execute(strSQL) 

' Get the @@IDENTITY. 
strSQL = "SELECT @@IDENTITY AS NewID" 
Set Rs = Conn.Execute(lsSQL) 
NewID = Rs.Fields("NewID").value 

' Close the connection 
Conn.Close() 
Set Conn = Nothing 

方法二(僅限於 ADO 2.0 以上): 

Dim Conn, strSQL, Rs 
Set Conn = CreateObject("ADODB.Connection") 
' Open a connection to the database 
Conn.Open("DSN=myDSN;UID=myUID;PWD=myPWD;") 

' Insert a new record into the table 
lsSQL = "INSERT INTO myTable (columnName) VALUES ('something');" &_ 
"SELECT @@IDENTITY AS NewID;" 

' Execute the SQL statement 
Set Rs = Conn.Execute(lsSQL) 

' Get the second resultset into a RecordSet object 
Set Rs = Rs.NextRecordSet() 

' Get the inserted ID 
NewID = Rs.Fields("NewID").value 

' Close the connection 
Conn.Close() 
Set Conn = Nothing 

方法三: 

Dim Conn, strSQL, Rs 
Set Conn = CreateObject("ADODB.Connection") 
' Open a connection to the database 
Conn.Open("DSN=myDSN;UID=myUID;PWD=myPWD;") 

' Insert a new record into the table 
strSQL = "SET NOCOUNT ON;" &_ 
"INSERT INTO myTable (columnName) VALUES ('something');" &_ 
"SELECT @@IDENTITY AS NewID;" 

' Execute the SQL statement 
Set Rs = Conn.Execute(lsSQL) 

' Get the inserted ID 
NewID = Rs.Fields("NewID").value 

' Close the connection 
Conn.Close() 
Set Conn = Nothing  
 
 
使用C#書寫的函數
/// <summary>
        /// 更新記錄並返回影響新第一行第一列ID
        /// </summary>
        /// <param name="sSQL"></param>
        /// <returns></returns>
        public int UpdateDbBySQLRetID(string sSQL)
        {
            int iRet = 0;
            using (SqlConnection conn = new SqlConnection(sConnect))
            {
                SqlCommand cmd = GetCmdByProperty();
                try
                {
                    cmd.Connection = conn;
                    cmd.CommandText = sSQL + ";Select @@IDENTITY;";
                    cmd.CommandType = CommandType.Text;
                    if (conn.State != ConnectionState.Open)
                        conn.Open();
                    object obj = cmd.ExecuteScalar();
                    if (obj == null)
                        iRet = 0;
                    else
                    {
                        if (!int.TryParse(obj.ToString(), out iRet))
                            iRet = 0;
                    }
                    return iRet;
                }
                catch (Exception e)
                {
                    Console.WriteLine("InsertDbBySQL:" + e.Message);
                    return -1;
                }
                finally
                {
                    if (cmd != null)
                        cmd = null;
                    conn.Close();
                }
            }
        }

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM