You are on page 1of 7

18/05/13

CRUD Operations using Stored Procedures


SQL Server 2012, 2008 - Tutorial, Article, Archive, Pdf, Handy Tricks, Training, Code Snippets, Reference Manual, T-SQL, PL/SQL, Development, Administration, Performance Tuning, Backup, Recovery, Reporting, Analysis, Security, XML, CLR Objects Analysis, Security, XML, CLR Objects Manual, T-SQL, PL/SQL, Development, Administration, Performance Tuning, Backup, Recovery, Reporting,

ot Net ot Net Tricks Tricks


Handy Tricks For Beginners & Professionals Handy Tricks For Beginners & Professionals Home SQL Server Tutorial C#

" Coding , A Rhythmic Literary Job " " Coding , A Rhythmic Literary Job "

SQL Server 2012, 2008 - Tutorial, Article, Archive, Pdf, Handy Tricks, Training, Code Snippets, Reference Asp.Net MVC LINQ Windows App Training Projects FAQ Advertise About Me Contact

CRUD Operations using Stored Procedures


Po s t e d By : Sh a ile n d ra Ch a u h a n , 2 1 A p r 2 0 1 1 V e rs io n Su p p o rt : SQL Se rv e r 2 0 0 5 , 2 0 0 8, 2 0 1 2 Ke y w o rd s : CRUD Op e ra t io n u s in g St o re d Pro c e d u re , St o re d p ro c e d u re w it h e x a m p le p d f Up d a t e d On : 0 8 Oc t 2 0 1 2

Tips & Tricks Subscription For latest article subscribe by Email

Submit Follow @

In database, CRUD stands for 4 database operations: Create, Retrieve, Update and Delete. If we want to make a
reliable and high performance system then these four operations must be implemented by stored procedures. Stored procedure also prevents Sql Injection attacks and reduce network traffic. For more about stored procedure refer the article Stored Procedure Plan Recompilation and Performance Tuning. Tuning .Net Interview Book (Coming Soon..)

Create Operation
We can create temporary table(s) using stored procedure for intermediate data manipulation. Below code is used to remove duplicate records from tables using procedure. 1 . C R E A T ET A B L EE m p l o y e e _ T e s t

2 . ( 3 . 4 . 5 . E m p _ I Di n ti d e n t i t y ,E m p _ N a m ev a r c h a r( 5 5 )N U L L , E m p _ S a ld e c i m a l( 1 0 ,2 )N U L L , E m p _ D e s i g n a t i o nv a r c h a r( 2 0 )N U L L Join Training Programs in Noida

6 . ) 7 . G o 8 . I N S E R TI N T O E m p l o y e e _ T e s t ( E m p _ N a m e , E m p _ S a l , E m p _ D e s i g n a t i o n ) V A L U E S ( ' A m i t ' , 1 2 0 0 0 , ' S E ' ) 9 . I N S E R TI N T O E m p l o y e e _ T e s t ( E m p _ N a m e , E m p _ S a l , E m p _ D e s i g n a t i o n ) V A L U E S ( ' A m i t ' , 1 2 0 0 0 , ' S E ' ) 1 0 . I N S E R TI N T O E m p l o y e e _ T e s t ( E m p _ N a m e , E m p _ S a l , E m p _ D e s i g n a t i o n ) V A L U E S ( ' M o h a n ' , 1 5 0 0 0 , ' S E ' ) 1 1 . I N S E R TI N T O E m p l o y e e _ T e s t ( E m p _ N a m e , E m p _ S a l , E m p _ D e s i g n a t i o n ) V A L U E S ( ' M o h a n ' , 1 5 0 0 0 , ' S E ' ) 1 2 . I N S E R TI N T O E m p l o y e e _ T e s t ( E m p _ N a m e , E m p _ S a l , E m p _ D e s i g n a t i o n ) V A L U E S ( ' R i y a z ' , 1 6 0 0 0 , ' S E ' ) 1 3 . I N S E R TI N T O E m p l o y e e _ T e s t ( E m p _ N a m e , E m p _ S a l , E m p _ D e s i g n a t i o n ) V A L U E S ( ' M o n u ' , 2 7 0 0 0 , ' S S E ' ) 1 4 . I N S E R TI N T O E m p l o y e e _ T e s t ( E m p _ N a m e , E m p _ S a l , E m p _ D e s i g n a t i o n ) V A L U E S ( ' A m i t ' , 1 2 0 0 0 , ' S E ' ) 1 5 . G O 1 6 . N o ws e et a b l e 1 7 . S E L E C T*F R O Md b o . E m p l o y e e _ T e s t

