我有一条 SQLUpdate
语句,当通过新的查询窗口在 VS2010 中运行时,它可以工作,但是当我将它构造为更新语句时,什么也没有发生。
我想知道是否有人可以看到我做错了什么?就像我说的,当通过 VS2010 中的查询窗口执行代码时,代码可以工作。
这是我的代码:
SqlConnection connection = new SqlConnection(DBConnection.GetConnection().ConnectionString);
string updateStatement =
"DECLARE @OldParent hierarchyid, @NewParent hierarchyid " +
"SELECT @OldParent = Hierarchy FROM SpecProducts " +
"WHERE ID = @NodeToMoveID ; ------ top item to be moved " +
"SELECT @NewParent = Hierarchy FROM SpecProducts " +
"WHERE ID = @NodeToMoveIntoID ; -- ID of item to move into - new parent " +
"DECLARE children_cursor CURSOR FOR " +
"SELECT Hierarchy FROM SpecProducts " +
"WHERE Hierarchy = @OldParent; " +
"DECLARE @ChildId hierarchyid; " +
"OPEN children_cursor " +
"FETCH NEXT FROM children_cursor INTO @ChildId; " +
"WHILE @@FETCH_STATUS = 0 " +
"BEGIN " +
"START: " +
"DECLARE @NewId hierarchyid; " +
"SELECT @NewId = @NewParent.GetDescendant(MAX(Hierarchy), NULL) " +
"FROM SpecProducts WHERE Hierarchy.GetAncestor(1) = @NewParent; " +
"UPDATE SpecProducts " +
"SET Hierarchy = Hierarchy.GetReparentedValue(@ChildId, @NewId), " +
"MovedToDate = @MovedToDate, " +
"MovedToBy = @MovedToBy " +
"WHERE Hierarchy.IsDescendantOf(@ChildId) = 1; " +
"IF @@error <> 0 GOTO START -- On error, retry " +
"FETCH NEXT FROM children_cursor INTO @ChildId; " +
"END " +
"CLOSE children_cursor; " +
"DEALLOCATE children_cursor;";
SqlCommand updateCommand = new SqlCommand(updateStatement, connection);
updateCommand.Parameters.AddWithValue("@NodeToMoveID", nodeIDToMove);
updateCommand.Parameters.AddWithValue("@NodeToMoveIntoID", newParentID);
updateCommand.Parameters.AddWithValue("@MovedToDate", DateTime.Now);
updateCommand.Parameters.AddWithValue("@MovedToBy", userModifying.ID);
try
{
connection.Open();
updateCommand.ExecuteNonQuery();
return true;
}
需要指出的一件事是,当我的代码在 VS2010 查询窗口中执行时,我收到有关“声明”功能的警告,但我按下继续,它就可以工作了。我的声明不起作用与 Declare 功能有关吗?
任何想法/解决方案将不胜感激。
非常感谢。
抢