A stored procedure is a sql query which could be executed at demand. A stored procedure could be used to delete, update, insert or select a record or set of records from database using any language. These procedures are stored in database. This a great way to save time and efforts because a single stored procedure could be called multiple times in an application which is opposite to writing inline select,delete,update or insert statements which may cause difficulty in maintaining the code as application grows.
The syntax for writing the stored procedure may vary from database to database but the main idea is the same.
Stored procedure don't require compilation each they are executed because the are compiled only once upon creation.
The following stored procedure inserts record in table
CREATE PROCEDURE dbo.Insert
@Name VARCHAR(150)
AS
BEGIN
INSERT INTO dbo.TableName
(
Name
)
VALUES
(
@Name
)
END
The following is the syntax to create the stored procedure which insets a record in the table in sql server
CREATE PROCEDURE [Insert]
@FirstName varchar(150)
AS
BEGIN
INSERT INTO dbo.TableName
(
FirstName
)
VALUES
(
@FirstName
)
END
Explanation
CREATE PROCEDURE is the key word which actually creates the stored procedure.
[Insert] This is the name of the stored procedure, It could be any string of characters or combination of characters and digits.
@FirstName is a parameter passed to stored procedure, this parameter is carrying the FirstName value inside it which we want to save in our table.
varchar defines that the parameter is of type int.
INSERT INTO defines the table name where we would save the record.
TableName The name of the table.
Sometimes we need to modify our stored procedure, to do so syntax is the same as for creating the stored procedure but we use the keyword alter procedure instead of create procedure.
The following is the syntax to alter the stored procedure which insets a record in the table in sql server
ALTER PROCEDURE [Insert]
@LastName varchar(150)
AS
BEGIN
INSERT INTO dbo.TableName
(
LastName
)
VALUES
(
@LastName
)
END
Explanation
ALTER PROCEDURE is the key word which alters or modifies the stored procedure.
[Insert] This is the name of the stored procedure, It could be any string of characters or combination of characters and digits.
@LastName is a parameter passed to stored procedure, this parameter is carrying the LastName value inside it which we want to save in our table.
varchar defines that the parameter is of type int.
INSERT INTO defines the table name where we would save the record.
TableName The name of the table.
Delete Stored Procedure SQL
For further question please mail: brainstormiert@gmail.com