SQL Server writes stored procedure gadgets (i)

Source: Internet
Author: User
Tags datetime insert table name
server| Stored Procedures

Enet Institute of Technology, all rights reserved

In the process of developing a database system, many stored procedures are often written. To unify the format and simplify the development process, I write some stored procedures to automate the generation of stored procedures. Here's a brief introduction to them. One is used to generate the insert process and the other to generate the update process.


Sp_geninsert

After the procedure is run, it generates a complete insert process for the given table. If the original table has an identity column, you must manually delete the set Idntity_insert on statement in the generated procedure.

The syntax is as follows

Sp_geninsert < Table Name >,< Stored Procedure name >

Take the Northwind database as an example

Sp_geninsert ' Employees ', ' ins_employees '

An insert stored procedure is generated at the end. With it, you can make further development.


Sp_genupdate

It generates an update stored procedure for a table. The syntax is as follows:

Sp_genupdate < Table Name >,< Primary Key >,< Stored Procedure Name >

Take the Northwind database as an example

Sp_genupdate ' Employees ', ' EmployeeID ', ' upd_employees '

After the run, generate the stored procedure as shown below:

Create Procedure Upd_employees

@EmployeeID int

@LastName nvarchar (40),

@FirstName nvarchar (20),

@Title nvarchar (60),

@TitleofCourtesy nvarchar (50),

@BirthDate datetime,

@HireDate datetime,

@Address nvarchar (120),

@City nvarchar (30),

@Region nvarchar (30),

@PostalCode nvarchar (20),

@Country nvarchar (30),

@HomePhone nvarchar (48),

@Extension nvarchar (8),

@Phote image,

@Notes ntext,

@ReportsTo int,

@PhotoPath nvarchar (510)

As

UPDATE Employees

SET

LastName = @LastName,

FirstName = @FirstName,

Title = @Title,

TitleOfCourtesy = @TitleofCourtesy,

Birthdate = @BirthDate,

HireDate = @HireDate,

Address = @Address,

City = @City,

Regin = @Regin,

PostalCode = @PostCode,

Country = @Country,

HomePhone = @HomePhone,

Extension = @Extension,

Photo = @Photo

Notes = @Notes,

ReportsTo = @ReportsTo,

Photopath = @PhotoPath

WHERE EmployeeID = @EmployeeID


Using the two stored procedures above saves me a lot of time. In particular, after changing the table structure, the process of restructuring individual stored procedures. You can overwrite these two programs to automatically generate other stored procedures.



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.