ALTER PROCEDURE [dbo].[S_DB_TFDB_ADD] @UserID int, @UserName varchar(20), @Password varchar(20), @GroupCode varchar(50), @RoleID varchar(20), @tableName varchar(20), @Num int AS begin declare @ChildTableSysUser varchar(2000), @id int,@ChildTableUserRole varchar(2000),@i int,@Role varchar(2000) set xact_abort on begin tran set @ChildTableSysUser='insert into '+@tableName+'.dbo.S_SysUser(UserName,Password,GroupCode)values('''+@UserName+''','''+@Password+''','''+@GroupCode+''')' print @ChildTableSysUser exec (@ChildTableSysUser) select @id=@@identity; print @id insert into S_SysUser_Child(UserID,UserName,Password,GroupCode,RoleID) values(@id,@UserName,@Password,@GroupCode,@RoleID) select @i=0 while @i<@Num begin set @ChildTableUserRole='insert into '+@tableName+'.dbo.S_UserRole(UserID,RoleID) values('+convert(nvarchar(20),@id)+','+@RoleID+')' print @ChildTableUserRole exec(@ChildTableUserRole) set @i=@i+1 commit tran end
set @ChildTableUserRole='insert into '+@tableName+'.dbo.S_UserRole(UserID,RoleID) values('+convert(nvarchar(20),@id)+','+@RoleID+')'
这个里面的@RoleID的格式是 '0,1,2,3,4'
我现在已经找了个函数分割好了
如何取出里面的 值 循环插入到set @ChildTableUserRole='insert into '+@tableName+'.dbo.S_UserRole(UserID,RoleID) values('+convert(nvarchar(20),@id)+','+@RoleID+')'
就是替换那个@RoleID
用游标取值吧 然后插入
'insert into '+@tableName+'.dbo.S_UserRole(UserID,RoleID) select '+convert(nvarchar(20),@id)+',a from f_split('''+@RoleID+''','','')'
呵呵 ok了 在网上看了下游标 取值 我不是要分割的问题
ALTER PROCEDURE [dbo].[S_DB_TFDB_ADD]
@UserID int,
@UserName varchar(20),
@Password varchar(20),
@GroupCode varchar(50),
@RoleID varchar(20),
@tableName varchar(20)
AS
begin
declare @ChildTableSysUser varchar(2000),@ChildTableUserRole varchar(2000),@Table varchar(200),@id int,@i int
set xact_abort on
begin tran
--插入子表S_SysUser
set @ChildTableSysUser='insert into '+@tableName+'.dbo.S_SysUser(UserName,Password,GroupCode)values('''+@UserName+''','''+@Password+''','''+@GroupCode+''')'
--print @ChildTableSysUser
exec (@ChildTableSysUser)
--获取上次插入S_SysUserID
select @id=@@identity;
--print @id
--插入S_SysUser_Child
insert into S_SysUser_Child(UserID,UserName,Password,GroupCode,RoleID) values(@id,@UserName,@Password,@GroupCode,@RoleID)
declare cr_cursor cursor --定义游标
for select * from dbo.f_split(@RoleID,',')
open cr_cursor --打开游标
fetch From cr_cursor into @Table --提取游标
while @@fetch_status=0
begin
set @ChildTableUserRole='insert into '+@tableName+'.dbo.S_UserRole(UserID,RoleID) values('+convert(nvarchar(20),@id)+','+@Table+')'
--print @ChildTableUserRole
exec(@ChildTableUserRole)
fetch next From cr_cursor into @Table
end;
close cr_cursor --关闭游标
deallocate cr_cursor --释放游标
commit tran
end