Insert Query in a SQL Server Stored Procedure Example – C# Programming Example
SQL

Insert Query in a SQL Server Stored Procedure Example

How to Insert Data Using Stored Procedure in SQL Server

How to insert rows with an INSERT query from a stored procedure with SQL Server? What option to display the number of rows inserted by the stored procedure? How to write a SQL Server procedure with an INSERT?

First, check beforehand that the example table does not exist, if it exists then we delete it with the DROP TABLE command. Then create the CLIENTS table which will be used to insert the lines.

How to write a SQL Server stored procedure with an INSERT command?

Here too, start by checking if the procedure already exists in the database and delete it if necessary. In a second step, create the stored procedure uspInsertClient to insert in the table of customers two fields passed in parameters, the name and the city.

How not to display the number of rows affected by the INSERT query in the stored procedure?

The “SET NOCOUNT ON” command tells SQL Server not to return the number of affected rows, in our case inserted by the INSERT SQL statement. Thus, if the option is activated, ie “SET NOCOUNT ON”, the number of lines is not reassembled, as in the example below:

Finally check the result of the INSERT query with a SELECT command on the Clients table.

 

 

 

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: