How to Create Stored Procedures in SQL Server? – Easy and Fast (Example)


For those who like to program and create databases, they should know that there is an official Microsoft program which makes their task easier and more fun. This has very good functions (such as the one that allows you to connect two computers to a database) and a very friendly interface, so today you will learn how to create stored procedures in SQL Server.

And there is no one better than the Microsoft company to innovate on programming. Since they always use cutting-edge technology and procedures, bringing each of their users programs that will help them in their most difficult tasks, so that in the long run they become the programmers of the future, such as SQL server (which you can download from their page).

Create stored procedures in SQL Server

To get straight to the point, the first thing you should do to create stored procedures in SQL Server is to connect to any instance of the base engine in the call «Data Explorer», and successively after this expand it.

You may also be interested in:

Then you must expand the following three elements: first «Database», then the database called AdventuWorks 2012 and «Programming».

At the end of this you must right click on the section «Stored Procedures» so that the option comes out «New stored procedure» which you must select.

Then go to the menu called «Query”And press «Specify values ​​for query parameters», this will bring up a dialog box where you must specify values ​​for the displayed parameters.

Once all have been entered, click on «okay» to confirm. Right after you have to replace the statement called SELECT with the following: SELECT FirstName, LastName, Department FROM HumanResources.vEmployeeDepartmentHistory WHERE FirstName = @FirstName AND LastName = @LastName AND EndDate IS NULL;

sql blue cloud

You can test this instruction or syntax by going to the «Queries”And pressing the“Analyze”, If you put everything as it is, it should not throw an error. This will allow you to go to the menu again and hit «Run”To finally create the procedure.

To finish you just have to go back to the object explorer and right click on the option «Stored Procedures», Then in «To update”. And finally. To execute this procedure, right click on its name and proceed to press «Run”.

Using transact-SQL

With the above, you will be able to create stored procedures in SQL Server, but this is not the only procedure, you can also achieve your goal with the transact as follows:

Connect as in the previous one to the database engine that is in the file explorer, and then in the menu that appears called “File”Press «New query». Next, in the window that will appear, you must paste:

USE AdventureWorks2012; GO CREATE PROCEDURE HumanResources.uspGetEmployeesTest2 @LastName nvarchar (50), @FirstName nvarchar (50) AS SET NOCOUNT ON; SELECT FirstName, LastName, Department FROM HumanResources.vEmployeeDepartmentHistory WHERE FirstName = @FirstName AND LastName = @LastName AND EndDate IS NULL; GO

This is an example so it is logical that the values ​​should be changed by your own. At the end of the process, proceed to copy and paste the following into another query window: EXECUTE HumanResources.uspGetEmployeesTest2 N’Ackerman ‘, N’Pilar’;

– Or EXEC HumanResources.uspGetEmployeesTest2 @LastName = N’Ackerman ‘, @FirstName = N’Pilar’; GO – Or EXECUTE HumanResources.uspGetEmployeesTest2 @FirstName = N’Pilar ‘, @LastName = N’Ackerman’; GO

sql server logo

Once done, give «Run«And voila again you will have managed to create stored procedures in SQL Server quickly and easily.

Now with this, if you know everything you need to be able to create the procedures in SQL. Remember that this is a complex program so if you want to advance faster on larger issues you have to dedicate time and patience to learning.

You can do this with different online tutorials, but as a recommendation it is better that you go little by little, learning one thing a day, for example tomorrow you can read, how to create databases, tables and records, and the following week how to create, modify or delete a view, which are basic things and will help you.

How useful did you find this content?

Click on a star to rate!

Average score 0 / 5. Counting of votes: 0

So far, no votes. Be the first to rate this content.

I'm sorry this content was not useful for you!

Let me improve this content!

Tell me, how can I improve this content?

You may also be interested in:

Deja un comentario