Which is the correct syntax for Stored procedure in SQL server ?
which one is correct ???
1. CREATE PROCEDURE procedure_name
AS
sql_statement
GO;
OR
2. CREATE PROCEDURE [schema_name].procedure_name
@parameter_name data_type,
....
parameter_name data_type
AS
BEGIN
-- SQL statements
-- SELECT, INSERT, UPDATE, or DELETE statement
END

Brahma Prakash ShuklaRank #633 · 3.8K reputationPosted Nov 8, 2022, 2:16 PM EST
The syntax to create a stored procedure in SQL Server (Transact-SQL) is: CREATE { PROCEDURE | PROC } [schema_name.] procedure_name [ @parameter [type_schema_name.] datatype [ VARYING ] [ = default ] [ OUT | OUTPUT | READONLY ] , @parameter [type_schema_name.]
Vishal YelveRank #178 · 13.6K reputationPosted Nov 9, 2022, 2:02 AM EST
CREATE PROCEDURE [schema_name].procedure_name @parameter_name data_type, .... parameter_name data_type AS BEGIN -- SQL statements -- SELECT, INSERT, UPDATE, or DELETE statement ENDVishal JoshiRank #377 · 6.7K reputationPosted Nov 9, 2022, 2:32 AM EST
Hello
Both syntaxes are correct. there are several properties we can configure during create procedure. like, parameterize, with output variable with NoCount, etc.
So, it depends on the requirements we can write a stored procedure. below is the default syntax:
SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO -- ============================================= -- Author: -- Create date: -- Description: -- ============================================= CREATE PROCEDURE -- Add the parameters for the stored procedure here = , = AS BEGIN -- SET NOCOUNT ON added to prevent extra result sets from -- interfering with SELECT statements. SET NOCOUNT ON; -- Insert statements for procedure here SELECT , END GOSatya KarkiRank #78 · 24.6K reputationPosted Nov 9, 2022, 5:27 AM EST
Hi, both Syntaxes are correct, Based on the need it depends on which one to use. The first one you can use when you don't need to pass parameters in the query such as SQL queries to select all values without where condition(s). The second one you can use when you need a parameterized query.
You can check the below link for a sample.
https://www.w3schools.com/sql/sql_stored_procedures.asp
Rajeev KumarRank #1,334 · 1.3K reputationPosted Mar 14, 2023, 3:44 AM EDT
CREATE procedure [dbo].[Usp_GetArticleDealDiscount]
@Articleid int
AS
Begin
Declare @SubscriptionPublishedDate datetime,@NetPriceAfterExchangeRate decimal(12,2),@JournalTypeid int
-- SQL statements -- SELECT, INSERT, UPDATE, or DELETE statement END