Sql server T-SQL:了解用于更新的游标

Sql server T-SQL:了解用于更新的游标,sql-server,tsql,Sql Server,Tsql,我不知道for update of在t-SQL中的游标中扮演什么角色 我写了以下SQL语句: declare @id int, @nom varchar(30), @age int; declare cur_info cursor for select id, nom, age from info **FOR UPDATE OF** age; open cur_info; fetch next from cur_info into @id, @nom, @age;

我不知道for update of在t-SQL中的游标中扮演什么角色

我写了以下SQL语句:

declare @id int, @nom varchar(30), @age int;

declare cur_info cursor for 
     select id, nom, age 
     from info **FOR UPDATE OF** age;

open cur_info;

fetch next from cur_info into @id, @nom, @age;

while @@FETCH_STATUS = 0
begin
    if @age = 20
        update info 
        set age = 0 
        where id = @id;

    fetch next from cur_info into @id , @nom , @age;
end

close cur_info;
deallocate cur_info;
当我执行代码时,更新完成了,但当我不使用更新的年龄时,更新也完成了,我想了解更新的含义,如果可以的话;我想要一个使用这个的真实例子,来自:

更新[OF _column_name_[,…n]]定义了中的可更新列 光标。如果指定了OF[,],则只有 列出的列允许修改。如果指定的更新没有 列列表,可以更新所有列

用例可能是一个表用户,您希望使用光标的代码能够更新EmailAddress和TelephoneNumber,但不能更新Username、HashedPassword或RegistrationDate。。。有关电子邮件地址、电话号码的更新。。。将阻止对其他列进行更改。

我找到了解决方案 我的问题是为什么我写作的时候 更新年龄 在光标中,我可以更新年龄和id、姓名。。。。。 例如:

declare @id int,@name varchar(30),@age int;
declare cur_info cursor  for select id , nom , age from info  for update of age ;
open cur_info;
fetch next from cur_info into @id,@nom,@age;
while @@FETCH_STATUS=0
begin
if @age = 20
update info set age = 10 where id = @id ;
print @age;
fetch next from cur_info into @id , @nom , @age;
end
close cur_info;
deallocate cur_info;
但是,当我在where之后使用“current of cursor_name”时,我无法更新任何列exepte var1,并且sql server给我一个错误,游标有一个FOR update列表,请求更新的列不在此列表中

范例

declare @id int,@nom varchar(30),@age int;
declare cur_info cursor  for select id , nom , age from info  for update of nom ;
open cur_info;
fetch next from cur_info into @id,@nom,@age;
while @@FETCH_STATUS=0
begin
if @age = 0
update info set age = 20 where current of cur_info ;
print @age;
fetch next from cur_info into @id , @nom , @age;
end
close cur_info;
deallocate cur_info;

oracle和tsql标记在此上下文中不兼容。不是一个就是另一个,但不是两个都有。至于这个问题。。。我在这里猜测,但我怀疑这是一个锁定提示。另外,这个查询根本不需要游标。谢谢你的评论,但是游标的课程包含在我的学校课程中,所以我需要了解它总是一个好的开始…我以前写过它,但我不明白更新问题的作用是什么,我用它更新EmailAddres、电话号码。。。但即使是所有我可以更新的用户名和所有列,如用户名password@AyoubELKhaddari正如您的回答所示,如果update语句没有引用游标,那么您可以更新任何列或行。如果使用…将更新绑定到光标。。。如果MyCursor为current,则它将强制执行游标的列限制。我不知道电流在哪里+非常感谢你