DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Please enter at least three characters to search
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Last call! Secure your stack and shape the future! Help dev teams across the globe navigate their software supply chain security challenges.

Modernize your data layer. Learn how to design cloud-native database architectures to meet the evolving demands of AI and GenAI workloads.

Releasing software shouldn't be stressful or risky. Learn how to leverage progressive delivery techniques to ensure safer deployments.

Avoid machine learning mistakes and boost model performance! Discover key ML patterns, anti-patterns, data strategies, and more.

Related

  • How to Restore a Transaction Log Backup in SQL Server
  • How to Attach SQL Database Without a Transaction Log File
  • A Deep Dive into Apache Doris Indexes
  • Spring Boot Sample Application Part 1: Introduction and Configuration

Trending

  • Understanding IEEE 802.11(Wi-Fi) Encryption and Authentication: Write Your Own Custom Packet Sniffer
  • Agentic AI for Automated Application Security and Vulnerability Management
  • Cookies Revisited: A Networking Solution for Third-Party Cookies
  • Ethical AI in Agile
  1. DZone
  2. Data Engineering
  3. Databases
  4. How to Generate and Use CRUD Stored Procedures in SQL Server

How to Generate and Use CRUD Stored Procedures in SQL Server

In this article, see how to generate and use CRUD stored procedures in SQL Server.

By 
Jordan Sanders user avatar
Jordan Sanders
·
Updated Feb. 24, 20 · Tutorial
Likes (3)
Comment
Save
Tweet
Share
39.9K Views

Join the DZone community and get the full member experience.

Join For Free

Most database systems operate on the basis of 4 of the simplest data manipulation operations that are called by the acronym CRUD. This acronym stands for CREATE, READ, UPDATE, and DELETE.

When developing and managing databases, you can use CRUD stored procedures to perform all data-related tasks. The benefit of such stored procedures is that, once they’re written once, they can be reused as many times as required, with no need to write new code each time. This is a great improvement over ad hoc SQL statements which should be written anew every time we use them.

Let’s look at each CRUD stored procedure in detail.

A Closer Look at CRUD Stored Procedures

Before moving forward, there’s one thing we want to say about naming CRUD stored procedures. It is usually a good practice to name them in such a way that each procedure contains the name of the table they’re applied to and also ends with the name of the operation they’re performing. This way, all procedures written for the same table will be grouped together and are much easier to search through.

However, it’s not mandatory at all and you can stick to any naming pattern you prefer.

Now, let’s look at the first procedure type.

You might also want to read: Collection: SQL Server Sample Databases

CREATE Procedures

These will execute an INSERT statement, creating a new record. Such procedures should accept one parameter for each column of the table.

SQL
 




x
18


 
1
IF OBJECT_ID('Sales.usp_Currency_Insert') IS NOT NULL BEGIN      DROP PROC Sales.usp_Currency_Insert  END  GO CREATE PROC Sales.usp_Currency_Insert      @CurrencyCode NCHAR(3),     @Name dbo.Name,     @ModifiedDate datetime AS      SET NOCOUNT ON      SET XACT_ABORT ON  
2
BEGIN TRAN
3
 
4
INSERT INTO Sales.Currency (CurrencyCode, Name, ModifiedDate)
5
SELECT @CurrencyCode, @Name, @ModifiedDate
6
 
7
/*
8
-- Begin Return row code block
9
 
10
SELECT CurrencyCode, Name, ModifiedDate
11
FROM   Sales.Currency
12
WHERE  CurrencyCode = @CurrencyCode AND Name = @Name AND ModifiedDate = @ModifiedDate
13
 
14
-- End Return row code block
15
 
16
*/
17
COMMIT
18
GO



READ Procedures

The READ procedure retrieves table records based on the primary key provided in the input parameter.

SQL
 




xxxxxxxxxx
1


 
1
IF OBJECT_ID('Sales.usp_Currency_Select') IS NOT NULL BEGIN      DROP PROC Sales.usp_Currency_Select  END GO CREATE PROC Sales.usp_Currency_Select     @CurrencyCode NCHAR(3),     @Name dbo.Name AS     SET NOCOUNT ON      SET XACT_ABORT ON  
2
BEGIN TRAN
3
 
4
SELECT CurrencyCode, Name, ModifiedDate 
5
FROM   Sales.Currency
6
WHERE  CurrencyCode = @CurrencyCode AND Name = @Name  
7
 
8
COMMIT
9
GO