C# 5.0 Asp.Net 4.5 MVC4 WCF 4.5 jQuery 1.8+ LINQ Entity Framework SQL Server 2012 Windows Phone8
Search Articles For finding articles enter search terms

Search Browse By Category .Net Framework Ajax 1 2 Ado.Net 6

Asp.Net 11 1 C 5 6 4 1

Asp.Net Web Page C# 15 CSS 5

C# Windows Apps

Dependency Injection 2 JQuery LINQ 6 1

Entity Framework JavaScript Knockout MVC 41 1 . c r e a t ep r o c e d u r eu s p _ R e m o v e d u p OOPS 3 3 5

Fluent Nhibernate 4

Nhibernate

SQL Server 45 1 SVN 3 1

2 . a s 3 . b e g i n 4 . C R E A T ET A B L E# t e m p E m p d a t a

SQL Server 2012 TFS WCF 1 3

Visual Studio Web API 4

5 . ( 6 . E m p _ N a m ev a r c h a r( 5 5 )N U L L ,

www.dotnet-tricks.com/Tutorial/sqlserver/I3W8010412-CRUD-Operations-using-Stored-Procedures.html

1/7

18/05/13
7 . 8 . E m p _ S a ld e c i m a l( 1 0 ,2 )N U L L , E m p _ D e s i g n a t i o nv a r c h a r( 2 0 )N U L L

CRUD Operations using Stored Procedures


Web Service 1 Windows Phone Apps 6

9 . ) 1 0 . I d e n t i f ya n ds a v ed i s t i n c td a t ai n t ot e m p o r a r yt a b l e 1 1 . I N S E R TI N T O# t e m p E m p d a t aS E L E C TD I S T I N C TE m p _ N a m e , E m p _ S a l ,E m p _ D e s i g n a t i o nF R O M E m p l o y e e _ T e s t 1 2 . D e l e t eo r i g i n a lt a b l ed a t ab yu s i n gT r u n c a t ec o m m a n d ,s i n c ei th a sE m p _ I Da s i d e n t i yc o l 1 3 . T R U N C A T Et a b l eE m p l o y e e _ T e s t 1 4 . N o wi n s e r td a t af r o mt e m pt a b l et oo r i g i n a lt a b l e 1 5 . I N S E R TI N T OE m p l o y e e _ T e s t ( E m p _ N a m e , E m p _ S a l , E m p _ D e s i g n a t i o n )S E L E C T E m p _ N a m e , E m p _ S a l , E m p _ D e s i g n a t i o nF R O M# t e m p E m p d a t a 1 6 . N o wd r o pt e m pt a b l e 1 7 . d r o pT A B L E# t e m p E m p d a t a 1 8 . e n d

Recent Articles Dependency Dependency Injection Injection in in ASP.NET ASP.NET MVC MVC 4 4 using using Unity Unity Dependency Injection in ASP.NET MVC 4 What What is is IoC IoC Container Container or or DI DI Container Container using Unity How to find a string within a How to find a string within a jQuery jQuery or or What is IoC Container or DI Container javascript javascript How to find a string within a jQuery or How How to to pass pass complex complex object object to to Web Web Api Api javascript and and MVC MVC How to pass complex object to Web Api Implementation Implementation of of Dependency Dependency Injection Injection and MVC Pattern Pattern Implementation of Dependency Injection Difference Difference between between ASP.NET ASP.NET MVC MVC and and Pattern ASP.NET Web API ASP.NET Web API Difference between ASP.NET MVC and What What is is Web Web API API and and why why to to use use it it ? ? ASP.NET Web API Understanding Understanding virtual, virtual, override override and and new new What is Web API and why to use it ? keyword keyword Understanding virtual, override and new Web Web API API CRUD CRUD Operations Operations with with Knockout Knockout keyword Knockout Knockout CRUD CRUD Operations Operations using using MVC4 MVC4 Web API CRUD Operations with Knockout Knockout CRUD Operations using MVC4 Most Popular Articles Understanding Understanding Inversion Inversion of of Control, Control, DI DI and and SL SL Understanding Inversion of Control, DI Difference Difference between between WCF WCF and and Web Web API API and SL and and WCF WCF REST REST Difference between WCF and Web API RenderPartial RenderPartial vs vs RenderAction RenderAction vs vs Partial Partial and WCF REST vs vs Action Action RenderPartial vs RenderAction vs Partial Understanding Understanding Model Model View View Controller Controller in in vs Action Asp.Net Asp.Net MVC MVC Understanding Model View Controller in MVC4 MVC4 performance performance optimization optimization Asp.Net MVC bundling bundling & & minifiy minifiy MVC4 performance optimization Routing Routing in in Asp.Net Asp.Net MVC MVC with with example example bundling & minifiy Difference Difference between between Generics Generics and and Routing in Asp.Net MVC with example Collections Collections Difference between Generics and Server Server Side Side Model Model Validation Validation in in MVC MVC Collections Razor Razor Server Side Model Validation in MVC CRUD CRUD Operations Operations using using jQuery jQuery dialog dialog and and Razor EF EF CRUD Operations using jQuery dialog and SQL SQL Joins Joins with with C# C#LINQ LINQ EF SQL Joins with C# LINQ

