In fact, it is easy to use SQL Server Stored Procedures and triggers (10-minute getting started tutorial)

Source: Internet
Author: User

(I said it was a 10-minute entry. In fact, for me, it was really just the ten minutes that the teacher talked about. It wasn't too exaggerated. Then I knew how to write the stored procedure and trigger, of course it's just getting started, but I have been waiting for ten minutes. It would take at least a few days for me to read a book by myself ....)

For a long time, the stored procedures and triggers of SQL Server are basically not used, but I occasionally find a few simple functions paste on the Internet to use in My SQL. I always felt a weakness when I wrote it myself. I spoke about the SQL Server for one day during the training of the organization a few days ago. After reading the stored procedure written by the teacher, I found that the stored procedure was not very difficult.

In fact, it is also the reason why I have never insisted on research, but it is undeniable that some people talk about it and learn it on their own, and the efficiency is really incomparable. Especially for those of us who have been programming for many years but are still restless to read and learn, there are a lot of things that need to be brought in front of us, I broke the layer of paper and gave everyone an overview. The rest is just a matter of time and proficiency.

 

I think the Stored Procedure paper is here:

1. Variable declaration and usage

Declare @ VaR as varchar (50)

Set Var = 'abcd'

2. If Loop

If isnull (@ prjid, 0) = 0
Begin
Print 'No prjid found !!! '
Return
End
Else

Begin

-- Some code must be written here; otherwise, an error is reported.

End

3. Use of cursors

  1. Declare curjd cursor
  2. Select ID, fzbh from _ TZK
  3. Open curjd
  4. Fetch next from curjd into @ jdid, @ paperno
  5. While @ fetch_status = 0
  6. Begin
  7. -- Here is some processing code
  8. Fetch next from curjd into @ jdid, @ paperno
  9. End
  10. Close curjd
  11. Deallocate curjd

4. Use of triggers

The key lies in the use of inserted tables and deleted tables.

  1. Alter trigger roleinsert on [DBO]. [rolename]
  2. After insert
  3. As
  4. Declare @ roleid as bigint
  5. Set @ roleid = (select roleid from inserted)
  6. Insert into rolerule (roleid, menuid, PW)
  7. Select @ roleid, menuid, 0 from menus where menus. menuid not in
  8. (Select menuid from rolerule where roleid = @ roleid)
  9. Update rolerule set PW = 1 where menuid in (select menuid from menus where always = 1)
  10. And roleid in (select roleid from inserted)

With these examples, the other is a skillful process.

Related Article

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.