Stored Procedure in SQL Server
How can I make a Stored Procedure in SQL Server
Know the answer? Post it. Somebody with the same question will find it here.
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questions
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend5days leftRegister
Mamta MRank #268 · 9.4K reputationPosted Oct 9, 2011, 8:26 PM EDT
Pravin MoreRank #1,419 · 1.2K reputationPosted Oct 9, 2011, 8:30 PM EDT
here is simple syntax for stored procedure in sql server.
use databasename
CREATE PROCEDURE Procedure_Name
-- Add the parameters for the stored procedure here if you want
@Param1 Datatype_For_Param1,
@Param2 Datatype_For_Param2
AS
BEGIN
SELECT * from TableName with(nolock)
END
GO
call it like..... exec Procedure_Name param1_value param2_value.
Thanx,
Pravin.
Prabhu RajaRank #861 · 2.6K reputationPosted Oct 9, 2011, 8:51 PM EDT
I had given a sample format of stored procedures,
create procedure procedurename
@userid int, -- Parameters to procedure
@username varchar(50)
Asbegin
//Your Queries Here
End
Execute As,
EXEC Procedurename parameter1,parameter2,...
And also Read this Documentations
How to: Create a Stored Procedure (SQL Server Management Studio)
SQL Stored Procedures
Pravin Patil60 reputationPosted Oct 9, 2011, 10:39 PM EDT
CREATE PROCEDURE Procedure_Name
@i_parameter1 DataType
,@i_parameter2 DataType
AS
BEGIN TRY
'Actual Processing
END TRY
BEGIN CATCH
'Error Handling
END CATCH
Where DataType could be int, varchar, datetime etc. You can do the error handling in the catch block. This feature is available in SQL 2005 onwards.
Also there is concept of OUT variable in SQL. Whatever value is stored in OUT variable that an be used even after the execution of the Stored Procedure. Just mention OUT after the name of the variable. For more information on OUT parameter visit http://msdn.microsoft.com/en-us/library/ms378108(v=sql.90).aspx
Hope this helps.
All the best.