1 .

E x e c u t ea b o v ec r e a t e dp r o c e d u r et or e m o v ed u p l i c a t er e c o r d s

2 . e x e cu s p _ R e m o v e d u p

1 .

n o ws e eo r i g i n a lt a b l e

2 . S e l e c t*f r o mE m p l o y e e _ T e s t

Insert Operation
We can insert records into the table(s) using stored procedure by passing data in input parameters. Below code is used to insert record in the table "Employee" using stored procedure 1 . C R E A T ET A B L EE m p l o y e e

2 . ( 3 . 4 . 5 . E m p I Di n tp r i m a r yk e y ,N a m ev a r c h a r ( 5 0 ) , S a l a r yi n t , A d d r e s sv a r c h a r ( 1 0 0 )

6 . )

1 .

I n s e r ti n t oE m p l o y e e ( E m p I D , N a m e , S a l a r y , A d d r e s s )V a l u e s ( 1 , ' M o h a n ' , 1 6 0 0 0 , ' D e l h i ' )

2 . I n s e r ti n t oE m p l o y e e ( E m p I D , N a m e , S a l a r y , A d d r e s s )V a l u e s ( 2 , ' A s i f ' , 1 5 0 0 0 , ' D e l h i ' ) 3 . I n s e r ti n t oE m p l o y e e ( E m p I D , N a m e , S a l a r y , A d d r e s s ) V a l u e s ( 3 , ' B h u v n e s h ' , 1 9 0 0 0 , ' N o i d a ' ) 4 . S e et a b l e 5 . S E L E C T*F R O ME m p l o y e e

www.dotnet-tricks.com/Tutorial/sqlserver/I3W8010412-CRUD-Operations-using-Stored-Procedures.html

2/7

18/05/13

CRUD Operations using Stored Procedures

1 .

C R E A T EP R O C E D U R Eu s p _ I n s e r t E m p l o y e e

2 . @ f l a gb i to u t p u t , -r e t u r n0f o rf a i l , 1f o rs u c c e s s 3 . @ E m p I Di n t , 4 . @ N a m ev a r c h a r ( 5 0 ) , 5 . @ S a l a r yi n t , 6 . @ A d d r e s sv a r c h a r ( 1 0 0 ) 7 . A S 8 . B E G I N 9 . 1 0 . B E G I NT R A N S A C T I O N B E G I NT R Y

1 1 . I n s e r ti n t oE m p l o y e e ( E m p I D , N a m e , S a l a r y , A d d r e s s ) V a l u e s ( @ E m p I D , @ N a m e , @ S a l a r y , @ A d d r e s s ) 1 2 . s e t@ f l a g = 1 ;

1 3 . I F@ @ T R A N C O U N T>0 1 4 . 1 5 . 1 6 . B E G I Nc o m m i tT R A N S A C T I O N ; E N D E N DT R Y

1 7 . B E G I NC A T C H 1 8 . I F@ @ T R A N C O U N T>0 1 9 . 2 0 . 2 1 . 2 2 . B E G I Nr o l l b a c kT R A N S A C T I O N ; E N D s e t@ f l a g = 0 ; E N DC A T C H

2 3 . E N D

1 .

E x e c u t ea b o v ec r e a t e dp r o c e d u r et oi n s e r tr o w si n t ot a b l e

2 . D e c l a r e@ f l a gb i t 3 . E X E Cu s p _ I n s e r t E m p l o y e e@ f l a go u t p u t , 1 , ' D e e p a k ' , 1 4 0 0 0 , ' N o i d a ' 4 . i f@ f l a g = 1 5 . p r i n t' S u c c e s s f u l l yi n s e r t e d '

