SQL Server Insert, Retrieve, Update, Delete Operations using Stored Procedures Keywords insert update delete using stored procedure,stored procedure with example,stored procedure example. In database, you need to do Insert, 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. This section of topics describes the techniques that LINQ to SQL provides for customizing insert, read, update, and delete operations in your application. The term Bulk Insert usually refers to the SQL Server specific ultra fast bcp based SqlBulkCopy implementation. It is built on top of IRowsetFastLoad. You perform Insert, Update, and Delete operations in LINQ to SQL by adding, changing, and removing objects in your object model. By default, LINQ to SQL translates. Insert Operation. We can insert records into the tables using stored procedure by passing data in input parameters. Below code is used to insert record in the table Employee using stored procedure CREATE TABLE Employee. Emp. ID int primary key, Name varchar5. January 25, 2015. Linqer 4. 5. 5 for. NET 4. 5. Entity Framework 6 support. Expose Edit popup menu for SQL and LINQ editors. Allow SQL to LINQ conversion in a different. Here Mudassar Ahmed Khan has explained how to Insert, Update, Edit and Delete record in GridView using SqlDataSource control in ASP. Net. In order to perform Insert. Tutorial on writting various kinds of database updatemanipulation queries in LinQ To SQL syntax with details code examples. Hi Vinz, mabuhay, and thank you for your blog It has been very helpful so far. Assuming that Im creating an insert form similar to the one at the top, but with. Insert Update In Linq To Sql Update' title='Insert Update In Linq To Sql Update' />Address varchar1. Insert into EmployeeEmp. ID,Name,Salary,Address Values1,Mohan,1. Delhi. Insert into EmployeeEmp. ID,Name,Salary,Address Values2,Asif,1. Delhi. Insert into EmployeeEmp. ID,Name,Salary,Address Values3,Bhuvnesh,1. Noida. SELECT FROM Employee CREATE PROCEDURE uspInsert. Employee. flag bit output, return 0 for fail,1 for success. Name varchar5. 0. Address varchar1. BEGIN TRANSACTION. Insert into EmployeeEmp. ID,Name,Salary,Address ValuesEmp. ID,Name,Salary,Address. IF TRANCOUNT 0. BEGIN commit TRANSACTION. IF TRANCOUNT 0. BEGIN rollback TRANSACTION. END Execute above created procedure to insert rows into table. Declare flag bit. EXEC uspInsert. Employee flag output,1,Deepak,1. Noida. print Successfully inserted. There is some error Execute above created procedure to insert rows into table. Declare flag bit. EXEC uspInsert. Employee flag output,4,Deepak,1. Noida. print Successfully inserted. There is some error now see modified table. Select from Employee Retrieve Operation. We can retrieve data from one or more tablesviews 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 first we Insert data in the table. Insert into EmployeeEmp. ID,Name,Salary,Address Values1,Mohan,1. Delhi. Insert into EmployeeEmp. ID,Name,Salary,Address Values2,Asif,1. Delhi. Insert into EmployeeEmp. ID,Name,Salary,Address Values3,Bhuvnesh,1. Noida. Now we create a procedure to fetch data. CREATE PROCEDURE uspSelect. Employee. Select from Employee ORDER By Emp. ID Execute above created procedure to fetch data. Select. Employee Update Operation. We can update records of the tables using stored procedure by passing data in input parameters. Below code is used to update a table Employee using stored procedure CREATE PROCEDURE uspUpdate. Employee. flag bit output, return 0 for fail,1 for success. Address varchar1. BEGIN TRANSACTION. Update Employee set SalarySalary, AddressAddress. Where Emp. IDEmp. ID. IF TRANCOUNT 0. BEGIN commit TRANSACTION. IF TRANCOUNT 0. BEGIN rollback TRANSACTION. END Execute above created procedure to update table. Declare flag bit. EXEC uspUpdate. Employee flag output,1,2. Noida. if flag1 print Successfully updated. There is some error now see updated table. Select from Employee Delete Operation. We can delete records of the tables using stored procedure by passing data in input parameters. Below code is used to update a table Employee using stored procedure CREATE PROCEDURE uspDelete. Employee. flag bit output, return 0 for fail,1 for success. BEGIN TRANSACTION. Delete from Employee Where Emp. IDEmp. ID set flag1. IF TRANCOUNT 0. BEGIN commit TRANSACTION. IF TRANCOUNT 0. BEGIN rollback TRANSACTION. END Execute above created procedure to delete rows from table. Declare flag bit. EXEC uspDelete. Employee flag output, 4. Successfully deleted. There is some error now see modified table. Select from Employee Note. In stored procedure we use output parameter to return multiple values. 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 basic Insert, Retrieve, Update, Delete Operations using Stored Procedures. I hope after reading this article you will be know how to implement these operations using stored procedure. I would like to have feedback from my blog readers. Please post your feedback, question, or comments about this article. Simple Insert Update and Delete Triggers in SQL Server with example. In this article I will explain with simple examples, how to write Insert, Update and Delete Triggers in SQL Server. This tutorial is applicable for all versions of SQL Server i. Database. I have made use of the following table Customers with the schema as follows. I have already inserted few records in the table. Below is the Customer. Logs table which will be used to log the Trigger actions. Note You can download the database table SQL by clicking the download link below. Triggers. Triggers are database operations which are automatically performed when an action such as Insert, Update or Delete is performed on a Table or a View in database. Triggers are associated with the Table or View directly i. Triggers. Types of Triggers. There are two types of Triggers. After and Instead of Triggers. After Triggers. These triggers are executed after an action such as Insert, Update or Delete is performed. Instead of Triggers. These triggers are executed instead of any of the Insert, Update or Delete operations. For example, lets say you write an Instead of Trigger for Delete operation, then whenever a Delete is performed the Trigger will be executed first and if the Trigger deletes record then only the record will be deleted. Algorithmic Trading Software Open Source there. After Triggers. Now I will explain you with examples the After Triggers for Insert, Update and Delete operations. Insert Trigger. Below is an example of an After Insert Trigger. Whenever a row is inserted in the Customers Table, the following trigger will be executed. The newly inserted record is available in the INSERTED table. The following Trigger is fetching the Customer. Id of the inserted record and the fetched value is inserted in the Customer. Logs table. CREATETRIGGERdbo. CustomerINSERT ONdbo. CustomersAFTERINSERTASBEGIN SETNOCOUNTON DECLARECustomer. Id. INT SELECTCustomer. IdINSERTED. Customer. Id FROMINSERTED INSERTINTOCustomer. Logs VALUESCustomer. Id,InsertedENDUpdate Trigger. Below is an example of an After Update Trigger. Whenever a row is updated in the Customers Table, the following trigger will be executed. The updated record is available in the INSERTED table. The following Trigger is fetching the Customer. Id of the updated record. In order to find which column is updated, you will need to use UPDATE function and pass the Column name of the Table to it. The UPDATE function will return TRUE for a Column if its value was updated else it will return false. Finally based on which column of the record has been updated a record containing the Customer. Id and the appropriate action is inserted in the Customer. Logs table. CREATETRIGGERdbo. CustomerUPDATE ONdbo. CustomersAFTERUPDATEASBEGIN SETNOCOUNTON DECLARECustomer. Id. INT DECLAREAction. VARCHAR5. 0 SELECTCustomer. IdINSERTED. Customer. Id FROMINSERTED IFUPDATEName BEGIN SETActionUpdated Name END IFUPDATECountry BEGIN SETActionUpdated Country END INSERTINTOCustomer. Logs VALUESCustomer. Id,ActionENDDelete Trigger. Below is an example of an After Delete Trigger. Whenever a row is delete in the Customers Table, the following trigger will be executed. The deleted record is available in the DELETED table. The following Trigger is fetching the Customer. Id of the deleted record and the fetched value is inserted in the Customer. Logs table. CREATETRIGGERdbo. CustomerDELETE ONdbo. CustomersAFTERDELETEASBEGIN SETNOCOUNTON DECLARECustomer. Id. INT SELECTCustomer. IdDELETED. Customer. Id FROMDELETED INSERTINTOCustomer. Logs VALUESCustomer. Id,DeletedENDThe following screenshot displays the Log table after the above Triggers were executed. Instead Of Triggers. Below is an example of an Instead Of Delete Trigger. Whenever anyone tries to delete a row from the Customers table the following trigger is executed. Inside the Trigger, I have added a condition that if record has Customer. Id value 2 then such a record must not be deleted and an error must be raised. Also a record is inserted in the Customer. Logs table. If the Customer. Id value is not 2 then a delete query is executed which deletes the record permanently and a record is inserted in the Customer. Logs table. CREATETRIGGERdbo. CustomerInstead. Of. DELETE ONdbo. CustomersINSTEADOFDELETEASBEGIN SETNOCOUNTON DECLARECustomer. Id. INT SELECTCustomer. IdDELETED. Customer. Id FROMDELETED IFCustomer. Id 2 BEGIN RAISERRORMudassar Khans record cannot be deleted,1. ROLLBACK INSERTINTOCustomer. Logs VALUESCustomer. Id,Record cannot be deleted. END ELSE BEGIN DELETEFROMCustomers WHERECustomer. IdCustomer. Id INSERTINTOCustomer. Logs VALUESCustomer. Id,Instead Of Delete ENDENDThe following error message shown when record with Customer. Id 2 is deleted. The following screenshot displays the Log table after the Instead Of Trigger is executed.