Use a cursor to generate an SQL script

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.