6 . e l s e 7 . p r i n t' T h e r ei ss o m ee r r o r '

1 .

E x e c u t ea b o v ec r e a t e dp r o c e d u r et oi n s e r tr o w si n t ot a b l e

2 . D e c l a r e@ f l a gb i t 3 . E X E Cu s p _ I n s e r t E m p l o y e e@ f l a go u t p u t , 4 , ' D e e p a k ' , 1 4 0 0 0 , ' N o i d a ' 4 . i f@ f l a g = 1 5 . p r i n t' S u c c e s s f u l l yi n s e r t e d '

6 . e l s e 7 . p r i n t' T h e r ei ss o m ee r r o r '

1 .

n o ws e em o d i f i e dt a b l e

www.dotnet-tricks.com/Tutorial/sqlserver/I3W8010412-CRUD-Operations-using-Stored-Procedures.html

3/7

18/05/13
2 . S e l e c t*f r o mE m p l o y e e

CRUD Operations using Stored Procedures

Retrieve Operation
We can retrieve data from one or more tables/views with the help of join, using stored procedure. We can put multiple sql statements with in a single stored procedure. Below code is used to fetch data from a table "Employee" using stored procedure 1 . -f i r s tw eI n s e r td a t ai nt h et a b l e

2 . I n s e r ti n t oE m p l o y e e ( E m p I D , N a m e , S a l a r y , A d d r e s s )V a l u e s ( 1 , ' M o h a n ' , 1 6 0 0 0 , ' D e l h i ' ) 3 . I n s e r ti n t oE m p l o y e e ( E m p I D , N a m e , S a l a r y , A d d r e s s )V a l u e s ( 2 , ' A s i f ' , 1 5 0 0 0 , ' D e l h i ' ) 4 . I n s e r ti n t oE m p l o y e e ( E m p I D , N a m e , S a l a r y , A d d r e s s ) V a l u e s ( 3 , ' B h u v n e s h ' , 1 9 0 0 0 , ' N o i d a ' ) 5 . g o 6 . N o ww ec r e a t eap r o c e d u r et of e t c hd a t a 7 . C R E A T EP R O C E D U R Eu s p _ S e l e c t E m p l o y e e 8 . A s 9 . S e l e c t*f r o mE m p l o y e eO R D E RB yE m p I D

1 .

E x e c u t ea b o v ec r e a t e dp r o c e d u r et of e t c hd a t a

2 . e x e cu s p _ S e l e c t E m p l o y e e

Update Operation
We can update records of the table(s) using stored procedure by passing data in input parameters. Below code is used to update a table "Employee" using stored procedure 1 . C R E A T EP R O C E D U R Eu s p _ U p d a t e E m p l o y e e

2 . @ f l a gb i to u t p u t , -r e t u r n0f o rf a i l , 1f o rs u c c e s s 3 . @ E m p I Di n t , 4 . @ S a l a r yi n t , 5 . @ A d d r e s sv a r c h a r ( 1 0 0 ) 6 . A S 7 . B E G I N 8 . 9 . 1 0 . 1 1 . 1 2 . B E G I NT R A N S A C T I O N B E G I NT R Y U p d a t eE m p l o y e es e tS a l a r y = @ S a l a r y ,A d d r e s s = @ A d d r e s s W h e r eE m p I D = @ E m p I D s e t@ f l a g = 1 ;

1 3 . I F@ @ T R A N C O U N T>0 1 4 . 1 5 . 1 6 . 1 7 . B E G I Nc o m m i tT R A N S A C T I O N ; E N D E N DT R Y B E G I NC A T C H

1 8 . I F@ @ T R A N C O U N T>0 1 9 . 2 0 . B E G I Nr o l l b a c kT R A N S A C T I O N ; E N D

www.dotnet-tricks.com/Tutorial/sqlserver/I3W8010412-CRUD-Operations-using-Stored-Procedures.html

4/7

18/05/13
2 1 . 2 2 . 2 3 . s e t@ f l a g = 0 ; E N DC A T C H E N D

CRUD Operations using Stored Procedures

1 .

E x e c u t ea b o v ec r e a t e dp r o c e d u r et ou p d a t et a b l e

2 . D e c l a r e@ f l a gb i t 3 . E X E Cu s p _ U p d a t e E m p l o y e e@ f l a go u t p u t , 1 , 2 2 0 0 0 , ' N o i d a ' 4 . i f@ f l a g = 1p r i n t' S u c c e s s f u l l yu p d a t e d ' 5 . e l s e 6 . p r i n t' T h e r ei ss o m ee r r o r '

