SQL Server BasicsWhat is SQL Server database?What is RDBMS?What is Normalization?Why we use Denormalization?What_is_SQL?What is PL/SQL?Difference between SQL and PL/SQLDatabase TableOne to One RelationshipOne to Many RelationshipMany to Many RelationshipMany to One RelationshipString Data TypesNumber Data TypesDate Data TypesOther Data TypesCreate DatabaseDrop DatabaseCreating and Managing Users in SQL ServerCreate TableAlter TableDrop TableConstraints in SQL serverPrimary KeyForeign KeyUnique KeyCandidate KeyComposite KeyDifference between primary key and candidate keyPrimary key and foreign key relationshipSurrogate KeyCascading Referential Integrity ConstraintsSelf Referential Integrity ConstraintsInsert into statementInsert multiple recordsUpdate statementDelete statementTruncate statementDifference between Delete and TruncateAlias in SQL ServerSelect statementSelect DistinctSelect TopSelect IntoNull Functions(ISNULL(),NULLIF(),COALESCE())Sub QueryIdentity ColumnSequence objectDifference between sequence and identity columnSQL Server ClausesWHERE ClauseOrder By ClauseTop N ClauseGroup By ClauseHaving ClauseDifference between Where and HavingSQL Server OperatorsArithmetic OperatorsComparison OperatorsLogical OperatorsBitwise OperatorsAny OperatorsAll OperatorsUnion OperatorsUnion All OperatorsDifference between Union and Union AllIntersect OperatorExcept OperatorDifference between Except and IntersectJoinsInner JoinLeft JoinRight JoinFull JoinSelf JoinCross JoinViewsWhat are views?Create views using SSMSIndexed ViewsComplex ViewsCheck Option in ViewCheck Encryption in ViewSchema Binding Option in ViewRead-only ViewsUpdatable ViewsAdvantages and disadvantages of viewsCreate multiple views on one tableCan we implement index on views?Can we Perform Insert, update, delete operation on views?Stored Procedure and FunctionsWhat are Stored Procedures?Why we use stored procedures?Passing parameters to Stored procedureUser-Defined FunctionsDifference between UDF and Stored procedurePre-Defined Functions@@Indentity and Scope_IndentityNULLIF, ISNULL and COALESCE

What are Stored Procedures?

In SQL Server, a stored procedure is a named set of SQL statements that are stored in the database and can be executed repeatedly. Stored procedures in SQL Server are written using the Transact-SQL (T-SQL) language, which is a procedural language extension to SQL.

Here's an example to illustrate how stored procedures work in SQL Server:

Let's say we have a database table called "Employees" with columns such as "EmployeeID," "FirstName," "LastName," and "Salary." We want to create a stored procedure that retrieves the details of an employee based on their ID.

Creating the stored procedure:


CREATE PROCEDURE GetEmployeeDetails
    @EmployeeID INT
AS
BEGIN
    SELECT EmployeeID, FirstName, LastName, Salary
    FROM Employees
    WHERE EmployeeID = @EmployeeID
END

In this example, we're creating a stored procedure named "GetEmployeeDetails" that takes a single parameter, @EmployeeID, of type INT.

Executing the stored procedure:

Once the stored procedure is created, we can execute it using the EXEC statement.


EXEC GetEmployeeDetails @EmployeeID = 123

In this case, we're passing the value 123 as the @EmployeeID parameter to the stored procedure.

The stored procedure will execute the SQL statement inside it and return the result set. In this case, it will fetch the details of the employee with the specified ID from the "Employees" table.

Stored procedures can have multiple input parameters, output parameters, and even return result sets. They can also include control flow statements like conditionals (IF statements) and looping constructs (WHILE or CURSOR).

Benefits of using stored procedures in SQL Server include improved performance, code reusability, security, and encapsulation of business logic within the database.

Note: The above example demonstrates a basic usage of stored procedures in SQL Server. In practice, stored procedures can be much more complex and involve multiple SQL statements or even incorporate business logic beyond simple database queries.