SQL Server tool for writing stored procedures (1)

Source: Internet
Author: User
Tags datetime
ENet Institute of Technology, All Rights Reserved
Many stored procedures are often written during database system development. To unify the format and simplify the development process, I wrote some stored procedures to automatically generate stored procedures. The following is a brief introduction to them. One is used to generate the Insert process, and the other is used to generate the Update process.

Sp_GenInsert
After this process is run, it generates a complete Insert process for the given table. If the original table has an ID column, you must manually delete the SET IDNTITY_INSERT ON statement generated during the process.

Syntax:
Sp_GenInsert <Table Name>, <Stored Procedure Name>
Take the northwind database as an example
Sp_GenInsert 'ployees', 'ins _ Employees'
An Insert stored procedure is generated. You can use it for further development.

Sp_GenUpdate
It generates an update stored procedure for a table. Syntax:
Sp_GenUpdate <Table Name>, <Primary Key>, <Stored Procedure Name>
Take the northwind database as an example
Sp_GenUpdate 'ployees', 'employeeid', 'upd _ Employees'
The following stored procedure is generated after running:
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 these two stored procedures saves me a lot of time. Especially when the table structure is changed and each stored procedure is re-constructed. You can rewrite 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.