Friday , December 27 2024

Sp(Stored Procedure) On SQL Server

 

You can keep the database-level code that you need to use every time in the application through “stored procedures” in the database.

You can achieve the same result by repeatedly sending the same code within the application. But SPs will be faster.

Because SPs are only compiled the first time they are run and receive an execution plan.

You can find the details of the execution plan in the article titled “What is Execution Plan On SQL Server“.

They will use this execution plan in their next work.

They will perform better because they will work without compilation.

But each time running with the same execution plan can sometimes lead to the parameter sniffing problem.

You can find details about parameter sniffing in the article “What is Parameter Sniffing“.

It will also help you develop a more understandable application by keeping the transactional logic of the application level in one place (within the sp in the database).

SPs can return results by taking parameters.

For example, you have a table with the Columns of Id and BookName, called Library.

You can create the table in your database with the following script.

 

After creating the table, create the SP with the following script.

 

The sp we have created takes the value of Id as a parameter and returns the BookName value that Id has.

We add two lines to our table using the following script.

 

When you run SP as follows, it will return a record like the one below.

Result:

yBook

Loading

About dbtut

We are a team with over 10 years of database management and BI experience. Our Expertises: Oracle, SQL Server, PostgreSQL, MySQL, MongoDB, Elasticsearch, Kibana, Grafana.

Leave a Reply

Your email address will not be published. Required fields are marked *