introduction of store procedure in mssql server

Hello friend,

It is very bad habit and difficult to write separate sql query for each operation in sql query. The best solution for this problem is procedure .

Procedure is similar to function which we will learn in next chapter, is collection of code which can be executed at any time.

To create a procedure open sql server, log in and expand database and browse your database. Follow the picture:

create store procedure
create store procedure

Right click Stored Procedures and and select create store procedure . Write the following query to create a store procedure.

Create procedure [dbo].[sp_FetchAdminLogin]
(
@task varchar(300),
@ID int,
@UserName varchar(max),
@Password varchar(max)
)
as
begin
if(@task=’FetchAdmin’)
begin
select Id,UserName,Password from dbo.AdminLogin where UserName=@UserName and                              Password=@Password
end
end

General format to create a store procedure is:

create procedure procedure_name(parameter1,parameter2,..)

as

begin

query 1

query 2

…….

end

 

To execute the procedure write following query:

exec procedure_name parameter1,parameter2

Happy coding

 

How useful was this post?

Click on a star to rate it!

Average rating / 5. Vote count:

As you found this post useful...

Follow us on social media!

We are sorry that this post was not useful for you!

Let us improve this post!

Leave a Reply

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

%d bloggers like this: