sql = @"insert into contact(contactName,nickName,address,city,state,crossStreets,postalCode,otRestaurantId,otNeighborhood,otCuisine,otPrice,colorCode,version,sharedContactId,enteredBy,entryDate)
values(@contactName,@nickName,@address,@city,@state,@crossStreets,@postalCode,@otRestaurantId,@otNeighborhood,@otCuisine,@otPrice,@colorCode,@version,@sharedContactId,@enteredBy,getDate());
select @@nestlevel,scope_identity()";
cmd = new SqlCommand(sql, siteconnc);
cmd.Transaction = sqlTran;
cmd.Parameters.AddWithValue("@contactName", row["primaryName"]);
cmd.Parameters.AddWithValue("@nickName", row["phoneNumber"]);
cmd.Parameters.AddWithValue("@address", row["address"]);
cmd.Parameters.AddWithValue("@city", row["city"]);
cmd.Parameters.AddWithValue("@state", row["state"]);
cmd.Parameters.AddWithValue("@crossStreets", row["crossStreet"]);
cmd.Parameters.AddWithValue("@postalCode", row["postalCode"]);
cmd.Parameters.AddWithValue("@otRestaurantId", row["openTableId"]);
cmd.Parameters.AddWithValue("@otNeighborhood", row["neighborhood"]);
cmd.Parameters.AddWithValue("@otCuisine", row["cuisine"]);
cmd.Parameters.AddWithValue("@otPrice", row["otprice"]);
cmd.Parameters.AddWithValue("@colorCode", "normal");
cmd.Parameters.AddWithValue("@version", 1);
cmd.Parameters.AddWithValue("@sharedContactId", row["contactId"]);
cmd.Parameters.AddWithValue("@enteredBy", "Gold Key Solutions");
string contactId = cmd.ExecuteScalar().ToString();
某人在.NET里使用这段代码,发现contactId竟然是等于2.有人知道这是为什么么求教
改成
select scope_identity()
如果在查询分析器里执行这个代码的话@@nestlevel=0
siteconnc = new SqlConnection(siteConnectionString);
Con = new SqlConnection(siteConnectionString);
siteconnc.Open();
sqlTran = siteconnc.BeginTransaction();
if (tableList.Count > 0)
{
foreach (DataTable dataTable in tableList)
{
foreach (DataRow row in dataTable.Rows)
{
sql = "select count(*) from contact where sharedContactId = @sharedContactId";
cmd = new SqlCommand(sql, siteconnc);
cmd.Transaction = sqlTran;
cmd.Parameters.AddWithValue("@sharedContactId", row["contactId"]);
string count = cmd.ExecuteScalar().ToString(); if (Convert.ToInt32(count) == 0)
{
sql = @"insert into contact(contactName,nickName,address,city,state,crossStreets,postalCode,otRestaurantId,otNeighborhood,otCuisine,otPrice,colorCode,version,sharedContactId,enteredBy,entryDate)
values(@contactName,@nickName,@address,@city,@state,@crossStreets,@postalCode,@otRestaurantId,@otNeighborhood,@otCuisine,@otPrice,@colorCode,@version,@sharedContactId,@enteredBy,getDate());
select @@nestlevel,scope_identity()";
cmd = new SqlCommand(sql, siteconnc);
cmd.Transaction = sqlTran;
cmd.Parameters.AddWithValue("@contactName", row["primaryName"]);
cmd.Parameters.AddWithValue("@nickName", row["phoneNumber"]);
cmd.Parameters.AddWithValue("@address", row["address"]);
cmd.Parameters.AddWithValue("@city", row["city"]);
cmd.Parameters.AddWithValue("@state", row["state"]);
cmd.Parameters.AddWithValue("@crossStreets", row["crossStreet"]);
cmd.Parameters.AddWithValue("@postalCode", row["postalCode"]);
cmd.Parameters.AddWithValue("@otRestaurantId", row["openTableId"]);
cmd.Parameters.AddWithValue("@otNeighborhood", row["neighborhood"]);
cmd.Parameters.AddWithValue("@otCuisine", row["cuisine"]);
cmd.Parameters.AddWithValue("@otPrice", row["otprice"]);
cmd.Parameters.AddWithValue("@colorCode", "normal");
cmd.Parameters.AddWithValue("@version", 1);
cmd.Parameters.AddWithValue("@sharedContactId", row["contactId"]);
cmd.Parameters.AddWithValue("@enteredBy", "Gold Key Solutions");
string contactId = cmd.ExecuteScalar().ToString();
返回对本地服务器上执行的当前存储过程的嵌套级别(初始值为 0)。你用到这里干什么
SELECT @@NESTLEVEL AS 'Current Nest Level';
EXEC ('SELECT @@NESTLEVEL AS OneGreater');
EXEC sp_executesql N'SELECT @@NESTLEVEL as TwoGreater' ;
GO
EXEC usp_NestLevelValues;
GO
/*
下面是结果集: Current Nest Level
------------------
1(1 row(s) affected)OneGreater
-----------
2(1 row(s) affected)TwoGreater
-----------
3(1 row(s) affected)
*/
如:
select * from tb
insert into tb select 1
select @@NESTLEVEL
可能是ExecuteScalar导致的吧 试试ExecuteNonQuery方法
siteconnc = new SqlConnection(siteConnectionString);
Con = new SqlConnection(siteConnectionString);
siteconnc.Open();
sqlTran = siteconnc.BeginTransaction();
if (tableList.Count > 0)
{
foreach (DataTable dataTable in tableList)
{
foreach (DataRow row in dataTable.Rows)
{
sql = "select count(*) from contact where sharedContactId = @sharedContactId";
cmd = new SqlCommand(sql, siteconnc);
cmd.Transaction = sqlTran;
cmd.Parameters.AddWithValue("@sharedContactId", row["contactId"]);
string count = cmd.ExecuteScalar().ToString(); if (Convert.ToInt32(count) == 0)
{
sql = @"insert into contact(contactName,nickName,address,city,state,crossStreets,postalCode,otRestaurantId,otNeighborhood,otCuisine,otPrice,colorCode,version,sharedContactId,enteredBy,entryDate)
values(@contactName,@nickName,@address,@city,@state,@crossStreets,@postalCode,@otRestaurantId,@otNeighborhood,@otCuisine,@otPrice,@colorCode,@version,@sharedContactId,@enteredBy,getDate());
select @@nestlevel,scope_identity()";
cmd = new SqlCommand(sql, siteconnc);
cmd.Transaction = sqlTran;
cmd.Parameters.AddWithValue("@contactName", row["primaryName"]);
cmd.Parameters.AddWithValue("@nickName", row["phoneNumber"]);
cmd.Parameters.AddWithValue("@address", row["address"]);
cmd.Parameters.AddWithValue("@city", row["city"]);
cmd.Parameters.AddWithValue("@state", row["state"]);
cmd.Parameters.AddWithValue("@crossStreets", row["crossStreet"]);
cmd.Parameters.AddWithValue("@postalCode", row["postalCode"]);
cmd.Parameters.AddWithValue("@otRestaurantId", row["openTableId"]);
cmd.Parameters.AddWithValue("@otNeighborhood", row["neighborhood"]);
cmd.Parameters.AddWithValue("@otCuisine", row["cuisine"]);
cmd.Parameters.AddWithValue("@otPrice", row["otprice"]);
cmd.Parameters.AddWithValue("@colorCode", "normal");
cmd.Parameters.AddWithValue("@version", 1);
cmd.Parameters.AddWithValue("@sharedContactId", row["contactId"]);
cmd.Parameters.AddWithValue("@enteredBy", "Gold Key Solutions");
string contactId = cmd.ExecuteScalar().ToString();
有可能是这里嵌套的原因,呵呵,你把它去掉试试。。
by referencing a common language runtime (CLR) routine, type, or aggregate, the
nesting level is incremented. When the maximum of 32 is exceeded, the transaction is
terminated.When @@NESTLEVEL is executed within a Transact-SQL string, the value
returned is 1 + the current nesting level. When @@NESTLEVEL is executed dynamically
by using sp_executesql the value returned is 2 + the current nesting level.
When @@NESTLEVEL is executed within a Transact-SQL string, the value
returned is 1 + the current nesting level. When @@NESTLEVEL
is executed dynamically
by using sp_executesql the value returned is 2 + the current nesting level.
每次一个存储过程通过引用公共语言运行时 (CLR) 例程、类型或聚合来调用另一个存储过程或执行托管代码
时,嵌套级别都会增加。超过最大级数 32 时,事务即被终止。在 Transact-SQL 字符串中执行 @@NESTLEVEL 时,返回的值为 1 + 当前嵌套级别。通过使用
sp_executesql 动态执行 @@NESTLEVEL 时,返回的值为 2 + 当前嵌套级别。
一条insert 一条select