1 .

n o ws e eu p d a t e dt a b l e

2 . S e l e c t*f r o mE m p l o y e e

Delete Operation
We can delete records of the table(s) using stored procedure by passing data in input parameters. Below code is used to update a table "Employee" using stored procedure 1 . C R E A T EP R O C E D U R Eu s p _ D e l e t e E m p l o y e e

2 . @ f l a gb i to u t p u t , -r e t u r n0f o rf a i l , 1f o rs u c c e s s 3 . @ E m p I Di n t 4 . A S 5 . B E G I N 6 . 7 . 8 . B E G I NT R A N S A C T I O N B E G I NT R Y D e l e t ef r o mE m p l o y e eW h e r eE m p I D = @ E m p I Ds e t@ f l a g = 1 ;

9 . I F@ @ T R A N C O U N T>0 1 0 . 1 1 . 1 2 . 1 3 . B E G I Nc o m m i tT R A N S A C T I O N ; E N D E N DT R Y B E G I NC A T C H

1 4 . I F@ @ T R A N C O U N T>0 1 5 . 1 6 . B E G I Nr o l l b a c kT R A N S A C T I O N ; E N D

1 7 . s e t@ f l a g = 0 ; 1 8 . E N DC A T C H 1 9 . E N D

1 .

E x e c u t ea b o v ec r e a t e dp r o c e d u r et od e l e t er o w sf r o mt a b l e

2 . D e c l a r e@ f l a gb i t 3 . E X E Cu s p _ D e l e t e E m p l o y e e@ f l a go u t p u t ,4 4 . i f@ f l a g = 1 5 . p r i n t' S u c c e s s f u l l yd e l e t e d '

www.dotnet-tricks.com/Tutorial/sqlserver/I3W8010412-CRUD-Operations-using-Stored-Procedures.html

5/7

18/05/13
6 . e l s e 7 . p r i n t' T h e r ei ss o m ee r r o r '

CRUD Operations using Stored Procedures

1 .

n o ws e em o d i f i e dt a b l e

2 . S e l e c t*f r o mE m p l o y e e

Note
1. In stored procedure we use output parameter to return multiple values. 2. Generally we use output parameter in stored procedure to get status of the operation as I used above "@flag" output parameter to get operations status whether these are successfully executed or not.

Summary
In this article I try to explain CRUD operation in Sql Server using Stored Procedure. I hope after reading this article you will be know how to implement CRUD operation using stored procedure. I would like to have feedback from my blog readers. Please post your feedback, question, or comments about this article.

Prev

Next

Recommended Articles !!
1. Different Types of SQL Joins 2. Introduction to SQL Server 3. SQL Server Exception Handling by TRYCATCH 4. Different Types of SQL Server Stored Procedures 5. Definition, Use of Group by and Having Clause 6. Different Types of SQL Keys 7. Basics of SQL Commands 8. Different Types of SQL Server Views 9. SQL Server Exceptions Working 10. SQL Integrity Constraints or Constraints

0 comments Leave a message...


Share

Newest

Community

No one has commented yet.

C o m m e n t fe e d

Su b s cri b e vi a e m a i l

What The ?

Who The ?

How The ?

Disclaimer !
6/7

www.dotnet-tricks.com/Tutorial/sqlserver/I3W8010412-CRUD-Operations-using-Stored-Procedures.html

18/05/13
Trying to understand the tips and tricks? Can't find a tutorial on it? Exhausted of seeing the awe-inspiring somewhere else and not knowing how it was done? This blog will suggest you how to do those awesomeness tips & tricks.

CRUD Operations using Stored Procedures


If you're looking to learn a trick or technique which you have seen Ultimately you come across to know new and fresh recipes. You're keen to learn those recipes but you don't know - how do that? Trust me, I have been there to help you for learning these new and fresh recipes. More.. This is my personal blog and having articles on .net and others .net related technologies. The opinions expressed here are my own and not belongs to my employer or other organization. I have listed my own learning

somewhere else. Let me know and I'll do my best to explain how it was done. If I don't know how to do, I'll take it as an exciting topic & I'll find upright resources and share. More..

experience on this blog.

Copyright 201 2-201 3. The content is copyright to Shailendra Chauhan and may not be reproduced on other websites without permission from the owner.

www.dotnet-tricks.com/Tutorial/sqlserver/I3W8010412-CRUD-Operations-using-Stored-Procedures.html

7/7

You might also like