

SQL Server Database
Happiness is the best medicine.
Happiness is the best medicine.
Happiness is the best medicine.
Happiness is the best medicine.
Happiness is the best medicine.
Joins in SQL servers are used to query for retrieve data from 2 or more related tables. A Join clause is used to combine rows from one , two or more tables, based on a related column.
In SQL, a join operation combines rows from two or more tables based on a related column between them. The result of a join is a new table that contains only the rows where the join condition is met. There are several types of joins in SQL, including:
There are three types of JOINS.
1. CROSS JOIN
2. INNER JOIN
3. OUTER JOIN
Outer Joins are 3 types
1. Left Join or Left Outer Join
2. Right Join or Right Outer Join
3. Full Join or Full Outer Join
Considers Employees (tblEmployee) and Departments (tblDepartment) tables
Before post, we have seen joining 2 different tables - tblEmployees and tblDepartments. Have you ever thought of a need to join a table by itself? Consider tblEmployees table shown below.
Consider the Employees table below.
Stored Procedures:- A stored Procedures is a group of a T-SQL statement. If you have a situation, where you write the same query over and over again, You can save that specific query as a Stored Procedure and call it just by its name.
Syntax:- Create procedure procedures name
As begin
SQL statement
End
Execute a Stored Procedures:-
exec procedure name
Stored Procedures Example:-
The following SQL statement creates a stored procedure named "SelectAllEmployee" that selects all records from the "tblemployee" table
Create proc SelectAllEmployee
as
Begin
Select * from tblemployee
End
Emp_Name Emp_ID Address
Lalita 2 Noida
Ankit 1 Noida
Ankita 3 Delhi
Anku 4 US
XYZ 5 UK
Stored Procedure With Multiple Parameters:-
Multiple parameters is very easy. Just list each parameter and the data type separated by a comma as shown below.
The following SQL statement creates a stored procedure that selects Tblemployee from a particular address with a particular PostalCode from the "tblemployee" table:
Example
CREATE PROC SelectAllEmployee @address nvarchar(30), @Emp_Id nvarchar(10)
AS
SELECT * FROM tblemployeeWHERE address = @address AND Emp_Id = @Emp_Id
GO;
Execute the stored procedure above as follows:
Example
EXEC SelectAllEmployee @address = 'London', @Emp_Id = '01';
To view the stored procedure in text:
sp_helptext stored_proc name
To change the stored procedure use,
alter procedure proc_name
as
begin
SQL statement
end
Drop Procedure
Drop procedure proc_name
To Encrypt the text of the Store Procedure
CREATE PROC SelectAllEmployee @address nvarchar(30), @Emp_Id nvarchar(10)
with encryption
AS
begin
SELECT * FROM tblemployeeWHERE address = @address AND Emp_Id = @Emp_Id
GO;
Creating and execute Stored Procedure with output parameters
create procedure getemployeecoutbyaddress
@address varchar(20),
@Empcount int output
as begin
select @Empcount=count(Emp_ID) from tblemployee where Address=@address
end
To Execute the stored procedure with output parameters
declare @totalemployee int
exec getemployeecoutbyaddress 'noida',@totalemployee output
print @totalemployee
if you don't specified the output keyword, when execute the stored procedure @totalemployee value will be null
Sp_help procedure name:- View the information
About the stored procedure like parameters name, their data type etc. Sp_help can be use with database object like table view etc. Alternative you can also press Art+F1