Access中的SELECT @@IDENTITY

來源:互聯網
上載者:User

   在Access資料庫中存在select @@identity嗎?答案是肯定的。但是Access一次只能執行一條SQL,多條SQL需要多次執行,這是限制。在SQL Server中,可以一次執行多條SQL語句。Access使用的是Jet-SQL,SQL Server使用的是T-SQL,兩者用法上相差很大。

 

   但是Access中可以連續執行N條語句,象下面這樣:
   cmd.CommandText = "INSERT INTO MyTable (N1,N2) VALUES (22,11)";
   int count = cmd.ExecuteNonQuery();
   cmd.CommandText = "SELECT @@IDENTITY";
   int newId = (int)cmd.ExecuteScalar();

 

   其中SELECT @@IDENTITY是取出前一條語句的自動編號的關鍵字。

 

 

在Sql Server中可以用ExecuteReader()來同時執行幾條一起的Sql語句
PetShop 4.0中有如下用法:
using (SqlDataReader rdr = cmd.ExecuteReader(CommandBehavior.CloseConnection)) {
// Read the returned @ERR
rdr.Read();
// If the error count is not zero throw an exception
if (rdr.GetInt32(1) != 0)
throw new ApplicationException("DATA INTEGRITY ERROR ON ORDER INSERT - ROLLBACK ISSUED");
}
其中cmd對象的Sql語句如下所示(我在調試中取出來的):
Declare @ID int;
Declare @ERR int;
INSERT INTO Orders VALUES
(@UserId, @Date, @ShipAddress1, @ShipAddress2, @ShipCity, @ShipState, @ShipZip, @ShipCountry, @BillAddress1, @BillAddress2, @BillCity, @BillState, @BillZip, @BillCountry, 'UPS', @Total, @BillFirstName, @BillLastName, @ShipFirstName, @ShipLastName, @AuthorizationNumber, 'US_en');
SELECT @ID=@@IDENTITY;
INSERT INTO OrderStatus VALUES(@ID, @ID, GetDate(), 'P');
SELECT @ERR=@@ERROR;
INSERT INTO LineItem VALUES( @ID, @LineNumber0, @ItemId0, @Quantity0, @Price0);
SELECT @ERR=@ERR+@@ERROR;
INSERT INTO LineItem VALUES( @ID, @LineNumber1, @ItemId1, @Quantity1, @Price1);
SELECT @ERR=@ERR+@@ERROR;
SELECT @ID, @ERR

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.