Professional Documents
Culture Documents
TSQL Material
TSQL Material
SQLSERVER DEVELOPEMENT
TSQL
By SURESH VADDE
(REAL TIME EXPERIENCED BUT NOT EXPERT)
KESHTO TECHNOLOGIES
http://care.dlservice.microsoft.com/download/D/8/0/
D808E432-5AC6-4DA5-A087-21947AC4AC5F/1033/
SQLFULL_ENU.iso?lcid=1033
INDEX:
Phone: 9951338095
Email: keshtotechnologies@gmail.com
KESHTO TECHNOLOGIES
2
Ex:
1. Text files
2. Facts
Phone: 9951338095
Email: keshtotechnologies@gmail.com
KESHTO TECHNOLOGIES
4
3. Figures (photos)
4. Audios
5. Videos etc...
Properties:
1. Create
2. Insert
3. Update
4. Delete
5. Select (query)
6. Low level security etc...
Phone: 9951338095
Email: keshtotechnologies@gmail.com
KESHTO TECHNOLOGIES
5
Phone: 9951338095
Email: keshtotechnologies@gmail.com
KESHTO TECHNOLOGIES
6
Ex of RDBMS:
1. Sqlserver
2. Oracle
3. Db2 (Database 2000, IBM)
4. Mysql (open source)
5. ms-access
6. Postgresql (open source)
7. Sybase
8. Tera data
Etc.....
Terminology:
1. Table -- Entity
2. Column (field) -- Attribute
3. Row (record) -- Tupple
4. Duplicate data (Repeated data) – Data Redundancy
Phone: 9951338095
Email: keshtotechnologies@gmail.com
KESHTO TECHNOLOGIES
7
Data models:
Relation
Perfect solution for duplicate data and null values
Normalization
EF CODD RULES:
Foundation Rule
A relational database management system must manage its
stored data using only its relational capabilities.
Phone: 9951338095
Email: keshtotechnologies@gmail.com
KESHTO TECHNOLOGIES
8
Information Rule
All information in the database should be represented in one
and only one way - as values in a table.
Phone: 9951338095
Email: keshtotechnologies@gmail.com
KESHTO TECHNOLOGIES
9
b. view definition
c. data manipulation (interactive and by program)
d. integrity constraints
e. authorization
f. transaction boundaries (begin, commit, and
rollback).
Phone: 9951338095
Email: keshtotechnologies@gmail.com
KESHTO TECHNOLOGIES
10
Integrity Independence
Integrity constraints specific to a particular relational
database must be definable in the relational data
sublanguage and storable in the catalog, not in the
application programs.
Distribution Independence
The data manipulation sublanguage of a relational DBMS
must enable application programs and terminal activities to
remain logically unimpaired whether and whenever data are
physically centralized or distributed.
Nonsubversion Rule
If a relational system has or supports a low-level (single-
record-at-a-time) language, that low-level language cannot
be used to subvert or bypass the integrity rules or
constraints expressed in the higher-level (multiple-records-
at-a-time) relational language.
SQLSERVER 2012
1. It is RDBMS database
2. It is a product of Microsoft
3. It is case insensitive
4. It is platform dependant-- it is compatible for windows OS
5. User friendly
Phone: 9951338095
Email: keshtotechnologies@gmail.com
KESHTO TECHNOLOGIES
11
Sqlserver 4.0
Sqlserver 5.0
Sqlserver 6.0 (SQL 95)
Sqlserver 6.5 (HYDRA)
Sqlserver 7.0 (SPHINX)
Sqlserver 8.0(2000) (SHILOH)
Sqlserver 9.0(2005) (YUKON)
Sqlserver 10.0(2008) (KALMAI)
Sqlserver 10.5(2008 R2) (KILIMANJARO)
Sqlserver 11.0(2012) (DENALI)
Editions (Flavors):
1. Enterprise edition
2. Standard edition
3. Developer edition
4. Mobile edition
5. Compact Edition
6. Personal edition (Express Edition) free
Software Requirements:
1. OS
Phone: 9951338095
Email: keshtotechnologies@gmail.com
KESHTO TECHNOLOGIES
12
Windows 8
Windows server 2003
Windows server 2008
Windows server 2008 R2
Hardware requirements:
1. Processor:
Ex:
2. Hard disk:
1 GB
3. Ram:
Phone: 9951338095
Email: keshtotechnologies@gmail.com
KESHTO TECHNOLOGIES
13
1 GB
Tool:
Phone: 9951338095
Email: keshtotechnologies@gmail.com
KESHTO TECHNOLOGIES
14
A. Windows’s authentication
B. Sqlserver authentication
Things to Observe:
Phone: 9951338095
Email: keshtotechnologies@gmail.com
KESHTO TECHNOLOGIES
15
Databases:
1. System databases:
A. master
B. model
C. msdb
D. tempdb
E. MS resources
Phone: 9951338095
Email: keshtotechnologies@gmail.com
KESHTO TECHNOLOGIES
16
Description
model
Database Template database
2. User databases:
Phone: 9951338095
Email: keshtotechnologies@gmail.com
KESHTO TECHNOLOGIES
17
Ex:
(NAME=suresh,
FILENAME="C:\MSSQL\vadde\suresh.mdf",
SIZE=100,
MAXSIZE=200,
FILEGROWTH=25%)
Log on
(
NAME=suresh_log,
FILENAME="C:\MSSQL\vadde\suresh_log.ldf",
SIZE=100,
MAXSIZE=200,
FILEGROWTH=25%)
Character strings:
Phone: 9951338095
Email: keshtotechnologies@gmail.com
KESHTO TECHNOLOGIES
18
Unicode strings:
Binary types:
Phone: 9951338095
Email: keshtotechnologies@gmail.com
KESHTO TECHNOLOGIES
19
Maximum 2GB
Number types:
Date types:
Phone: 9951338095
Email: keshtotechnologies@gmail.com
KESHTO TECHNOLOGIES
21
Phone: 9951338095
Email: keshtotechnologies@gmail.com
KESHTO TECHNOLOGIES
22
DATA:
Phone: 9951338095
Email: keshtotechnologies@gmail.com
KESHTO TECHNOLOGIES
23
Phone: 9951338095
Email: keshtotechnologies@gmail.com
KESHTO TECHNOLOGIES
24
DATA:
Phone: 9951338095
Email: keshtotechnologies@gmail.com
KESHTO TECHNOLOGIES
25
SQL
Sublanguages:
Phone: 9951338095
Email: keshtotechnologies@gmail.com
KESHTO TECHNOLOGIES
26
1. DDL:
Commands:
1. Create
2. Truncate
3. Drop
4. Alter
Note:
Max length of a table name and column name is 30; it
may include alphabets, digits 0 to 9 and special
character '_' (Underscore).
It is known as "naming conventions".
Max of 1024 columns can be included into a table.
Phone: 9951338095
Email: keshtotechnologies@gmail.com
KESHTO TECHNOLOGIES
27
Ex:
4. Alter:
Phone: 9951338095
Email: keshtotechnologies@gmail.com
KESHTO TECHNOLOGIES
28
Ex: alter table T1 add sal numeric (7, 2), comm Numeric (5,
2), address varchar (50)
Miscellaneous:
Phone: 9951338095
Email: keshtotechnologies@gmail.com
KESHTO TECHNOLOGIES
29
Ex: sp_help T1
Syn: sp_tables
Ex: sp_tables
Syn: sp_databases
Phone: 9951338095
Email: keshtotechnologies@gmail.com
KESHTO TECHNOLOGIES
30
DML:
Commands:
1. Insert
2. Update
3. Delete
Note: while working with char and date time data we should
use single quotes
Phone: 9951338095
Email: keshtotechnologies@gmail.com
KESHTO TECHNOLOGIES
31
Phone: 9951338095
Email: keshtotechnologies@gmail.com
KESHTO TECHNOLOGIES
32
DRL:
Commands:
Ex: select*from T3
Phone: 9951338095
Email: keshtotechnologies@gmail.com
KESHTO TECHNOLOGIES
33
Constraints:
Adv:
Phone: 9951338095
Email: keshtotechnologies@gmail.com
KESHTO TECHNOLOGIES
34
Types:
1. Primary key
2. Unique constraint
3. Check constraint
4. Not null
5. Foreign key (Referential integrity)
1. Primary key:
Properties:
It is unique
It is not null
Phone: 9951338095
Email: keshtotechnologies@gmail.com
KESHTO TECHNOLOGIES
35
Ex: create table t1 (id int constraint p_key primary key, name
varchar (20), DOB datetime)
Ex: create table t2 (id int, name varchar (20), DOB datetime,
constraint p_key3 primary key (id, name))
2. Unique constraint:
Properties:
Unique
Phone: 9951338095
Email: keshtotechnologies@gmail.com
KESHTO TECHNOLOGIES
36
5. Foreign key:
Phone: 9951338095
Email: keshtotechnologies@gmail.com
KESHTO TECHNOLOGIES
37
Note:
Phone: 9951338095
Email: keshtotechnologies@gmail.com
KESHTO TECHNOLOGIES
38
Ex: create table t6 (id int, address varchar (50), job varchar
(20), constraint f_key foreign key (id) references t1 (id))
Dropping constraint:
Ex: sp_helpconstraint t1
Normalization:
Phone: 9951338095
Email: keshtotechnologies@gmail.com
KESHTO TECHNOLOGIES
39
Un-Normalized Table
Phone: 9951338095
Email: keshtotechnologies@gmail.com
KESHTO TECHNOLOGIES
40
Phone: 9951338095
Email: keshtotechnologies@gmail.com
KESHTO TECHNOLOGIES
41
PROJ-INFO
PROJNO PNAME BUD
P1 Pn1 ------
P2 Pn2 ------
P3 Pn3 ------
EMP-INFO
EMPNO ENAME SAL DEPTNO DNAME LOC
11 ---- ---- 10 ------ -----
22 ---- ---- 10 ------ -----
33 ---- ---- 20 ------ -----
Phone: 9951338095
Email: keshtotechnologies@gmail.com
KESHTO TECHNOLOGIES
42
PROJ-INFO
PROJNO PNAME BUD
P1 Pn1 ------
P2 Pn2 ------
P3 Pn3 ------
EMP-INFO DEPT-INFO
EMPNO ENAME SAL DEPTNO DNAME LOC
11 ---- ---- 10 ------ -----
22 ---- ---- 20 ------ -----
33 ---- ----
1. GRANT Statement
2. REVOKE Statement
Syntax:
Ex:
Phone: 9951338095
Email: keshtotechnologies@gmail.com
KESHTO TECHNOLOGIES
44
Ex:
Syntax:
Ex:
Phone: 9951338095
Email: keshtotechnologies@gmail.com
KESHTO TECHNOLOGIES
45
TCL:
ROLLBACK Statement
COMMIT Statement
SAVE TRAN [SACTION]
Ex:
SET IMPLICIT_TRANSATIONS ON
Phone: 9951338095
Email: keshtotechnologies@gmail.com
KESHTO TECHNOLOGIES
46
ROLLBACK TRAN
Ex:
BEGIN TRAN
INSERT INTO DEPT VALUES (50,’TRA’,’AUS’)
GO
UPDATE EMP SET SAL=SAL+1000 WHERE EMPNO=11
GO
Phone: 9951338095
Email: keshtotechnologies@gmail.com
KESHTO TECHNOLOGIES
47
ROLLBACK TRAN
Ex:
Ex:
BEGIN TRAN
INSERT INTO DEPT VALUES (50,’TRA’,’AUS’)
Phone: 9951338095
Email: keshtotechnologies@gmail.com
KESHTO TECHNOLOGIES
48
GO
UPDATE EMP SET SAL=SAL+1000 WHERE EMPNO=11
GO
DELETE FROM STUDENT WHERE SNO=101
COMMIT TRAN
BEGIN TRAN
SAVE TRAN A
INSERT INTO DEPT VALUES (50,’TRA’,’AUS’)
SAVE TRAN B
UPDATE EMP SET SAL=SAL+1000 WHERE EMPNO=11
SAVE TRAN C
DELETE FROM STUDENT WHERE SNO=101
Phone: 9951338095
Email: keshtotechnologies@gmail.com
KESHTO TECHNOLOGIES
49
Operators:
1. +:
2.-:
3.*:
4. /:
=, <> or ! =,>,>=,<,<=
1. =:
Phone: 9951338095
Email: keshtotechnologies@gmail.com
KESHTO TECHNOLOGIES
50
2. <> or! =:
3.>:
4.>=:
5. <:
6. <=:
Phone: 9951338095
Email: keshtotechnologies@gmail.com
KESHTO TECHNOLOGIES
51
1. And:
2. Or:
Not between:
Ex: select*from EMP where sal not between 2000 and 5000
Not in:
Phone: 9951338095
Email: keshtotechnologies@gmail.com
KESHTO TECHNOLOGIES
52
Alias:
Alias is temporary
Ex:
Phone: 9951338095
Email: keshtotechnologies@gmail.com
KESHTO TECHNOLOGIES
53
Inbuilt functions:
1. Sum ():
2. Average ():
3. Max ():
4. Min ():
Phone: 9951338095
Email: keshtotechnologies@gmail.com
KESHTO TECHNOLOGIES
54
Note:
1. Count Function doesn’t consider null as a value.
2. COUNT function does not count the rows of the column
whose data type is BIGINT, In such situations we have to
use COUNT_BIG function
7. Variance ():
DIFF_VALUES
10
20
NO_OF_ROWS
Phone: 9951338095
Email: keshtotechnologies@gmail.com
KESHTO TECHNOLOGIES
55
2. Numeric functions:
1. Absolute:
O/p: 9.5
2. Ceiling:
O/p: 10
3. Floor:
O/p: 9
4. Square root:
O/p: 12
Phone: 9951338095
Email: keshtotechnologies@gmail.com
KESHTO TECHNOLOGIES
56
5. Power:
O/p: 27(3*3*3)
6. Square:
O/p: 9(3*3)
7. Pi ():
Select pi ()
O/p: 3.14(22/7)
8. Log:
O/p: 0.693147180559945
9. Exp:
Phone: 9951338095
Email: keshtotechnologies@gmail.com
KESHTO TECHNOLOGIES
57
O/p: 7.38905609893065
1. Length:
Phone: 9951338095
Email: keshtotechnologies@gmail.com
KESHTO TECHNOLOGIES
58
O/p: 9
2. Upper:
O/p: HYDERABAD
3. Lower:
O/P: hyderabad
4. Reverse:
O/p: Malayalam
O/p: loohcs
5. Replace:
Phone: 9951338095
Email: keshtotechnologies@gmail.com
KESHTO TECHNOLOGIES
59
O/p: cyberabad
6. Substring:
O/p: derab
O/p: 65
O/p: 97
O/p: b
1. Getdate ():
Select getdate ()
Phone: 9951338095
Email: keshtotechnologies@gmail.com
KESHTO TECHNOLOGIES
60
2. Getutcdate ():
Select getutcdate ()
3. Dateadd:
4. Datediff:
O/p: 73
5. Date part:
O/p: 2010
Phone: 9951338095
Email: keshtotechnologies@gmail.com
KESHTO TECHNOLOGIES
61
O/p: 4
O/p: 20
6. Date name:
O/p: Tuesday
Set operators:
a= {1, 2, 3}
b= {3, 4, 5}
1. A union B= {1, 2, 3, 4, 5}
3. A intersect B= {3}
Phone: 9951338095
Email: keshtotechnologies@gmail.com
KESHTO TECHNOLOGIES
62
1. Union:
2. Union all:
3. Intersect:
4. Except:
Group by clause:
Phone: 9951338095
Email: keshtotechnologies@gmail.com
KESHTO TECHNOLOGIES
63
Having clause:
Phone: 9951338095
Email: keshtotechnologies@gmail.com
KESHTO TECHNOLOGIES
64
It is an arithmetical expression
Order by clause:
Ascending:
Phone: 9951338095
Email: keshtotechnologies@gmail.com
KESHTO TECHNOLOGIES
65
Descending:
Clause precedence:
1. Where clause
2. Group by clause
3. Having clause
4. order by clause
Joins:
Phone: 9951338095
Email: keshtotechnologies@gmail.com
KESHTO TECHNOLOGIES
66
Types of JOINS:
Ex:
Phone: 9951338095
Email: keshtotechnologies@gmail.com
KESHTO TECHNOLOGIES
67
Phone: 9951338095
Email: keshtotechnologies@gmail.com
KESHTO TECHNOLOGIES
68
a= (1, 2, 3)
b= (4, 5, 6)
A*B=3*3=9
Ex:
SUB-QUERIES:
Syn:
Phone: 9951338095
Email: keshtotechnologies@gmail.com
KESHTO TECHNOLOGIES
69
Types:
Select max (sal) from EMP where sal< (select max (sal) from
EMP)
Phone: 9951338095
Email: keshtotechnologies@gmail.com
KESHTO TECHNOLOGIES
70
Select max (sal) from EMP where sal< (select max (sal) from
EMP where
Sal< (select max (sal) from EMP))
Ex:
n=n-1
Phone: 9951338095
Email: keshtotechnologies@gmail.com
KESHTO TECHNOLOGIES
71
Ex:
Phone: 9951338095
Email: keshtotechnologies@gmail.com
KESHTO TECHNOLOGIES
72
Syn:
Ex:
Ex: (Oracle)
Phone: 9951338095
Email: keshtotechnologies@gmail.com
KESHTO TECHNOLOGIES
73
Syn:
Ex:
Views:
1. Simple Views
2. Complex Views
Phone: 9951338095
Email: keshtotechnologies@gmail.com
KESHTO TECHNOLOGIES
74
Syntax:
Ex:
The above insert statement inserts the values into base table
EMP as well as into view V1.
Ex:
The above insert statement inserts the values into base table
EMP as well as into view
V2.
Phone: 9951338095
Email: keshtotechnologies@gmail.com
KESHTO TECHNOLOGIES
75
The above insert statement inserts the values into only base
table EMP but not into view
V2 because according to the definition of V2 user supplied
values are invalid values. It means invalid values are inserting
into base table EMP. To stop this kind of operations we have
to create the view with ‘WITH CHECK OPTION’.
Ex:
Syntax:
SP_HELPTEXT VIEWNAME
Ex: SP_HELPTEXT V1
Phone: 9951338095
Email: keshtotechnologies@gmail.com
KESHTO TECHNOLOGIES
76
Ex:
SP_HELPTEXT V4
Ex:
SP_HELPTEXT V4
Phone: 9951338095
Email: keshtotechnologies@gmail.com
KESHTO TECHNOLOGIES
77
2. Complex Views:
Ex:
Syntax:
Ex:
Phone: 9951338095
Email: keshtotechnologies@gmail.com
KESHTO TECHNOLOGIES
78
Indexes:
Phone: 9951338095
Email: keshtotechnologies@gmail.com
KESHTO TECHNOLOGIES
79
TYPES OF INDEXES:
CLUSTERED INDEX
NON-CLUSTERED INDEX
Syntax:
Ex:
CREATE CLUSTERED INDEX CI ON EMP (EMPNO)
Phone: 9951338095
Email: keshtotechnologies@gmail.com
KESHTO TECHNOLOGIES
80
Syntax:
CREATE [UNIQUE] NONCLUSTERED INDEX INDEXNAME
ON TABLENAME (COLUMN1…)
Ex:
CREATE NONCLUSTERED INDEX NCI ON EMP (ENAME, SAL)
Ex:
CREATE UNIQUE NONCLUSTERED INDEX UI ON DEPT
(DNAME)
DEPTNO DNAME
10 SALES
20 HR
30 IR
10 HR (Accepted)
20 SALES (Accepted)
Phone: 9951338095
Email: keshtotechnologies@gmail.com
KESHTO TECHNOLOGIES
81
Ex:
Synonym:
It is a database object
Phone: 9951338095
Email: keshtotechnologies@gmail.com
KESHTO TECHNOLOGIES
82
Retrieving data:
Dropping:
SQLSERVER LOCKS:
1. Intent
Intent lock shows the future intention of SQL Server's lock
manager to acquire locks on a specific unit of data for a
particular transaction. SQL Server uses intent locks to queue
exclusive locks, thereby ensuring that these locks will be
placed on the data elements in the order the transactions
were initiated.
Types: Intent locks are 3 types……
Intent shared (IS): IS locks indicate that the transaction will
read some (but not all) the resources in the table or page by
placing shared locks.
Phone: 9951338095
Email: keshtotechnologies@gmail.com
KESHTO TECHNOLOGIES
83
Phone: 9951338095
Email: keshtotechnologies@gmail.com
KESHTO TECHNOLOGIES
84
Syntax:
Ex:
Phone: 9951338095
Email: keshtotechnologies@gmail.com
KESHTO TECHNOLOGIES
85
Syntax:
SP_BINDDEFAULT DEFAULTNAME,
‘TABLENAME.COLUMN’
Ex:
Phone: 9951338095
Email: keshtotechnologies@gmail.com
KESHTO TECHNOLOGIES
86
Syntax:
SP_UNBINDEFAULT ‘TABLENAME...COLUMN’
SP_UNBINDEFAULT ‘ DEPT.LOC’
SP_UNBINDEFAULT ‘ EMP.ENAME’
Syntax:
DROP DEFAULT D1
Syntax:
Ex:
Phone: 9951338095
Email: keshtotechnologies@gmail.com
KESHTO TECHNOLOGIES
87
ERROR
DROP RULE R1
TSQL Programs:
Phone: 9951338095
Email: keshtotechnologies@gmail.com
KESHTO TECHNOLOGIES
88
Types:
Ex: @x int
Ex: 1, 2,3,4,5
1. Named batches
2. Unnamed batches
Structure:
Declare
Declaration and initiation of variables
Begin
Body of the program
Phone: 9951338095
Email: keshtotechnologies@gmail.com
KESHTO TECHNOLOGIES
89
End
1. Declare:
2. Begin:
Logic
Exceptions
Conditional statements
Output
Begin
Print 'welcome to database'
Phone: 9951338095
Email: keshtotechnologies@gmail.com
KESHTO TECHNOLOGIES
90
End
Declare @x int
Set @x=20
Declare @y int
Set @y=10
Declare @z int
Begin
Set @z=@x+@y
Print @z
End
Conditional Statements:
1. If. Else:
Syn:
Declare
Variable declaration and initiation
Begin
If condition
Output
Else
Output
End
Phone: 9951338095
Email: keshtotechnologies@gmail.com
KESHTO TECHNOLOGIES
91
Declare @x int
Set @x=20
Declare @y int
Set @y=10
Begin
If @x>@y
Print 'x is greatest'
Else
Print 'y is greatest'
End
Syn:
Declare
Variable declaration and initiation
Begin
If condition
Output
Else if condition
Output
Else
Output
End
Phone: 9951338095
Email: keshtotechnologies@gmail.com
KESHTO TECHNOLOGIES
92
Declare @x int
Set @x=20
Declare @y int
Set @y=10
Declare @z int
Set @z=5
Begin
If @x>@y and @x>@z
Print 'x is greatest'
Else if @y>@x and @y>@z
Print 'y is greatest'
Else
Print 'z is greatest'
End
3. While:
Syn:
Declare
Variable declaration and initiation
While condition
Begin
Body of the program
End
Phone: 9951338095
Email: keshtotechnologies@gmail.com
KESHTO TECHNOLOGIES
93
Cursors:
Types of Cursors:
1. STATIC CURSOR
2. DYNAMIC CURSOR
3. KEYSET CURSOR
4. FORWARD_ONLY CURSOR
DEPT_TABLE:
Phone: 9951338095
Email: keshtotechnologies@gmail.com
KESHTO TECHNOLOGIES
94
Syntax:
OPEN CURSORNAME
FETCH {FIRST/NEXT/PRIOR/LAST/ABSOLUTE
N/RELATIVE N}
FROM CURSORNAME
CLOSE CURSORNAME
Phone: 9951338095
Email: keshtotechnologies@gmail.com
KESHTO TECHNOLOGIES
95
DEALLOCATE CURSORNAME
1. STATIC CURSOR:
1.
Phone: 9951338095
Email: keshtotechnologies@gmail.com
KESHTO TECHNOLOGIES
96
OPEN SC
CLOSE SC
OPEN SC
Phone: 9951338095
Email: keshtotechnologies@gmail.com
KESHTO TECHNOLOGIES
97
CLOSE SC
DEALLOCATE SC
2. DYNAMIC CURSOR:
Ex:
DECLARE DC CURSOR DYNAMIC FOR SELECT * FROM DEPT
OPEN SC
Phone: 9951338095
Email: keshtotechnologies@gmail.com
KESHTO TECHNOLOGIES
98
CLOSE DC
DEALLOCATE DC
3. KEYSET CURSOR:
Ex:
OPEN KC
Phone: 9951338095
Email: keshtotechnologies@gmail.com
KESHTO TECHNOLOGIES
99
CLOSE KC
OPEN KC
CLOSE KC
DEALLOCATE KC
4. FORWARD_ONLY CURSOR:
Ex:
DECLARE FC CURSOR FORWARD_ONLY FOR SELECT * FROM
DEPT
Phone: 9951338095
Email: keshtotechnologies@gmail.com
KESHTO TECHNOLOGIES
100
OPEN FC
CLOSE SC
DEALLOCATE FC
NOTE:
Phone: 9951338095
Email: keshtotechnologies@gmail.com
KESHTO TECHNOLOGIES
101
SET @N=@N+1
PRINT @N
END
CLOSE SC
DEALLOCATE SC
OUTPUT:
20 HR CHE
Stored Procedures:
It is a database object
After successful compilation it will be stored into a
precompiled programming unit
Phone: 9951338095
Email: keshtotechnologies@gmail.com
KESHTO TECHNOLOGIES
102
Types:
Ex:
sp_tables
sp_helptable <TN>
sp_helpdb <database name>
sp_rename
sp_addumpdevice
Phone: 9951338095
Email: keshtotechnologies@gmail.com
KESHTO TECHNOLOGIES
103
Syntax:
Drop:
Programs:
Phone: 9951338095
Email: keshtotechnologies@gmail.com
KESHTO TECHNOLOGIES
104
Execution:
Result:
Select*from dept
Phone: 9951338095
Email: keshtotechnologies@gmail.com
KESHTO TECHNOLOGIES
105
Execution:
Result:
Select*from dept
CREATE PROCEDURE P1
AS
BEGIN
SELECT * FROM EMP
END
EXEC P1
Phone: 9951338095
Email: keshtotechnologies@gmail.com
KESHTO TECHNOLOGIES
106
EXEC P2 20
EXEC P3
Output: 30
EXEC P3 25, 45
Output: 70
It is a database object
Phone: 9951338095
Email: keshtotechnologies@gmail.com
KESHTO TECHNOLOGIES
107
Phone: 9951338095
Email: keshtotechnologies@gmail.com
KESHTO TECHNOLOGIES
108
@ PARA 2 DATATYPE…)
RETURNS <DATATYPE>
AS
BEGIN
DECLARE @VARIABLE DATATYPE
--------
----------
RETURN @VARIABLE
END
Phone: 9951338095
Email: keshtotechnologies@gmail.com
KESHTO TECHNOLOGIES
109
PRINT/SELECT DBO.F2(22)
Phone: 9951338095
Email: keshtotechnologies@gmail.com
KESHTO TECHNOLOGIES
110
Syntax:
CREATE FUNCTION <FUNCTION_NAME> (PARA 1 DATA
TYPE ……….)
RETURNS TABLE
AS
BEGIN
<FUNCTION BODY>
RETURN (SELECT STATEMENT)
END
Phone: 9951338095
Email: keshtotechnologies@gmail.com
KESHTO TECHNOLOGIES
111
ENAME DNAME
SMITH RESEARCH
MILLER ACCOUNTING
No
Phone: 9951338095
Email: keshtotechnologies@gmail.com
KESHTO TECHNOLOGIES
112
Drop:
Syn:
TRIGGERS:
Phone: 9951338095
Email: keshtotechnologies@gmail.com
KESHTO TECHNOLOGIES
113
DML Triggers:
AFTER triggers are only allowed for tables, and they execute
after the data modification has been completed against the
table.
Phone: 9951338095
Email: keshtotechnologies@gmail.com
KESHTO TECHNOLOGIES
114
Phone: 9951338095
Email: keshtotechnologies@gmail.com
KESHTO TECHNOLOGIES
115
Parts of Trigger:
1. Triggering Statement
2. Triggering Restriction
3. Triggering Action
Phone: 9951338095
Email: keshtotechnologies@gmail.com
KESHTO TECHNOLOGIES
116
Things to Observe:
Phone: 9951338095
Email: keshtotechnologies@gmail.com
KESHTO TECHNOLOGIES
117
Drop:
Phone: 9951338095
Email: keshtotechnologies@gmail.com
KESHTO TECHNOLOGIES
118
End
Result:
Note:
Ex:
Phone: 9951338095
Email: keshtotechnologies@gmail.com
KESHTO TECHNOLOGIES
119
BEGIN
UPDATE EMP SET ename=Upper (ename)
WHERE empno IN (SELECT empno FROM INSERTED)
END
Instead of Trigger:
Ex:
Syntax:
Phone: 9951338095
Email: keshtotechnologies@gmail.com
KESHTO TECHNOLOGIES
120
1.
USE PUBS
2.
USE PUBS
Phone: 9951338095
Email: keshtotechnologies@gmail.com
KESHTO TECHNOLOGIES
121
Phone: 9951338095
Email: keshtotechnologies@gmail.com
KESHTO TECHNOLOGIES
122
DECLARE @i MyType
END
BEFORE INSTEAD OF
triggers triggers Use INSTEAD OF trigger in SQL
Server as an equivalent to
Oracle's BEFORE trigger.
SELECT Sport,
CASE Sport
WHEN 'Cricket' THEN 'England'
WHEN 'Hockey' THEN 'India'
WHEN 'Base Ball' THEN
'America'
ELSE NULL
END AS 'Originating Country'
FROM Sports
DESCRIBE sp_help or
sp_columns There are a lot of alternatives
for Oracle's DESCRIBE, in SQL
Phone: 9951338095
Email: keshtotechnologies@gmail.com
KESHTO TECHNOLOGIES
123
Phone: 9951338095
Email: keshtotechnologies@gmail.com
KESHTO TECHNOLOGIES
124
SELECT 'Something'
FROM DUAL
SELECT 'Something'
Phone: 9951338095
Email: keshtotechnologies@gmail.com
KESHTO TECHNOLOGIES
125
Phone: 9951338095
Email: keshtotechnologies@gmail.com
KESHTO TECHNOLOGIES
126
)
MINUS Not
operator supported Use NOT EXISTS clause in your
SELECT statement to generate
the same result.
Phone: 9951338095
Email: keshtotechnologies@gmail.com
KESHTO TECHNOLOGIES
127
Phone: 9951338095
Email: keshtotechnologies@gmail.com
KESHTO TECHNOLOGIES
128
Phone: 9951338095
Email: keshtotechnologies@gmail.com
KESHTO TECHNOLOGIES
129
rownum No equivalent
pseudo Though there is no rownum or
column rowid in SQL Server, there are
several ways in which a row
number can be generated.
Phone: 9951338095
Email: keshtotechnologies@gmail.com
KESHTO TECHNOLOGIES
130
Phone: 9951338095
Email: keshtotechnologies@gmail.com
KESHTO TECHNOLOGIES
131
Synonym Views
You can simulate Oracle
Synonyms in SQL Server using
Views. For example, the
following creates a view that
returns the OrderID and
OrderDate from Orders table.
Phone: 9951338095
Email: keshtotechnologies@gmail.com
KESHTO TECHNOLOGIES
132
SELECT table_name
FROM TABS
Phone: 9951338095
Email: keshtotechnologies@gmail.com
KESHTO TECHNOLOGIES
133
Phone: 9951338095
Email: keshtotechnologies@gmail.com
KESHTO TECHNOLOGIES
134
Operator Description
+= Addition with assignment, or
string concatenation with
assignment
-= Subtraction with assignment
*= Multiplication with
assignment
/= Division with assignment
%= Modulo with assignment
Ex:
Phone: 9951338095
Email: keshtotechnologies@gmail.com
KESHTO TECHNOLOGIES
135
SYNTAX:
EX:
4. MERGE Statement
Phone: 9951338095
Email: keshtotechnologies@gmail.com
KESHTO TECHNOLOGIES
136
Syntax:
Phone: 9951338095
Email: keshtotechnologies@gmail.com
KESHTO TECHNOLOGIES
137
Some, like the date data type which stores a simple date
with out the time component. Others like the geometry
data type, which allows storage and querying of spatial
data, have only recently addressed.
SQL Server supports four brand new date and time data
types, which includes
1. date
2. time
3. datetime2
4. datetimeoffset
Ex:
Phone: 9951338095
Email: keshtotechnologies@gmail.com
KESHTO TECHNOLOGIES
138
Ex:
Phone: 9951338095
Email: keshtotechnologies@gmail.com
KESHTO TECHNOLOGIES
139
Ex:
Method Description
GetAncestor(n) Retrieves the nth ancestor of
Phone: 9951338095
Email: keshtotechnologies@gmail.com
KESHTO TECHNOLOGIES
140
SQL Server 2008 includes two new data types for storing,
querying, and manipulating spatial data.
Phone: 9951338095
Email: keshtotechnologies@gmail.com
KESHTO TECHNOLOGIES
141
Geometry
Geography
QUERIES:
Phone: 9951338095
Email: keshtotechnologies@gmail.com
KESHTO TECHNOLOGIES
142
Phone: 9951338095
Email: keshtotechnologies@gmail.com
KESHTO TECHNOLOGIES
143
Phone: 9951338095
Email: keshtotechnologies@gmail.com
KESHTO TECHNOLOGIES
144
Phone: 9951338095
Email: keshtotechnologies@gmail.com
KESHTO TECHNOLOGIES
145
30. Display empno, ename, deptno, and sal. Sort the output
first based on name and within name by deptno and
within deptno by Sal;
Select * from EMP order by ename, deptno, sal;
31. Display name, Sal, hra, pf, da, total Sal for each
employee. The output should be in the order of total Sal,
Phone: 9951338095
Email: keshtotechnologies@gmail.com
KESHTO TECHNOLOGIES
146
36. Display the various jobs and total salary for each job.
select job, sum(sal) from emp group by job;
Phone: 9951338095
Email: keshtotechnologies@gmail.com
KESHTO TECHNOLOGIES
147
37. Display each job along with minimum sal being paid in
each job group.
select job, min(sal) from emp group by job;
39. Display the various jobs along with total sal for each of
the jobs where total sal is greater than 40000.
select job, sum(sal) from emp group by job having
sum(sal)>40000;
Phone: 9951338095
Email: keshtotechnologies@gmail.com
KESHTO TECHNOLOGIES
148
44. Display the names of clerks who earn salary more than
that of James of that of sal lesser than that of Scott.
select ename from emp where job='CLERK' and
sal<(select sal from emp where ename='SCOTT') and
sal>(select sal from emp where ename='JAMES');
Phone: 9951338095
Email: keshtotechnologies@gmail.com
KESHTO TECHNOLOGIES
149
50. Display the job groups having total salary greater then
the maximum salary for managers.
select job, sum(sal) from emp group by job having
sum(sal) >
(select max(sal) from emp where job='MANAGER');
Phone: 9951338095
Email: keshtotechnologies@gmail.com
KESHTO TECHNOLOGIES
150
Phone: 9951338095
Email: keshtotechnologies@gmail.com
KESHTO TECHNOLOGIES
151
59. Display the details of those who do not have any person
working under them.
select empno from emp where empno not in (select
mgr from emp where mgr is not null);
Phone: 9951338095
Email: keshtotechnologies@gmail.com
KESHTO TECHNOLOGIES
152
61. Display those who are not managers and who are
managers any one.
select * from emp where empno in(select mgr from
emp) union
select * from emp where empno not in(select mgr
from emp where mgr is not null);
Phone: 9951338095
Email: keshtotechnologies@gmail.com
KESHTO TECHNOLOGIES
153
71. Display those employees who are not working under any
manger.
select * from emp where mgr is null or empno=mgr;
Phone: 9951338095
Email: keshtotechnologies@gmail.com
KESHTO TECHNOLOGIES
154
Phone: 9951338095
Email: keshtotechnologies@gmail.com
KESHTO TECHNOLOGIES
155
77. Display name and salary of ford if his Sal is equal to high
Sal of his grade.
select ename,sal from emp e where ename='FORD'
and sal=(select hisal from salgrade where
grade=(select grade from salgrade where
e.sal>=losal and e.sal<=hisal));
78. Display employee name, his job, his dept name, his
manager name, his grade and make out of an under
department wise.
select d.deptno, e.ename, e.job, d.dname, m.ename,
s.grade from
emp e, emp m, dept d, salgrade s where
e.deptno=d.deptno and e.sal between s.losal and
s.hisal and e.mgr=m.empno order by e.deptno;
79. List out all the employees name, job, and salary grade
and department name for every one in the company
except ‘CLERK’. Sort on salary display the highest salary.
select empno, ename, sal, dname, grade from emp e,
dept d, salgrade s where e.deptno=d.deptno and
e.sal between s.losal and s.hisal and e.job<>'CLERK'
order by sal;
Phone: 9951338095
Email: keshtotechnologies@gmail.com
KESHTO TECHNOLOGIES
156
Phone: 9951338095
Email: keshtotechnologies@gmail.com
KESHTO TECHNOLOGIES
157
Phone: 9951338095
Email: keshtotechnologies@gmail.com
KESHTO TECHNOLOGIES
158
92. Delete those records from EMP table whose deptno not
available in dept table?
delete from emp where deptno not in(select deptno
from dept);
94. Display employee name, Sal, comm. and whose net pay
is greater than any other in the company?
Select ename, sal, comm from emp where
sal+sal*15/100-sal*5/100 +sal*10/100 = (select
max(sal+sal*15/100-sal*5/100+sal*10/100) from
emp);
Phone: 9951338095
Email: keshtotechnologies@gmail.com
KESHTO TECHNOLOGIES
159
Phone: 9951338095
Email: keshtotechnologies@gmail.com
KESHTO TECHNOLOGIES
160
104. Print the details of all the employees who are sub
ordinate to Blake.
Select * from emp where mgr=(select empno from
emp where ename='BLAKE');
Phone: 9951338095
Email: keshtotechnologies@gmail.com
KESHTO TECHNOLOGIES
161
107. Find out how many mangers are there with out listing
them.
Select count (*) from EMP where empno in (select
mgr from EMP);
109. List out the lowest paid employees working for each
manager, exclude any groups where min sal is less than
1000 sort the output by sal.
select e.ename,e.mgr,e.sal from emp e where sal
in(select min(sal) from emp where mgr=e.mgr) and
e.sal>1000 order by sal;
110. find out the all employees who joined the company
before their manager.
Select * from emp e where hiredate<(select hiredate
from emp where empno=e.mgr);
Phone: 9951338095
Email: keshtotechnologies@gmail.com
KESHTO TECHNOLOGIES
162
111. list out the all employees by name and number along
with their manager’s name and number also display ‘No
Manager’ who has no manager.
select e.empno,e.ename,m.empno Manager,m.ename
ManagerName from emp e,emp m where
e.mgr=m.empno
union
select empno,ename,mgr,'No Manager' from emp
where mgr is null;
112. find out the employees who earned the highest Sal in
each job typed sort in descending Sal order.
select * from emp e where sal =(select max(sal) from
emp where job=e.job);
113. find out the employees who earned the min Sal for
their job in ascending order.
select * from emp e where sal =(select min(sal) from
emp where job=e.job) order by sal;
114. find out the most recently hired employees in each dept
order by hire date
select * from emp order by deptno, hiredate desc;
115. display ename, sal and deptno for each employee who
earn a Sal greater than the avg of their department order
by deptno
Phone: 9951338095
Email: keshtotechnologies@gmail.com
KESHTO TECHNOLOGIES
163
120. display employees who can earn more than lowest Sal
in dept no 30
select * from emp where sal>(select min(sal) from
emp where deptno=30);
Phone: 9951338095
Email: keshtotechnologies@gmail.com
KESHTO TECHNOLOGIES
164
Phone: 9951338095
Email: keshtotechnologies@gmail.com
KESHTO TECHNOLOGIES
165
Phone: 9951338095
Email: keshtotechnologies@gmail.com
KESHTO TECHNOLOGIES
166
Phone: 9951338095
Email: keshtotechnologies@gmail.com
KESHTO TECHNOLOGIES
167
Phone: 9951338095
Email: keshtotechnologies@gmail.com
KESHTO TECHNOLOGIES
168
Phone: 9951338095
Email: keshtotechnologies@gmail.com
KESHTO TECHNOLOGIES
169
Phone: 9951338095
Email: keshtotechnologies@gmail.com