【SQL】把另一个数据库里的表A的信息在B表中提取出来,然后添加到C表。修改了标识列值

SET IDENTITY_INSERT [UserRole] ON;

SELECT ROW_NUMBER() OVER ( ORDER BY T1.ID ) i,T1.ID INTO #A FROM [GMSAccount].[dbo].[User] AS T1
JOIN [GMSToolManage].[dbo].[Manager] AS T2
ON T1.EmpID=T2.ID;

DECLARE @count INT;
SELECT @count=COUNT(1) FROM #A; 
DECLARE @tot INT;
SELECT @tot=COUNT(1) FROM [GMSAccount].[dbo].[UserRole]; 

DECLARE @i INT=1;
WHILE(@i<=@count)
BEGIN
  INSERT INTO [GMSAccount].dbo.[UserRole](ID,UserID,RoleID)
        SELECT @i+@tot,ID,5
        FROM #A 
        WHERE i=@i;
  SET @i=@i+1;                                
END;

if OBJECT_ID('tempdb..#A') is not null drop table #A;
SET IDENTITY_INSERT [UserRole] OFF;

DBCC CHECKIDENT(UserRole)--修改标识列值

 

posted @ 2020-05-23 15:41  aq_seabiscuit  阅读(293)  评论(0编辑  收藏  举报