A requirement was received last week to help the customer initialize the data of the permission module (including the user, role, module, and relationship between the three, as well as the relationship between the user and the user, and between the role and role, the relationship between the module and the module). After reading the requirements, I was confused. Fortunately, the customer provided an Excel table, but the data was distributed in two work tables. It was not easy to split it out ~~~
It is in this format (this is a role and a superior role)
Writing one by one is obviously not feasible. I thought that a circular parent script was just created with the cursor a few days ago, is it possible to use a cursor to traverse the data given by the customer and generate a script in the desired format? The answer is yes.
First, import the initial data processing (remove some blank records, sort out the header supervisor-role into supervisorrole, etc.) into the database (table name roledata), and then start to use the cursor to customize the initial script...
Take the role table as an example.
Declare @ SQL varchar (8000 ),
@ Rolename varchar (100)
Set @ SQL =''
Declare rolecursor cursor
Select [role] From roledata -- data to be traversed
Open rolecursor
Fetch next from rolecursor into @ rolename
While @ fetch_status = 0
Begin
Set @ SQL = 'insert into roles (rolename )'
Set @ SQL = @ SQL + 'values (''' + @ rolename + ''');'
Print @ SQL
Fetch next from rolecursor into @ rolename
End
Close rolecursor
Deallocate rolecursor
This completes the role data import, followed by the relationship between role and role (supervisor-role)
the writing method is similar to this method, but it is more complicated, for specific implementation, you can download the attachment analysis.
Of course, it is better to put all the required scripts into one cursor.
the detailed script is in the attachment, including creating roles scripts and Excel documents and the scripts for these two cursors...
attachment:/files/Beal/initrole.rar