我有2张桌子。一个具有管理器名称和空列的,我想从表1(具有层次结构)插入数据。我想我们的插件,但我没有使用确切的名称,我想使用变量将表2中的名称传递给表1,因此它的动态。请告诉我是否可以这样做以及如何做到这一点。
UPDATE direct_manager_report
Set Manager_level_number =
(Select 'Manager_Level_0' from manager_hierarchy where level_0_manager_name = 'ABC'
UNION
Select 'Manager_Level_1' from manager_hierarchy where level_0_manager_name < > 'ABC' AND level_1_manager_name = 'ABC'
UNION
Select 'Manager_Level_2' from manager_hierarchy where level_1_manager_name < > 'ABC' AND level_2_manager_name = 'ABC'
UNION
Select 'Manager_Level_3' from manager_hierarchy where level_2_manager_name < > 'ABC' AND level_3_manager_name = 'ABC'
)
Where full_name = 'ABC'
答案 0 :(得分:0)
我相信如果您只想让一个位置放置经理的名字,这应该有效,假设查询返回所需的结果,除了必须硬编码&#39; ABC&#39;到处:
DECLARE @man_name AS VARCHAR(50) = 'ABC'
UPDATE direct_manager_report
Set Manager_level_number =
(Select 'Manager_Level_0' from manager_hierarchy where level_0_manager_name = @man_name
UNION
Select 'Manager_Level_1' from manager_hierarchy where level_0_manager_name < > @man_name AND level_1_manager_name = @man_name
UNION
Select 'Manager_Level_2' from manager_hierarchy where level_1_manager_name < > @man_name AND level_2_manager_name = @man_name
UNION
Select 'Manager_Level_3' from manager_hierarchy where level_2_manager_name < > @man_name AND level_3_manager_name = @man_name
)
Where full_name = @man_name
您可以通过select语句填充@man_name变量: DECLARE @man_name AS VARCHAR(50) SET @man_name =(SELECT FROM)
如果你想循环使用一些名字,你可以使用光标,或者你可以忘记变量并在更新中使用JOIN。