UPDATE Procedures

These procedures use the primary key for a record specified in the WHERE clause to execute an UPDATE statement on a table. Just like CREATE procedures, it accepts one parameter for each table column.

SQL
 




xxxxxxxxxx
1
19


 
1
IF OBJECT_ID('Sales.usp_Currency_Update') IS NOT NULL BEGIN      DROP PROC Sales.usp_Currency_Update END  GO CREATE PROC Sales.usp_Currency_Update @CurrencyCode NCHAR(3), @Name dbo.Name, @ModifiedDate datetime AS      SET NOCOUNT ON      SET XACT_ABORT ON  
2
BEGIN TRAN
3
 
4
UPDATE Sales.Currency
5
SET    ModifiedDate = @ModifiedDate
6
WHERE  CurrencyCode = @CurrencyCode AND Name = @Name
7
 
8
/*
9
-- Begin Return row code block
10
 
11
SELECT ModifiedDate
12
FROM   Sales.Currency
13
WHERE  CurrencyCode = @CurrencyCode AND Name = @Name
14
 
15
-- End Return row code block
16
 
17
*/
18
COMMIT
19
GO



DELETE Procedures

This procedure will delete a row provided in the WHERE clause of the statement.

SQL
 




xxxxxxxxxx
1


 
1
IF OBJECT_ID('Sales.usp_Currency_Delete') IS NOT NULL BEGIN      DROP PROC Sales.usp_Currency_Delete END  GO CREATE PROC Sales.usp_Currency_Delete  @CurrencyCode NCHAR(3), @Name dbo.Name AS      SET NOCOUNT ON      SET XACT_ABORT ON  
2
BEGIN TRAN
3
 
4
DELETE
5
FROM   Sales.Currency
6
WHERE  CurrencyCode = @CurrencyCode AND Name = @Name
7
 
8
COMMIT
9
GO



Generating CRUD Procedures Using dbForge SQL Complete

Using the dbForge SQL Complete add-in that works both in SSMS and Visual Studio, we can generate CRUD procedures in a few clicks with a variety of options that allow configuring how exactly these procedures are generated. In this article, we’ll use SSMS to show SQL Complete’s functionality.

To generate CRUD procedure for a table, right-click the table, go to the SQL Complete menu, and click Script Table as CRUD:

Screenshot of dbForge SQL Complete


When this is done, a new SQL file will be opened. Here, you can see all CRUD operations for the table.

Screenshot of dbForge SQL Complete


Changing CRUD Generation Settings

To configure how dbForge SQL Complete generates CRUD, you would first need to go to the SQL Complete menu at the top of the window and click Options:

Screenshot of dbForge SQL Complete


In the Options window that will be opened as a result, go to the CRUD menu and click General:

Screenshot of dbForge SQL Complete


In this tab, you can specify which procedures include in the CRUD generation process and specify which column order to use — alphabetical or by ordinal number.

You can also configure each procedure separately by choosing the corresponding option in the CRUD menu. First of all, you can manually change the name of the generated procedures:

Name of generated procedures


Next, there are options unique to each procedure.

  • For SELECT, there is a Return all data if input parameters are null checkbox
  • For INSERT, you can specify whether to return the inserted row upon completion

A similar option is available for UPDATE — it allows you to choose whether you want the updated row to be returned.

There is no additional unique option for DELETE.

Finally, for each procedure, there is the Code template section. In this section, you can change how the code of the specified procedure is generated. In code templates, there are parameters provided in the format $name$ (for example, $schema$ or $columns$). By changing these parameters, you can modify the code of the generated procedure.

Conclusion

As you can see, implementing and managing the data manipulation process using CRUD commands is much more preferable to using ad hoc SQL statements. This can be done easily with the help of the dbForge SQL Complete add-in. However, working with CRUD is not its only functionality (and by a long stretch).

sql Database

Published at DZone with permission of Jordan Sanders. See the original article here.

Opinions expressed by DZone contributors are their own.

Related

  • How to Restore a Transaction Log Backup in SQL Server
  • How to Attach SQL Database Without a Transaction Log File
  • A Deep Dive into Apache Doris Indexes
  • Spring Boot Sample Application Part 1: Introduction and Configuration

Partner Resources

×

Comments
Oops! Something Went Wrong

The likes didn't load as expected. Please refresh the page and try again.

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends:

Likes
There are no likes...yet! 👀
Be the first to like this post!
It looks like you're not logged in.
Sign in to see who liked this post!