日期:2014-05-18  浏览次数:20419 次

帮看下这个存储过程怎样优化。
SQL code

ALTER PROCEDURE [dbo].[Mcd_cr_user_AddNewUser] 
@userId int,
@nowSystem_id int,
@OpUserId int
AS
BEGIN
--从临时表读取数据存入临时表

select [region_id],[first_name],[second_name],[update_user],[update_time],[ad_account],[email] into #T from [MCDTemp].[dbo].[McD_CR_Tmp_User] where [tmp_user_id]=@userId

--定义存入实际用户表的变量名

declare @RegionId int
declare @firstname varchar(50)
declare @secondname varchar(50)
declare @password varchar(50)
declare @updatetime smalldatetime
declare @updateruser int
declare @ad_account nvarchar(50)
declare @email nvarchar(100)
declare @expire_flag bit
declare @delete_flag bit
declare @newUserId int
declare @isAdmin int

--从临时表中读取数据给变量赋值

select @RegionId= [region_id] from #T 
select @firstname=[first_name] from #T
select @secondname=[second_name] from #T
select @updatetime=[update_time] from #T
select @updateruser=[update_user] from #T
select @ad_account=[ad_account] from #T
select @email=[email] from #T
set @expire_flag= 1
set @delete_flag= 0
set @password= 'pass1234'
set @isAdmin= 0
--执行插入新表操作

insert into [McD_CR].[dbo].[McD_Sys_Data_User]([region_id],[first_name],[second_name],[update_user],[update_time],[ad_account],[email],[expire_flag],[delete_flag],[password],[is_admin])
values(@RegionId,@firstname,@secondname,@updateruser,@updatetime,@ad_account,@email,@expire_flag,@delete_flag,@password,@isAdmin)

--取得新增的userID

select @newUserId=(select [USER_ID] from [McD_CR].[dbo].[McD_Sys_Data_User] 
where [region_id]=@RegionId and [first_name]=@firstname  and [second_name]=@secondname
)

--定义存入新角色表关系游标中的变量

declare @role_rel_role_id int
declare @role_rel_module_id int

--定义存入新角色关系表的变量
declare @newRole_userId int
declare @newRole_relId int
declare @newRole_moduleRel_id int

--读取临时表该用户的角色关系记录存入新角色关系表

declare MyCursorRoleRel cursor for select [role_id],[module_id] FROM [MCDTemp].[dbo].[McD_CR_Tmp_User_Role] where [tmp_user_id]=@userId
open MyCursorRoleRel
fetch next from MyCursorRoleRel into @role_rel_role_id,@role_rel_module_id
while(@@fetch_status=0)
    begin
    select @newRole_userId= @newUserId
    select @newRole_relId= @role_rel_role_id
    select @newRole_moduleRel_id= @role_rel_module_id
    insert into [McD_CR].[dbo].[McD_Sys_Rel_User_Role] values(@newRole_userId,@newRole_relId,@newRole_moduleRel_id)
    fetch next from MyCursorRoleRel into @role_rel_role_id,@role_rel_module_id
    end
    close MyCursorRoleRel
    deallocate MyCursorRoleRel
    
--新增用户角色关系表添加结束

--定义存入新用户组织关系游标中的变量

declare @orgrel_orgId int

--定义存入新用户组织关系表中的变量

declare @newOrgRel_userId int
declare @newOrgRel_orgId int
declare @system_id int
declare @update_id int
declare @update_time smalldatetime

--读取临时表中的数据存入新用户组织关系表

declare MyCursorRoleOrg cursor for select [node_key] FROM [MCDTemp].[dbo].[McD_CR_Tmp_User_Org] where [tmp_user_id]=@userId and [org_type]=1
open MyCursorRoleOrg
fetch next from MyCursorRoleOrg into @orgrel_orgId
while(@@fetch_status=0)
    begin
    select @newOrgRel_orgId= @orgrel_orgId
    select @newOrgRel_userId= @newUserId
    select @system_id= @nowSystem_id 
    select @update_id= @OpUserId
    select @update_time=GETDATE()
    insert into [McD_CR].[dbo].[McD_Sys_Rel_User_Org] values(@newOrgRel_userId,@newOrgRel_orgId,@system_id,@update_time,@update_id)
    fetch next from MyCursorRoleOrg into @orgrel_orgId
    end
    close MyCursorRoleOrg
    deallocate MyCursorRoleOrg

--新增用户组织关系表添加结束

--定义插入新组织关系授权表的变量

declare @newOPSUserId int
declare @newOPSNodeId int
declare @newOPSNodeLevel int
declare @newOpsUpdateTime smalldatetime
declare @newOPSUpdateUserId int

select [node_key],[node_level] into #T1 FROM [MCDTemp].[dbo].[Mcd_CR_Tmp_User_Org] where [tmp_user_id]=@userId and [org_type]=2

select @newOPSUserId= @newUserId
select @newOPSNodeId=[node_key] from #T1
select @newOPSNodeLevel=[node_level] from #T1
set @newOpsUpdateTime=GETDATE()
select @newOPSUpdateUserId