T-SQL - SQL Server - 根据ID_A更新ID_B中的记录

时间:2017-05-17 09:31:10

标签: sql sql-server tsql

我有两个方面:

Permissions

- Permission_ID
- Permission_Desc

Users:

- User_ID
- User_Name

一个Fact_Tables

- User_ID
- Permission_ID
- Salary_Amt

现在我要做的是将User_ID 1的权限应用于User_2。将User_ID_1的权限复制到User_ID_2

如何在更新声明中执行此操作?

谢谢!

1 个答案:

答案 0 :(得分:-1)

然而,如果我正确地理解了这个问题,我认为这应该可以完成工作:

    Create Table #permissions (permission_id int, permission_desc 
    varchar(100))

    insert into #permissions values (1, 'select'),(2,'insert')

   Create Table #users (user_idd int, user_desc varchar(100))

   insert into #users values (1, 'John'),(2,'Rohn')

   Create Table #fact_table (user_idd int, permission_id int, sal int)

   insert into #fact_table values (1,1,200), (2,2,300)

   select * from #fact_table 
   update a set a.permission_id = b.permission_id from #fact_table a 
    inner join (
    select (select user_idd from #fact_table where user_idd=2) user_idd, 
   permission_id, sal from #fact_table where permission_id = 1
   ) b on a.user_idd=b.user_idd