zoukankan      html  css  js  c++  java
  • Access中的SELECT @@IDENTITY

       在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

  • 相关阅读:
    char nvarchar varchar
    第32月第8天 打包 Framework 时使用 CocoaPods 引入第三方库的方法
    第31月第25天 xcode debug 限制uitextfiled输入
    第31月第22天 draw
    第31月第19天 NV12
    第31月第17天 resolveInstanceMethod
    第31月第15天 -fembed-bitcode
    第31月第10天 tableview头部空白 Other Linker Flags rtmp
    第31月 第9天 责任链AppDelegate
    第30月第18天 autolayout代码
  • 原文地址:https://www.cnblogs.com/zywf/p/4644068.html
Copyright © 2011-2022 走看看