所以,你想要你的肮脏方式。
这正是您正在寻找的。
好的,那么现在看看这个。
假设您有一个如下表:
CREATE TABLE products1 (id int IDENTITY, product varchar(40))
CREATE TABLE products2 (id int IDENTITY, product varchar(40))
行。因此,现在您将 Excel 中的记录插入到 Product1 表中。像
Int LastId=0
Foreach (Rows from ExcelDoc1){
ProductName=ExcelColumn
Insert into Products1 values(ProductName)
}
所以现在获取最后插入行的标识符,以便我们可以在另一个必须是起始 ID 的表上使用它,如下所示:
LastId= Select isnull(Max(ID),0)+1 as NextId from Products1
So now you have an Last Identity on LastID
So Before Inserting an new row on the Next table
Modify that tables, Identity Insert Mode, Because you are self inserting an new Identity on that table.
SET IDENTITY_INSERT products1 ON
Foreach (Rows from ExcelDoc2){
ProductName=ExcelColumn
Insert into Products1 values(LastId,ProductName)
LastId++
}
Again reset that Setting so that, you dont have to insert an identity value yourslef.
SET IDENTITY_INSERT products1 OFF
完毕!!!
如果有的话看看这个链接: http: //msdn.microsoft.com/en-us/library/aa259221 (v=sql.80).aspx