You are on page 1of 87

SQL Server

:
Kaffash@RAI.IR
86

PDF created with pdfFactory Pro trial version www.pdffactory.com




. SQL Server
%20
%100
SQL Server

.
.
SQL Server .

PDF created with pdfFactory Pro trial version www.pdffactory.com



SQL Server

PDF created with pdfFactory Pro trial version www.pdffactory.com


: RDBMS RDBMS ) (Relational Data Base Management System
Object .
: DBA
.
: Object View Trigger RDBMS .
: Table
. :
PK
PK

FK

FK

PK

FK

FK

FK

PDF created with pdfFactory Pro trial version www.pdffactory.com

:
-1
.
-2 .
-3 .
-4
.
-5
(PK) Primary Key PK
PK.
-6 PK 1n
(FK) Foregin Key .
-7 PK FK
.
View : .
) : (Index
PK FK .
: (SP) Stored Procedures .
: (UDF) User Define Function
.
: (ESP) Extended Stored Procedures C ESP C

DLL ESP DLL
.
2

PDF created with pdfFactory Pro trial version www.pdffactory.com

: Trigger Event ... .


Trigger
.
: SQL Server 200X SQL Server 200X
.

SP

250 MB

8060 Byte

249

16

PK FK

16

1024

Select

4096

Insert

1024

Server

32767

SP

32

Sub Query

32

Object

Trigger

Object

: SQL Server
:
(1
3

PDF created with pdfFactory Pro trial version www.pdffactory.com

(2
(3
(4
(5 ) (UDF
(6


SQL Server :
- 1 :


.

)unicode (UC

)unicode (UC

Binary

PDF created with pdfFactory Pro trial version www.pdffactory.com

bit

10

TinyInt

255

SmallInt

- 32768

+32767

Int

BigInt

18

Decimal

Numeric
Float

+1

38

-10

Char
VarChar

8000Byte

Text

2GB

nVarChar

nText

uc


uc

+10

-3,4 1038

-1

38

+3,4 1038

8000Byte

nChar

-1,79 10308

Real

*
uc

18

+1,79 10308

Byte

4000

4000Byte

1GB

* UC = UniCode
5

PDF created with pdfFactory Pro trial version www.pdffactory.com

Binary

8000Byte

Varbinary

8000Byte

Image

2GB

Table

Time Stamp

UniqueIde
ntifier


...

Varbinary .

binary Power Point
Word , Excel
Tiff , Bmp ,Gif ,
Varbinary

SP


) (

- : SQL Server

IS

and

<

or

=<

Not

>

=>

><

IN
BetWeen
Like

PDF created with pdfFactory Pro trial version www.pdffactory.com

: SQL Server
SQL
Server @ .
@ Declare

] [ ,

0 255 .
TinyInt

Declare @x

Set Select
Select Set
.
= @ Set

] [ , = @ Select
:
@x = 14

Set

@x = @x +2*@ y

Set

-2 : SQL Server If Case



SQL Server .
Begin

End

PDF created with pdfFactory Pro trial version www.pdffactory.com

Begin End
. If :

IF

[
ELSE

A
B ELSE

Begin End . :
Declare @x Int , @y Int
Set @x= 200
Set @y= 300 - @x
IF @x > @y
Set @y = @x + 2
ELSE Begin
Set @y = @x + 3
Set @x = @x + 1
END

ELSE Begin
End .

PDF created with pdfFactory Pro trial version www.pdffactory.com

: Case Case . Case .


Case 2

Case 1
Case

Case
Then 1 When 1

Then 1 When 1

Then 2 When 2

Then 2 When 2

] [ ELSE n + 1

] [ ELSE n + 1
END

END

2 1 1
:
Declare @x Int
Set @x = ( Case @y
When 1 Then 102
When 2 Then 203
When 3 Then 304
ELSE
1000
)END
Case ) (
. 2 :
Declare @x Int
Set @x = ( Case
When @y=1 Then 102
When @y=2 Then 203
When @y=3 Then 304
ELSE
1000
)END

PDF created with pdfFactory Pro trial version www.pdffactory.com

2 :

@y BetWeen 2 and 10 Then 300


@y=15 or @y=17
Then 310
@y=18 or @y>=20 Then 320
@y+@z < = 30
Then 330
Not @z < = 40
Then @z
@y END

Declare @x Int
Set @x = Case When
When
When
When
When
ELSE

Select Case
.
( A G :
Set @A = Case
@B Then 10
@C=0 Then @D
)@D=@E Then Len (@F
@F=@G Then 20

When
When
When
When
END

- 3 : SQL Server
While :
1

2
While

Begin While

Continue
Break
END

2 .
1 END , Begin
Break Continue .
10

PDF created with pdfFactory Pro trial version www.pdffactory.com

:
Declare @x Int , @y BigInt
Set @x = 0
Set @y = 1
While @x < 10 Begin
Set @x = @x + 1
Set @y = @y * @x
END
Break .
Declare @x Int , @y BigInt
Set @x = 0
Set @y = 1
While 1=1 Begin
Set @x = @x + 1
IF @x>= 10 Break
Set @y = @y * @x
END
- 4 :


SQL Server .
: Left .

) , ( Left

.
: Right .

) , ( Right

.
11

PDF created with pdfFactory Pro trial version www.pdffactory.com

: SubString .
) ] [ , , ( SubString


. x Ali .
) Set @x = Left (And, 1) + SubString (Hello , 3 , 1 ) + Left (in , 1
: LTrim :

) ( LTrim

: RTrim :

) ( RTrim

:
) Set @x = LTrim (RTrim ( Hello
: Upper .

) ( Upper

12

PDF created with pdfFactory Pro trial version www.pdffactory.com

: Lower .

) ( Lower

: Len .

) ( Len

: .
)Declare @x VarChar (50) , @C TinyInt , @y VarChar (50
Set @x = Hello
= Set @y
Set @C = 0
While @C < Len (@x) Begin
Set @C = @C + 1
Set @y = SubString (@x , @C , 1) + @y
END
: Char .

) ( Char

: Reverse .

) ( Reverse

13

PDF created with pdfFactory Pro trial version www.pdffactory.com

: Replace .

) , , ( Replace


:
)Set @x = Replace (Hello,ale,xyz
ale xyz x Hzyyo
.
: Space .

) ( Space

: User-name ) ( User-name SQL Server .


: ABS | |x ) :( ABS :Floor ] [x ):( Floor : Power SQL Server Power .

) , ( Power

14

PDF created with pdfFactory Pro trial version www.pdffactory.com

: Sqrt Sqrt .

) ( Sqrt

: Isnull Null .
Null

) , ( Isnull


Null
:
)Set @x = Isnull (@y,0
y Null x
x y .
: GetDate Server . : Convert .

) , ( Convert

.
15

PDF created with pdfFactory Pro trial version www.pdffactory.com

:
) Set @x = Convert (VarChar (10) , @y
@y Unicode 10
x .
: CharIndex .

)]

[, , ( CharIndex

:
IF CharIndex (@x , @y) < > 0
Set @z = Ok
ELSE
Set @z = Not

- 5 :
: (SP) Stored Procedure SP
:
SP

]Proc[edure

Create
[

] =[ [AS] 1 1
] ] =[ [AS] 2

[ ,2

]
]

AS

16

PDF created with pdfFactory Pro trial version www.pdffactory.com

SP
SP

AS .
: Select Select .
] ... [ , 2

Select

:
2*Len (Hello) + 1

Select

11 .
: SP SP SP SP .
SP

SP


SP

SP

]EXEC [ute


: hh:mm:ss .
Create Proc Time2Val
)@T Char(8
AS
Declare @h TinyInt , @m TinyInt , @s TinyInt
))Set @h = Convert (TinyInt , SubString (@T,1,2
))Set @m = Convert (TinyInt , SubString (@T,4,2
))Set @s = Convert (TinyInt , SubString (@T,7,2
Select @h*3600 + @m*60 + @s

17

PDF created with pdfFactory Pro trial version www.pdffactory.com

: (UDF) User Define Function SQL Server UDF


.
) ]] ] [, = [ ] [ AS @[ (

Create Function

Returns
] [ AS
Begin

Return
End

UDF SQL
EXECute UDF SP UDF
Table
Select Return
UDF 1024 SP

:
Owner ) ( dbo UDF . - (ESP) Extended Stored Procedure .

18

PDF created with pdfFactory Pro trial version www.pdffactory.com

: .
)Create Function Fact (@n TinyInt
Returns BigInt
AS
Begin
Declare @RtnValue BigInt
IF @n = 0 or @n = 1
Set @RtnValue = 1
ELSE
Set @RtnValue = dbo.Fact (@n 1) *@n
Return @RtnValue
END

10

! i
i =1

:
Declare @S BigInt , @C TinyInt
Set @S = 0
Set @C = 0
While @C < 10 Begin
Set @C = @ C + 1
) Set @S = @ S + dbo.Fact (@C
END
Select @S
- 6 :
: Goto Goto :

Goto


: . :

IF @x>0 Goto Endof


ELSE
Set @y = @y + 1
)Set @x = ABS (@x
Endof:
19

PDF created with pdfFactory Pro trial version www.pdffactory.com

: Return SP .
] : EXEC[ute SQL Server ] EXEC[ute .
2

1
]EXEC[ute

) ( ]EXEC[ute

1 SQL Server 2
] EXEC[ute
.
: " "12:13:14 Time2Val .
)EXEC dbo.Time2Val (12:13:14
: ] EXEC[ute :
)Declare @x Int , @y Int , @S VarChar (4000
Set @x = 10
Set @y = 20
)Set @S=Select+ Convert (VarChar (10),@x) +++ Convert (VarChar(10),@y
)EXEC (@S

30 Select
.
20

PDF created with pdfFactory Pro trial version www.pdffactory.com

] : Begin Trans[Action TransAction


%100
) ( Begin TransAction
Server .
]] : Commit [Trans[Action Commit TransAction
.
-

]][Trans[Action

: RollBack

Begin TransAction
RollBack TransAction

Begin TransAction .
: Use .
Use
( ) ( n = 20n

!n

+ ... +

x +x +x

!3

!2

!1

= 1+

- ( .

( hh:mm:ss .21

PDF created with pdfFactory Pro trial version www.pdffactory.com

SQL Server

PDF created with pdfFactory Pro trial version www.pdffactory.com

SQL Server

Object ) (Alter ) (Drop )(Create

SQL Server Select

.
Object : SQL Server OOP Object .

] [ [ DataBaseName. ]Owner.]ObjectName[.
Master SQL Server
MyDB
:
MyDB.dbo.MyTBL.MyFld
Owner
dbo :
MyDB..MyTBL.MyFld
Object : Object ) Create ( ) Alter , ( ) Drop , (
Object


Drop,Alter,Create Object
:
22

PDF created with pdfFactory Pro trial version www.pdffactory.com

DataBase

Table

Index

View

Stored
Procedure

User Define
Function

Trigger

Default

User Define
Data Type

Extended
Stored
Procedure

DB

SP

UDF

UDDT

ESP

Create

Alter

Drop

Object Index Alter


Object Drop Create
Default
Create Drop
UDDT ESP
Drop , Alter , Create ESP
Object SP
.
: Create Table Object Wizard EnterPrise Manager

Create Table . :
( Create Table
] Unique Not Null ] [ Primary Key [ Null AS
] NonClustered ] [ Default [ Clustered
] [ Check
[,

23

]n
)

PDF created with pdfFactory Pro trial version www.pdffactory.com

Create Table
.

.
Null. Primary Key Unique .
Primary Key Clustered NonClustered
NonClustered .
Default .
Check .
.
: Create Table .
Person :
( Create Table Person
PID Int Not Null Primary Key Clustered ,
PName nVarChar(20) Not Null ,
PFamily nVarChar(30) Not Null ,
PFather nVarChar(20) ,
BCity
SmallInt ,
) UnitID SmallInt Not Null

PID Primary Key


Null.
24

PDF created with pdfFactory Pro trial version www.pdffactory.com

: Create Table
.
Unit :
( Create Table Unit
UntiID SmallInt Not Null Primary Key Clustered ,
UnitName nVarChar (100) Not Null
)
: Create Table
.
a2 = b2 + c2 a
. .
( Create Table Polygon
b Real Not Null ,
c Real Not Null ,
) )a As SQRT (Power (b,2) + Power (c,2
)
a c , b

.
: Create Table
.
PK Relate
:
( Create Table Relate
RelateID TinyInt Not Null Primary Key Clustered ,
) RelateName nVarChar (15) Not Null
25

PDF created with pdfFactory Pro trial version www.pdffactory.com

PrsChild :
( Create Table PrsChild
PID Int Not Null ,
RelateID TinyInt Default 1 Check (RelateID > = 1),
CName nVarChar (20) Not Null ,
CFamily nVarChar (30) Not Null ,
CFather nVarChar (20) ,
) BCity SmallInt

: Alter Table Alter Table .


Alter Table
] Not Null [ Null Alter Column
Alter Table

ADD
][, n

Alter Table
] [ ,

Column

Drop

: Person .
ADD .
Begin Tran
Alter Table Person
)ADD NationalCode VarChar (10
Commit Tran
26

PDF created with pdfFactory Pro trial version www.pdffactory.com

: Alter PFather .
Alter Column .
Begin Tran
Alter Table Person
Alter Column PFather nVarChar (25) Not Null
Commit
: Alter Person .
Begin Tran
Alter Table Person
Drop Column NationalCode
Commit TransAction
: Drop Table Drop Object Drop
.
Drop Table
: .
:
Drop Table Person
: Select Select SQL Select
:

, ] = [

] ][ [AS

* ]Select [Top n
] [ Into

] [ ] ][ [AS

From

] [ Where
] [ Group By
] [Having
] [ Order By
27

PDF created with pdfFactory Pro trial version www.pdffactory.com

:
Into . - .

, or , Not

and

=> = , <> ,< , > , <= ,

Is Null
and BetWeen
) ( In
) In ( SubQuery

. , Left , ABS
) ( UDF
% , + , _ , * , /

" " SubQuery .


:
SubQuery



28

PDF created with pdfFactory Pro trial version www.pdffactory.com

*
AS =

SQL Server . Top n n
.
:
Desc ] , ... [ASC SubQuery


Desc , ASC
ASC .

.
Select :

) ( ] On ] [ [AS

[ Inner ] Join

Left Join

Right Join
[


]
Inner Join .
Left Join .
Right Join .
29

PDF created with pdfFactory Pro trial version www.pdffactory.com

Join
PK FK
.
Group By
Having .
Select
Query

.

30

PDF created with pdfFactory Pro trial version www.pdffactory.com

: .
7 :

Person

Int

PrsID

)nVarChar(20

PName

)nVarChar(100

PFamily

SmallInt

)nVarChar(30

PK

)nVarChar(20

PFather

SmallInt

CityID

FK

UnitID

FK

SmallInt

BigInt

SmallInt

UnitID

UnitName

CityID

Price

Child

Int

PrsID

FK

TinyInt

ChdID

PK

)nVarChar(20

CName

Unit

SmallInt

CityID

)nVarChar (50

CityName

Int

PoPulate

CFamily

)nVarChar(20

CFather

TinyInt

SmallInt

RID

CityID

FK

)nVarChar (20

RName

RID

FK

TinyInt

Int

PrsID

LvL

FK

TinyInt

ChdID

TinyInt

LID

)Char(10

LDate

)nVarChar (20

LName

TinyInt

PK

ChdLvL

)nVarChar(200

PK

Relate

)nVarChar(30

FK

City

PK

PK

LPlace

LID

FK

31

PDF created with pdfFactory Pro trial version www.pdffactory.com

: .
, , , , ,
: Person City Unit
.
: SubQuery
Select :
Select PrsID, PName, PFamily, PFather,
(Select Top 1 CityName From City Where City.CityID=Prs.CityID)As CityN,
(Select UnitName From Unit Where UnitID=Prs.UnitID) As UnitN
From Person As Prs
Order By PrsID

: Join :
Select Prs.PrsID, Prs.PName, Prs.PFamily, Prs.PFather,
City. CityName As CityN, UnitN = Unit.UnitName
From Person As Prs
)Left Join City As City On (City.CityID = Prs.CityID
)Left Join Unit On (Unit.UnitID = Prs.UnitID
Order By 1
SubQuery City
CityN Person
Prs SubQuery
UnitN PrsID
Join
FK , PK
Join On
PrsID .
32

PDF created with pdfFactory Pro trial version www.pdffactory.com

. SP :
Child :
.
. Relate City SubQuery :
Create Proc GetChild1
@ PrsID Int
AS
Select CName, CFamily, CFather
(Select CityName From City Where CityID=Child.CityID)As CityName,
(Select RName From Relate Where RID=Child.RID)AS RName
From Child
Where PrsID = @PrsID
Order By RName

. Relate City Join :


Create Proc GetChild2
@PID Int
As
Select C.CName , C.CFamily , C.CFather,
City. CityName , R.RName
From Child As C
Left Join City On (City.CityID = C.CityID)
Left Join Relate As R On (R.RID = C.RID)
Where C.PrsID = @ PID
Order By R.RName
SP Join , SubQuery
.
Create Proc GetChild3
@P Int
As
Select C.CName , C.CFamily , C.CFather , City.CityName,
(Select Top 1 RName From Relate Where RID = C.RID ) As RName
Form Child As C
Left Join City On(City.CityID= C.CityID)
Where C.PrsID=@P
Order By (Select RName From Relate Where RID = C.RID ) Asc
33

PDF created with pdfFactory Pro trial version www.pdffactory.com

5 RName SubQuery Order By



SubQuery .
: ) ( Group By :

Min

Max

Avg

Sum

Count

SubQuery
.
: Select
.
, , , ,
) ( RID = 2 , 3
: Person Child
:
Select PrsID , PFamily , PName,
(Select Count (*) From Child Where PrsID=P.PrsID) As CT ,
(Select Count (*) From Child Where PrsID = P.PrsID
and RID In ( 2 , 3 ) ) As CC
From Person P
Where (Select Count (*) From Child Where PrsID = P.PrsID ) > 1
Order By 2, PName
34

PDF created with pdfFactory Pro trial version www.pdffactory.com

Person Child :
:
Select C1.PrsID,
(Select PFamily From Person Where PrsID = C1.PrsID) As PFamily,
(Select PName From Person Where PrsID = C1.PrsID) As PName,
Count (*) As CT,
(Select Count(*) From Child C2 Where PrsID=C1.PrsID and RID In(2,3)) As CC
From Child C1
Group By C1.PrsID
Having Count (*) > 1
Order By PFamily , 3


SubQuery 3 , 2 Having
. Order By
. SP :

.
Child , Person Unit :
:
Create Proc MIS1 AS
Select UnitName ,
(Select Count (*) From Person Where UnitID = U.UnitID )As PCnt ,
(Select Count (*) From Child Where
(Select UnitID From Person Where PrsID=Child.PrsID)=U.UnitID)As TCnt,
From Unit As U
Order By UnitName
35

PDF created with pdfFactory Pro trial version www.pdffactory.com

: Child ,Unit Person :


Create Proc MIS2 AS
Select (Select UnitName Form Unit Where UnitID = P1.UnitID)As UnitName,
Count (*) As PCnt,
(Select Count(*) From Child Where (Select UnitID From Person P2 Where PrsID
= Child.PrsID ) = P1.UnitID ) As TCnt
From Person P1
Group By UnitID
Order By 1

. :
. 6 3 SP :
: Person
Create Proc MySP1 AS
Select P.*,
(Select Count(*) From Child C Where PrsID = P.PrsID )As Cnt
From Person As P
Where (Select Count (*) From Child Where PrsID= P.PrsID) BetWeen 3 and 6
Order By PrsID
:
: n SP
. NoChild
Create Function NoChild (@P Int )
Returns TinyInt
As
Begin
Declare @Cnt TinyInt
Set @Cnt = (Select Count(*) From Child Where PrsID = @P )
Return @Cnt
END
36

PDF created with pdfFactory Pro trial version www.pdffactory.com

Cnt SubQuery Return


NoChild SP n
.
Create Proc MySP2 @n TinyInt
As
)Select * , dbo.NoChild (PrsID
From Person
Where dbo.NoChild (PrsID) > = @n
Order By PrsID
:
.


:
)Create Function dbo.MaxCL (@PID Int
Returns TinyInt
As
Begin
Return (Select Max(LID) From ChdLvL
) Where PrsID = @PID
END
SQL :
Select * , dbo.MaxCL(PrsID) AS CMCL ,
(Select LName From LvL Where LID=dbo.MaxCL(P.PrsID)) AS NMCL
From Person AS P
Order By CMCL Desc , PrsID
: .

:
)Create Function SumPoP (@CID SmallInt
Returns Int AS Begin
)Return (Select PoPulate From City Where CityID = @CID
END
37

PDF created with pdfFactory Pro trial version www.pdffactory.com

SQL .
Declare @S BigInt
) Set @S = ( Select Sum( PoPulate ) From City
Select * , dbo. Sumpop (CityID) / @S * 100 AS SPP
From Unit
Order By SPP Desc
: Insert Insert .
] Insert [ Into
] ) , ... , 2 [ ( 1
Values
) , ... , 2 ( 1


Null Default Null
Default
.
: .
Insert Person
) , 1 , 2 , , Values ( 200 ,
Insert 6 Person
.
: .
) Insert Into Person (PrsID , UnitID , CityID, PFather , PName , PFamily
) , , Values ( 201, 2 , 3 ,
) ( 38
PDF created with pdfFactory Pro trial version www.pdffactory.com

: UpDate UPDate .
Set

] ] [ [ AS

UpDate
, = 1 1
, = 2 2

] [ Where

.

PK
.
: UpDate 200
.
UpDate Person Set
, = PFamily
UnitID = 1
Where PrsID = 200
200
.
: UpDate %20 .
Price :
UpDate Person Set
) Price = Convert ( BigInt , Price * 1.2
: = 1 = 2
%25 :
) UpDate Person Set Price = Convert (BigInt , Price* 1.25
)Where CityID In (1,2
) ( 39
PDF created with pdfFactory Pro trial version www.pdffactory.com

: .
UpDate Person P Set Price = Price * ( 1 +
( Select Isnull (C.PoPulate ,0) Form Unit U
)Left Join City C On (C.CityID = U.CityID
)Where U.UnitID = P.UnitID
) ) ( Select Sum (PoPulate ) From City



:
) * ( 1 + =


SP
:
Create Proc NewPrice AS
Declare @SumPoP BigInt
) Set @SumPoP = ( Select Sum (PoPulate ) From City
Begin Tran
UpDate Person P Set Price = Price * ( 1 +
( Select Isnull (C.PoPulate , 0 ) From Unit U
)Left Join City C On ( C.CityID = U.CityID
) Where U.UnitID = P.UnitID ) / @ SumPoP
Commit Tran
: Delete Delete :

] ] [ Where ][ [AS

]Delete [From

) ( 40
PDF created with pdfFactory Pro trial version www.pdffactory.com

Where
.
:
.
Delete ChadLvL C Where C.PrsID %2 = 0
: .
Delete ChdLvL

) ( 41
PDF created with pdfFactory Pro trial version www.pdffactory.com

SQL Server

PDF created with pdfFactory Pro trial version www.pdffactory.com

.
Rushmore ESP View Trigger

.
: Declare
Declare

SQL Server .

Char

NChar

VarChar

nVarChar

binary

Varbinary

Numeric

18,0

Decimal

18,0

Dec

18,0

Cast Convert
30
Cast Convert
30
Cast Convert
30
Cast Convert
30
Cast Convert
30
Cast Convert
30

38

38

38


) ( 42

PDF created with pdfFactory Pro trial version www.pdffactory.com

nVarChar ,VarChar ,nChar ,Char ( ?



Declare @x ? (15)
Set @x = Hello
Select Len ( @x ) , DataLength ( @x )

.
Data Length

Len

15

Char

30

nChar

VarChar

10

nVarChar

(
Select

Len ( Convert (nChar , 300)) ,


DataLength (Cast (300 As nChar))
. 3,60
. nVarChar (

Declare @x nVarChar (4000) , @L1 SmallInt , @L2 SmallInt


Set
@x = Hello
Select @L1 = Len ( @x ) , @L2 = DataLength ( @x )
Select @L1 + @L2 AS L
( 43 )

PDF created with pdfFactory Pro trial version www.pdffactory.com

:



SQL Server .

SQL Server 9
:

Ascii

)Select Ascii (A

65

Char

)Select Char (65

)Select CharIndex (o,Hello

)Select Left (Hello , 2

He

)Select Len (Hello

CharIndex

Left

Len

) ( 44

PDF created with pdfFactory Pro trial version www.pdffactory.com

Lower
LTrim

)Select Lower (Hello

hello

)Hello

( Select LTrim

Hello

unicode

NChar

)Select NChar (65

65535

Replace

Replicate

Reverse

Right
RTrim
Space

)Select Replace (abc,a,x

xbc

) Select Replicate (ok,2

okok

)Select Reverse (Hello

olleH

) Select Right (Hello,2

lo

Select RTrim (Hello


)Select Space (5

Hello

Str

10

) )Select Len ( Str (500.5

10

Stuff
Substring

) Select Str (500.3,5,2

500.3

) Select Str (500.3188,7,3

500.319

) Select Stuff (Hello,2,1,ok

Hokllo

) Select Substring (Hello,3,2

ll

) ( 45

PDF created with pdfFactory Pro trial version www.pdffactory.com

Unicode

Unicode

Upper

) Select Unicode ( @Mystr

)Select Upper (Hello

HELLO

ABS

)Select ABS (-1.7

1.7

)Select Ceiling (123.45

124

)Select Exp (0

)Select Floor (-2.7

-3

) )Select Log (Exp (1

)Select Log10 (100

)Select Power (10,2

100
-

Ceiling

Exp

ex

Floor

x ] [x

) e(

Log

Log10

10

Power

ab

Rand

0 1

) ( Select Rand

Round

) Select Round (123.4567,2

123.4600

) Select Sign ( -3.7

-1
25.0
1.2

Sign

+1 , -1 , 0

SQuare

) Select SQuare ( -5

SQrt

) Select SQrt (1.44

) ( 46

PDF created with pdfFactory Pro trial version www.pdffactory.com

ACos
ASin

)Select ACos (1

0.0

)Select ASin (0

0.0

)Select ATtan (1

0.785398
1.0

ATAN

Cos

)Select Cos (0

Cot

)Select Cot (1

0.64209

)Select Degrees (PI( ) /2

90

) ( Select PI

3.141592

)Select Radians (180.0

3.141592
1.0
0.999999

Degrees

PI

Radians

Sin

) )Select Sin (Radians (90.0

Tan

) Select Tan ( PI ( ) /4

) ( APP_Name

Cast

Convert

Cast

) ( 47

PDF created with pdfFactory Pro trial version www.pdffactory.com

Current _User

Owner

DataLength
@@Error
) ( Host _ID
) ( Host_Name
IsNull
IsNumeric
NewID
@@RowCount
RowCount_Big

)Select DataLength (100



Null


UniquIdentifier

)Select IsNumeric (12

) ( Select NewID

Select UpDate ...


@@RowCount

SQL Server

System_User

Select System _User

SQL Server

Current_User

User_Name

User

)Select User_Name (1

CheckSum

) ( 48

PDF created with pdfFactory Pro trial version www.pdffactory.com

Check

Sum (
.

Select

* , CheckSum (*) From Child

( 49 )

PDF created with pdfFactory Pro trial version www.pdffactory.com

: View
View
View Order By
) Into ,( Top Select
* Select View
View
View SP

Select

View .
] ) ( [ Create View
][With SchemaBinding
As
Select
Select

Select SubQuery
. Select
SP View .

( View .
] [ ] , [ ] , [ ] , [ ] , [

Join
.
Create View V1 As
Select No=PrsID,PFamily,PName,C.CityName As CN ,
(Select Count(*) From Child Where PrsID=P.PrsID) As Cno
From Person P
)Left Join City C On(C.CityID=P.CityID
) ( 50

PDF created with pdfFactory Pro trial version www.pdffactory.com

( View V1
2 .
] [ ] , [ ] , [ ] , [
:
Select No,PName , PFamily , Cno
From V1
Where Cno > 2
Order By PFamily , PName
( View
10 .
] [ ] , [

Group By Person
:
Create View V2
AS
Select (Select UnitName From Unit Where UnitID= P.UnitID )AS UN,
Count (*) AS No
From Person P
Group By UnitID
Count (*) >10

Having

( View V2 SP 30 50
.
Create Proc GetUnitNo As
Select * From V2
Where No Between 30 and 50
Order By No Desc
) ( 51

PDF created with pdfFactory Pro trial version www.pdffactory.com

( View V2 .
No
View :

Select No ,Count (*) From V2


Group By No
Having Count (*) > 1
Order By No Desc
Union : Select

Union Select
Union
Union All Union
.
Select

] ][ Union [All
Select
.
.
.
( Union .
Union :
City

Select * From
Union

City

Select

* From

Union All :
City

Select * From
Union All

City

Select

* From

) ( 52

PDF created with pdfFactory Pro trial version www.pdffactory.com

Union
City City
City City
.
( .
D

3.7
4.8
5.6
6.7

Ok
Yes
No
Ok

10
15
30
40

1
2
3
4

Union Select .
A=1 , B=10 , C= Ok , D=3.7 Union
A=2 , B=15 , C= Yes , D=4.8 Union
A=3 , B=30 , C= No , D=5.6 Union
A=4 , B=40 , C= Ok , D=6.7

Select
Select
Select
Select

: Select
, Sum , Count Null

Count :

) *


Null

) ( All


Null ) ( Distinic
) ( 53

PDF created with pdfFactory Pro trial version www.pdffactory.com

: Insert Into
Insert

.

] [ Into

Insert

Select
] [ EXEC[ute] SP

] [ Into

Insert

]EXEC[ute

] [ Into

Insert

] [ Into

Insert

) (

View

SP
View Select Insert .
( MyCity
MyPerson City .
:
Insert Into City Select * From MyPerson . . MyCity
: Trigger
Trigger Event View
Object
. View
Trigger Trigger Instead Of , After
Delete ,UpDate , Insert :

After

SQL

Insert
UpDate

View

[Instead Of

Delete

) ( 54

PDF created with pdfFactory Pro trial version www.pdffactory.com

Trigger After
UpDate , Insert Delete Event
Trigger Instead Of View Trigger
Trigger Insert

Trigger
Trigger Instead Of
Trigger :
View

ON

Trigger

Trigger

Create

Instead Of After
] [ Insert ] [ , ] [ UpDate ] [ , ] [ Delete
As
SQL
Trigger :
View Trigger Instead Of . Delete , UpDate , Insert Trigger View .
Trigger Event Trigger
Trigger SP

SP_SettriggerOrder Firs/Last , Trigger


.
Trigger 100 UpDate
Trigger UpDate .

) ( 55

PDF created with pdfFactory Pro trial version www.pdffactory.com

Trigger Deleted , Inserted


.
Deleted

Inserted

Trigger
Insert

UpDate

Delete

Trigger .
Deleted , Inserted .
Trigger Object .
SQL Server For After .
Trigger Instead Of FK .
) ( UpDate UpDate .
) ( Columns_UpDate UpDate .
Trigger @@RowCount . Recursive Trigger Trigger .

) ( 56

PDF created with pdfFactory Pro trial version www.pdffactory.com

LogFile (
. Trigger .
Person Delete
: LogFile
Create Trigger DelPrs On Person
After Delete
AS
Insert Into LogFile Select D AS LogType , PrsID , GetDate ( ) As LogDate
From Deleted
LogFile Trigger (
.
.

Delete , UpDate , Insert Trigger (

.
Create Trigger I_Prs On Person
After Insert AS
Insert Into LogFile Select I AS LogType,
PrsID ,
GetDate ( ) AS LogDate
From Inserted

Create Trigger U_Prs On Person


After UpDate AS
Insert Into LogFile Select U AS LogType,
PrsID,
GetDate ( ) AS LogDate
From Deleted

Create Trigger D_Prs On Person


After Delete AS
Insert Into LogFile Select D AS LogType,
PrsID,
GetDate ( ) AS LogDate
From Deleted
( 57 )

PDF created with pdfFactory Pro trial version www.pdffactory.com

Trigger

Trigger (

. Delete , UpDate , Insert


Create Trigger IUD_Prs On Person
After Insert , UpDate , Delete AS
Declare @I BigInt , @D BigInt , @ T Char (1) , @ S VarChar (500)
Set @I = (Select Count (*) From Inserted )
Set @D = (Select Count (*) From Deleted )
Set @T = Case
When @ I < > 0 and @ D = 0
Then I
When @ I < > 0 and @ D < > 0 Then U
When @ I = 0
and @ D < > 0 Then D
END
If @T =I
Insert Into LogFile Select @T As LogType, PrsID ,GetDate ( )
As LogDate From Inserted
Else
Insert Into LogFile Select @T As LogType ,PrsID, GetDate ( )
As LogDate From Deleted
T Trigger
LogFile Deleted Inserted
.
. 1000 Trigger (
: Instead of Trigger
Create Trigger PrsValid On Person
Instead Of Insert , UpDate AS
IF (Select Count (*) From Inserted Where PrsID < 1000 ) = 0
Insert Into Person Select * From Inserted
ReCursive
Check 1000
.
( 58 )

PDF created with pdfFactory Pro trial version www.pdffactory.com

Trigger
Check
Trigger .
: Index

PK FK
View
:
Index
) ] Desc ] [ ,

] NonClustered

[ Asc (

[ Unique ] [ Clustered

ON

Create

Clustered NonClustered NonClustered .


.
Unique .
View . Asc
.
SchemaBinding View .
View Owner .( :
Clustered
:
)Create Unique Clustered Index Ix_UID ON Unit (UnitID
) ( 59

PDF created with pdfFactory Pro trial version www.pdffactory.com

( .

PK
:
Create Unique Clustered Index Ix_PC
) ON Child ( PrsID , ChdID


.
.
:


Select Order By
.
:

.



:
PK .
) ( 60

PDF created with pdfFactory Pro trial version www.pdffactory.com

Where PK
.

.
: Rushmore

.
Rushmore
.

100 1000 .
2217 42818 4/61

) ( 61

PDF created with pdfFactory Pro trial version www.pdffactory.com

0/16 29

. Rushmore


. Where
Order By Rushmore
Rushmore :
Rushmore
Rushmore .
.
Rushmore


.
Rushmore .
Rushmore
Rushmore .


.
) ( 62

PDF created with pdfFactory Pro trial version www.pdffactory.com

(

Order By ChdID

Select * From Child Where PrsID = @ P

PrsID
ChdID PK
PrsID ChdID
.
: ( ESP ) Extended Stored Procdures
ESP Extended Stored Procdures
VC++ DLL
SQL Server DLL
Master Wizard
SP SP_addextendedproc EXEC
:
ESP SP_addextendedproc . DLL ESP Bin SQL DLL.
ESP SP_addextendedproc .
ESP . EXEC ESP .( SQL Public TinyInt
Sever .
:
) ( 63

PDF created with pdfFactory Pro trial version www.pdffactory.com

: Globals VC++ ESP # include < stdafx.h >


unsigned char ID = 0 ;
# ifdef __ cplusplus
extern C {
# endif
RVRETCODE __declspec (dllexport) Xp_MyID (SRV_PROC *srvproc) ;
# ifdef __ cplusplus
}
# endif
//--- Get New ID ---SRVRETCODE __declspec (dllexport) Xp_MyID (SRV_PROC *srvproc)
{
if (ID == 255) ID = 0 ;
++ ID ;
return ID ;
}
. C:\MyID\ MyProjID DLL SQL DLL SP_addextendedproc . Server
Use Master
EXEC SP_addextendedproc

Xp_MyID , ` C:\MyID\MyProjID.dll `

. ESP Xp_MyID
: ESP GNID Create Function GNID ( )
Returns TinyInt
As
Begin
Declare @R TinyInt
EXEC @R = master. . Xp_MyID
Return @R
End
( 64 )

PDF created with pdfFactory Pro trial version www.pdffactory.com

ESP : SQL Server


ESP SQL Server
.
ESP
xp_Cmdshell

xp_Readmail

xp_Sendmail

xp_msver

Dos Server

Master . . xp_cmdshell
Dir C:\

mail in box
mail

Query
mail

Query SQL Server


master . . xp_msver

) ( 65

PDF created with pdfFactory Pro trial version www.pdffactory.com

SP : SQL Server
ESP SP SQL Server
:
SP

SP_adduser

SP_attach_db

SP_colums

SP_databases

SP_datatype_info

SP_detach_db

SP_fkeys

FK
SP_databases

SP_helpdb

SP_helpdevice

Back Up SQL

SP_helpfile

...

SP_helpindex

SP_helpuser

User

SP_monitor

SQL Server

SP_ password

SP_ Pkeys

PK

SP_ Rename

Object ...

SP_ Renamedb

SP_Stored_Procedures

SP UDF

SP_Tables

SP_addumpdevice

Device Back Up SQL Server

) ( 66

PDF created with pdfFactory Pro trial version www.pdffactory.com

: Cursor
SQL Select


.
. Select . Open . Fetch .
Close . Deallcate . :
Declare
:
] Global [ Local
] Dynamic Keyset

Cursor

Scroll ] [ Static

] Optimistic

Scroll_Locks

Declare

[ Forward_Only

[ Read_Only

Select

For

] ] [ For UpDate [ Of

:
Local SP Trigger .
Global SP
.
) ( 67

PDF created with pdfFactory Pro trial version www.pdffactory.com

Forward_Only
Scroll .
Static TempDB
.
Keyset .
Dynamic .
Read_Only . Scroll_Locks .
Optimistic TimeStamp
CheckSum .
Select Select Into .
For UpDate . ) : ( Fetching
ADO
Fetch Fetch :
Last
]

First

Relative

Prior

Fetch [ Next

Absolute

From

[ Into

) ( 68

PDF created with pdfFactory Pro trial version www.pdffactory.com

:
Next . Prior . First . Last . Absolute . Relative -

Into .
@@Fetch_Status
.
( .
ChdLvL Fetch
:
) Create Function AvgLvL (@PrsID Int
Returns Real
AS Begin
Declare @i TinyInt , @L Real , @A Real
Set @i = 0 Set @A = 0
Declare TmPCursor Cursor For
Select LID From ChdLvL Where PrsID = @PrsID
Open TmPCursor
While 1=1 Begin
Fetch Next From TmPCursor Into @L
If @@ Fetch_Status < > 0 Break
Set @i = @i + 1
Set @A= @A +@L
End
Close TmPCursor
Deallocate TmPCursor
If @i < >0 Set @A = @A / @i
Return @A
END
) ( 69

PDF created with pdfFactory Pro trial version www.pdffactory.com

AvgLvL
TmPCursor Select PrsID

@@Fetch_Status Close
Deallocate

Select
Select

.

:
SQL Server

LinkedServer ,OpenRowSet , OpenDataSource
Provider ConnectionString
.

: OpenDataSource
OpenDataSource ConnectionString , Provider
Provider
OLE_DB ConnectionString
. Provider
OLE_DB ODBC ODBC
.

) ( 70

PDF created with pdfFactory Pro trial version www.pdffactory.com

( :
RServer
:
Select P.* , R.UnitName
From Person P
; Left Join OpenDataSource (SQLOLEDB,Data Source = RServer
User ID = sa;Password=123 ). PersonDB.dbo.Unit R
) On ( R.UnitID = P.UnitID

SQL Server
.
Select P.* , R.UnitName
From Person P
Left Join OpenDataSource (Microsoft.Jet.OLEDB.4.0,
C:\ PersonDB.XLS;User ID=Admin;Password=123;Extended
Properties=Excel 5.0 )Unit R
) On ( R.UnitID = P.UnitID
Unit Spread Sheet PersonDB.XLS
Engine .
: OpenRowSet
OpenRowSet Query Provider
Provider
OpenDataSource Provider
Query . Select
Delete , Insert , UpDate .

) ( 71

PDF created with pdfFactory Pro trial version www.pdffactory.com

( OpenRowSet RServer
.
Select P.* , U.UnitName , C.CityName From
OpenRowSet(SQLOLEDB , RServer ;sa;123,
Select * From Person DB.dbo.Person Order By PrsID
) As P
Left Join OpenRowSet (Microsoft.Jet.OLEDB.4.0 ,
C:\PersonDB.MDB;Admin;123,Unit ) As U
) On (U.UnitID = P.UnitID
) Left Join City C On (C.CityID = P.CityID
OpenDataSource , OpenRowSet
Books OnLine .
: Linked Server
Linked Server
OpenRowSet , OpenDataSource
Provider OLE_DB .
ODBC Provider
OLE_DB ODBC . Linked Server
.
SP SP_addLinkedServer OLE_DB .
OpenQuery SQL . SP_LinkedServers Linked Server .
SP_dropServer Linked Server .
OpenDataSource OpenRowSet Linked Server OpenQuery SP
Linked Server .
) ( 72

PDF created with pdfFactory Pro trial version www.pdffactory.com

SQL SP EXEC .( RServer


SP .
RServer SQL Server
.
RServer .Use Master
Go
SP_AddLinkedServer MyLinked , , MSDASQL , Null , Null ,
;Driver = {SQL Server};Database=PersonDB;Server=Rserver
UID=sa;PWD=123;
Linked Server MyLinked .
Create Proc GetPrs
As
Select P.* , U.UnitName From Person P
Left Join OpenQery(MyLinked ,Select * From Unit) U
)On (U.UnitID=P.UnitID
RServer
MyLinked .
Use Master
Go
SP_AddLinkedServer MyLinked ,OLE DB Provider for Jet ,
Microsoft.Jet.OLEDB.4.0 , C:\PersonDB.mdb

SP_AddLinkedServer Books OnLine .
) ( 73

PDF created with pdfFactory Pro trial version www.pdffactory.com



SQL Server

PDF created with pdfFactory Pro trial version www.pdffactory.com

-1
Declare @x TinyInt , @y BigInt
Set @y = 1
While @x < 5 Begin
Set @x = @x + 1
Set @y = @y * @x
END
Select @y
24 (2

120 (1

(4

1 (3

-2 SQL Variant
Fy .
Then
)Then dbo.Fx (@A
) )Then dbo.Fx (dbo.Fx (@A

) (@D = dbo.Fy
@A = @B
@D = @B + @C
) IsNull (@E,

Case
When
When
When
ELSE

Select

END
(1

(3

(2

(4

-3
Declare @x nVarChar Set @x = SQL
)Select Len (@x

1 (1

3 (3

2 (2

6 (4

-4

TinyInt (1

Int (3

SmallInt (2

BigInt (4

)(

PDF created with pdfFactory Pro trial version www.pdffactory.com

x -5
Declare @x TinyInt
Set @x = 10
IF @x%2 = 0
Set @x = @x 2
ELSE
Set @x = Case When @x = Power (2,2) Then @x-3 end
Set @x = Case When @x = Power (4,2) Then @x-2 end
Set @x = Case When @x = Power (3,2) Then @x-1
END
Select @x
( 4

4 (3

5 (2

8 (1

-6

x3
x5
x
+

3
!
5
!
T ( x) =
x2
x4
1
+

2!
4!

()

PDF created with pdfFactory Pro trial version www.pdffactory.com

-1 (0/5) .
Declare @x , @y BigInt
Set @y = 1
While @x < 5 Begin
Set @x = @x + 1
Set @y = @y * @x
END
Select @y

120 (1

24 (2

1 (3

(4

-2 )(0/5
Declare @x TinyInt , @S Int
Set @x = 0 Set @S = 0
While @x < 3
Set @x = @x + 1
) Set @S = @S + Power (@x , 2
Select @S

9 (1

14 ( 3

11 ( 2

( 4

-3
)(0/5

TinyInt ( 1

Int ( 3

SmallInt ( 2

BigInt ( 4

)(

PDF created with pdfFactory Pro trial version www.pdffactory.com

(0/5) -4
Declare @x TinyInt
Set @x = 0
Select Case @x
When 0 Then
When 1 Then
END Select @x
( 4

@x + 1
@x + 2

2 (3

1 (2

0 (1

(0/5) -5
Declare @x TinyInt , @y TinyInt , @z TinyInt
Select @x = 10 , @y = 13 , @z = 5
If @x < @y
If @x < @z Select @x
Else
Select @y
Else
Select @z
( 4

z (3

y (2

x (1

(2/5) . SP -6

x3 x5
x4 x6
2
L(x) = (x + ......) (x + ......)
2! 4!
3! 5!

()

PDF created with pdfFactory Pro trial version www.pdffactory.com

: :

-1 Object Alter ) 0/5(


Trigger ( 2

Index ( 1

Default ( 4

ESP ( 3

-2 ) 1(
Select

)PrsID,(Select Top 2 From City C Where P.CityID = CityID


UnitID
From Person P
)Join Unit U On (UnitID = P.UnitID
Order By Select Top 3 UnitName From Unit
Where CityID = P.CityID
)Where X(Price

-3 ) 2(

-4 SubQuery
) 1/5(

............

...........................

............

............

...........................

...............

)(

PDF created with pdfFactory Pro trial version www.pdffactory.com

PK

FK

PrsID

Int

UnitID

SmallInt

UnitName

nVarChar(100)

PName

CityID

SmallInt

PFamily

nVarChar(30)

PFather

nVarChar(20)

PK

CityID

SmallInt

CityName

nVarChar (50)

PoPulate

Int

CityID

FK

UnitID

SmallInt

Price

BigInt

Child

SmallInt

FK

PrsID

Int

PK

ChdID

TinyInt

CName

nVarChar(20)

CFamily

nVarChar(30)

RID

TinyInt

CFather

nVarChar(20)

RName

nVarChar (20)

FK

CityID

FK

RID

LvL

FK

TinyInt

PrsID

Int

TinyInt

LID

TinyInt

ChdID

LName

nVarChar (20)

LDate

FK

SmallInt

ChdLvL

PK

nVarChar(20)

FK

Relate

PK

City

PK

Person

Unit

Char(10)

LPlace

nVarChar(200)

LID

TinyInt

()

PDF created with pdfFactory Pro trial version www.pdffactory.com

: :

-1 Select Round (123,-2)/3+Len(Str(@x))/3 )(0/25

36.66666 ( 1

( 3 x

36 ( 2

( 4 -2

-2 ) (@M )
( )(0/25
Select 31-@M/7-@M/12 ( 1
Select 31- Round (@M/7,0)-Round (@M/12,0) ( 2
Select 31-Floor (@M/7)-Floor(@M/12) ( 3
( 4

-3 SQL Server
Object )(0/25

SP ( 1

View ( 2

ESP ( 3

(4

-4 SP
)(0/25
OpenDataSource ( 1

OpenRowSet ( 2

OpenQuery ( 3

( 4

-5
)(0/5

)(
PDF created with pdfFactory Pro trial version www.pdffactory.com

-6
PIndex (3/5) .
Child

Person

BigInt

PID

TinyInt

PK

)Char(10

PK

CID

Bdate


PK

BigInt

PID

BigInt

PIndex

YYYY/MM/DD

1 n 1
t 2

n 1 i =1

= PIndex

= n
) ( = t

:
( Child .
(
.
( .

)(

PDF created with pdfFactory Pro trial version www.pdffactory.com

You might also like