(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
- Declare curjd cursor
- Select ID, fzbh from _ TZK
- Open curjd
- Fetch next from curjd into @ jdid, @ paperno
- While @ fetch_status = 0
- Begin
- -- Here is some processing code
- Fetch next from curjd into @ jdid, @ paperno
- End
- Close curjd
- Deallocate curjd
4. Use of triggers
The key lies in the use of inserted tables and deleted tables.
- Alter trigger roleinsert on [DBO]. [rolename]
- After insert
- As
- Declare @ roleid as bigint
- Set @ roleid = (select roleid from inserted)
- Insert into rolerule (roleid, menuid, PW)
- Select @ roleid, menuid, 0 from menus where menus. menuid not in
- (Select menuid from rolerule where roleid = @ roleid)
- Update rolerule set PW = 1 where menuid in (select menuid from menus where always = 1)
- And roleid in (select roleid from inserted)
With these examples, the other is a skillful process.