SQL Server databases (SQL sever language stored procedures and triggers)

Source: Internet
Author: User

Stored procedure: Just like a function
stored in the database--"programmability--" storage procedures


Create stored procedures: Save database tables, programmability, stored procedures
Create proc Jiafa

--Required parameters
@a int,
@b int
As
--Stored Procedure content
declare @c int;
Set @c [email protected] + @b;
return @c;
Go


Equivalent to a function
public int function name (variable 1, variable 2)
{
Function statements
return int C
}

Can be called directly after storage,
Executing stored procedures
EXEC Store Name

When there is a return value
declare @f int;
EXEC @f= Jiafa 3, 5;
Print @f;


--Example: Check the car table for the corresponding type of vehicle number
--Stored procedures
creat proc Chaxun
@n varchar (20)
As
DECLARE @num int
Select @num =coount (*) from car where name like '% ' [email protected]+ '% '
Go

--Call method
DECLARE @m int
exec @m=chaxun ' Audi '
Print @m

Trigger:
is a special stored procedure;
By adding or deleting the action to trigger the execution, no parameters, no return value;
The storage location is in the drop-down list of the table to be executed

Do not go to trigger not execute, meet the conditions after execution, equivalent to an event in HTML

Create TRIGGER Trigger Name

Insert_student-Name Specification Action _ The name of the table to be executed in order to understand the circumstances of the trigger to be used more easily

On Student-pin for that watch
For insert--triggers for that action

--onclick= "Show ()"
As
Code to be executed by the trigger

Go

1. For means to trigger after the action is executed
2, instead of Delete means that the deletion before the throw, can be understood as an alternative, write this after writing the code is useless, the code is covered by the trigger

When you delete the contents of a table that has a foreign key, you need to first delete the contents of the Foreign key connected table
Create Trigger Delete_info
On inf0
Instead of delete
As
DECLARE @c varchar (20)
Select @c =code from deleted

Delete from work where [email protected]
Delete from family where [email protected]
Delete from info where [email protected]

Go


Instead of use is more frequent than for

The interview process is more asked, but the actual work is relatively small

SQL Server databases (SQL sever language stored procedures and triggers)

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.