突然想到的些sql语句

现有两张表 DataCenter_UserInfo 和 datacenter_power

userInfo 表里面有383条数据,userPower 表中有11条数据,

想实现:创建一个中间表DataCenter_User_Power,每个用户对应11条权限

 

方法一:

declare @i int=0
while @i<383
begin
insert into DataCenter_User_Power (UserID,PowerID,IsActive) select 0,id,1 from datacenter_power
set @i=@i+1
end

 

declare @num int=11
declare @j int =1
while @num<=4213
begin
update DataCenter_User_Power set UserID=(select id from (select ROW_NUMBER() over(order by id) as num,id from DataCenter_UserInfo) t where t.num=@j) where id<=@num and id>= @num-10
set @num=@num+11
set @j=@j+1
end

 

方法二:

(中间表还没创建)

select * into DataCenter_User_Powerfrom(SELECT d.id UserID,a.id PowerID from DataCenter_UserInfo d left join Datacenter_Power a on 1=1) t

 

你可能感兴趣的:(突然想到的些sql语句)