Sei sulla pagina 1di 17

1.

Temporary Database

2.Temporary Table

3.Temporary Stored Procedure


Temporary Database
Tempdb is a system database used by SQL
Server to store temporary tables and
temporary stored procedures,

Tempdb database contains only temporary


objects

Tempdb is created every time SQL Server is


started.
●It stores temporary objects like temporary
tables and temporary stored procedure.

●Each time the SQL server service starts,


the tempdb is newly created by copying
from model database

●The tempdb default size is 8 MB when the


service starts.
Temporary Tables
● Local temporary table create use pound
sign(#).

●Local temporary tables are only available to


the current connection to the database for
the current user.

● Both types of temporary tables are created


in the system database tempdb.
Create
Create table #Hari(eno varchar(5),Name
varchar(10), deptno varchar(5))
Insert:
Insert into #Hari('101','Nana','30')
Select:
Select * from #Hari
Drop:
Drop table #Hari
● If two different users both create a #Yaks
table each will have their own copy of it

●Temporary table create inside a stored


procedure is automatically dropped when
the stored procedure finishes executing.

●Temporary tables can be created like any


table in SQL Server with a CREATE TABLE
or SELECT..INTO statement.
● If stored procedure A creates a temporary
table and calls stored procedure B
● B will be able to use the temporary table
that A created

●Temporary tables are usually pretty quick.


Since you are creating and deleting them on
the fly, they are usually only cached in
memory.
Global Temporary Tables

● Create the Global temporary table use


name with two pound signs.
Example: ##Employee is a global
temporary table

●Global temporary tables are available to


any connection once created

●Both types of temporary tables are created


in the system database tempdb.
Restrictions on the use of declared
temporary tables:

1. Declared temporary tables cannot be


specified in an ALTER TABLE, COMMENT,
CREATE TRIGGER, GRANT, LABEL,
LOCK, RENAME or REVOKE statement.

2. Declared temporary tables cannot be


specified as the parent table in referential
constraints
3.If a declared temporary table is referenced
in a CREATE INDEX or CREATE VIEW
statement, the index or view must be created
in SESSION (or library QTEMP).

●The QTEMP library is a library that the


system automatically provides for each job on
the system and is unique to each job on the
system

●Temporary tables can be created like any


table in SQL Server with a CREATE TABLE or
SELECT..INTO statement.
Create:
Create table ##people(eno
varchar(5),Ename varchar(10),dept
varchar(10))

Select into:
Select eno,Name into ##emp from
Employee
Stored procedure

A collection of batch of T-SQL statements &


control-of-flow statements
● stored under one name,executed as a

single unit
● pre-compiled object stored in the database

no time is spent on sending the query to the


server,parsing and compiling it.
●Developer specify a stored procedure in

terms of input and output variables.


create procedure empord
@val varchar(10)
As
begin
select firstname,lastname from employees
where employeeid in
(select employeeid from orders where
orderid in(select orderid from orderdetails
where productid=@val))
End
Exec empord '101'
Benefits of stored procedure
1.Precompiled execution.
2.Reduced client/server traffic.
3.Efficient reuse of code
4.Enhanced security controls

Temporary Stored procedure

# denotes a local temporary stored procedure;


## denotes a global temporary stored procedure.
Local temporary:
● A local temporary procedure is visible only to
the connection that created it.
● Local temporary procedures are automatically

dropped at the end of the current session.

Global temporary:
● A global temporary procedure is available to all
connections.
● Global temporary procedures are dropped at

the end of the last session using the procedure.


create a local temporary stored procedure.
CREATE PROCEDURE #Cash AS
SELECT * from employee

Exec #Cash

create a global temporary stored procedure.


CREATE PROCEDURE ##account AS
SELECT * from department

Exec ##account
Thank
You

Potrebbero piacerti anche