昨天在.NET区发了一个关于SQL的帖子无人问津

yibey 2010-08-24 10:33:50

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.有人知道这是为什么么求教
...全文
233 35 打赏 收藏 转发到动态 举报
写回复
用AI写文章
35 条回复
切换为时间正序
请发表友善的回复…
发表回复
yibey 2010-08-24
  • 打赏
  • 举报
回复
谢谢你拉,可是这没用不是有效的
「已注销」 2010-08-24
  • 打赏
  • 举报
回复
再给你翻译一下哦:
每次一个存储过程通过引用公共语言运行时 (CLR) 例程、类型或聚合来调用另一个存储过程或执行托管代码
时,嵌套级别都会增加。超过最大级数 32 时,事务即被终止。

在 Transact-SQL 字符串中执行 @@NESTLEVEL 时,返回的值为 1 + 当前嵌套级别。通过使用
sp_executesql 动态执行 @@NESTLEVEL 时,返回的值为 2 + 当前嵌套级别。


「已注销」 2010-08-24
  • 打赏
  • 举报
回复
楼主你没看到我在26楼贴的内容么?
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.
yibey 2010-08-24
  • 打赏
  • 举报
回复
总之能试的地方我都试验过了,但是在查询分析器里结果跟这里就是不一样
yibey 2010-08-24
  • 打赏
  • 举报
回复
24楼大哥,谢谢你提的意见我已经尝试过了这个循环语句,结果不是这里影响的
「已注销」 2010-08-24
  • 打赏
  • 举报
回复
Each time a stored procedure calls another stored procedure or executes managed code
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.
「已注销」 2010-08-24
  • 打赏
  • 举报
回复
Each time a stored procedure calls another stored procedure or executes managed code 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.
黄_瓜 2010-08-24
  • 打赏
  • 举报
回复
siteConnectionString = siteConnectionString.Replace("{0}", siteModel.HostName);
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();


有可能是这里嵌套的原因,呵呵,你把它去掉试试。。。。。。。。。。。
yibey 2010-08-24
  • 打赏
  • 举报
回复
[Quote=引用 20 楼 sh_suyuelin 的回复:]

为什么等于2.调试下不就知道了吗?
[/Quote]就是调试的时候看到结果是2,你能解释一下为什么是2么。小弟感激
yibey 2010-08-24
  • 打赏
  • 举报
回复
伤心啊,小弟被这JJ东西弄的无语了。从理论上讲这个值应该是0的,结果是2
黄_瓜 2010-08-24
  • 打赏
  • 举报
回复
[Quote=引用 17 楼 yibey 的回复:]
所以我才说你别怀疑,在触发器里也是可以使用的
[/Quote]
触发器是特殊的存储过程

大哥,小弟帮不了你了,O(∩_∩)O~
能力有限,别丢我板砖哦
sh_suyuelin 2010-08-24
  • 打赏
  • 举报
回复
为什么等于2.调试下不就知道了吗?
yibey 2010-08-24
  • 打赏
  • 举报
回复
同样肯定不是ExecuteScalar这个导致的,我已经验证过了
yibey 2010-08-24
  • 打赏
  • 举报
回复
因为我在当前SQL执行的时候返回是2,该插入表的触发器里该参数就是3,因为被嵌套一层了
yibey 2010-08-24
  • 打赏
  • 举报
回复
所以我才说你别怀疑,在触发器里也是可以使用的
黄_瓜 2010-08-24
  • 打赏
  • 举报
回复
[Quote=引用 14 楼 yibey 的回复:]
楼上的大哥请不要怀疑代码的可使用性
[/Quote]
我想告诉你的事ms说了 @@NESTLEVEL用于存储过程的判断
黄_瓜 2010-08-24
  • 打赏
  • 举报
回复
不知道你为什么要做这样的测试
可能是ExecuteScalar导致的吧 试试ExecuteNonQuery方法
yibey 2010-08-24
  • 打赏
  • 举报
回复
楼上的大哥请不要怀疑代码的可使用性
黄_瓜 2010-08-24
  • 打赏
  • 举报
回复
大哥,这个是用于存储过程的判断,普通语句后面跟一个都会返回0
如:
select * from tb
insert into tb select 1
select @@NESTLEVEL

yibey 2010-08-24
  • 打赏
  • 举报
回复
有哪位大哥能解决,小弟感激涕零啊
加载更多回复(15)

34,575

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server相关内容讨论专区
社区管理员
  • 基础类社区
  • 二月十六
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