Professional Documents
Culture Documents
:
Kaffash@RAI.IR
86
. SQL Server
%20
%100
SQL Server
.
.
SQL Server .
SQL Server
: RDBMS RDBMS ) (Relational Data Base Management System
Object .
: DBA
.
: Object View Trigger RDBMS .
: Table
. :
PK
PK
FK
FK
PK
FK
FK
FK
:
-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
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
(2
(3
(4
(5 ) (UDF
(6
SQL Server :
- 1 :
.
)unicode (UC
)unicode (UC
Binary
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
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
: SQL Server
SQL
Server @ .
@ Declare
] [ ,
0 255 .
TinyInt
Declare @x
Set Select
Select Set
.
= @ Set
] [ , = @ Select
:
@x = 14
Set
@x = @x +2*@ y
Set
End
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 .
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
2 :
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
:
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
: 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
: 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
: Replace .
) , , ( Replace
:
)Set @x = Replace (Hello,ale,xyz
ale xyz x Hzyyo
.
: Space .
) ( Space
) , ( Power
14
: Sqrt Sqrt .
) ( Sqrt
: Isnull Null .
Null
) , ( Isnull
Null
:
)Set @x = Isnull (@y,0
y Null x
x y .
: GetDate Server . : Convert .
) , ( Convert
.
15
:
) 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
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
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
: .
)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
: . :
: 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
]][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
SQL Server
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
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
23
]n
)
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
: 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
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
ADD
][, n
Alter Table
] [ ,
Column
Drop
: Person .
ADD .
Begin Tran
Alter Table Person
)ADD NationalCode VarChar (10
Commit Tran
26
: 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
:
Into . - .
, or , Not
and
Is Null
and BetWeen
) ( In
) In ( SubQuery
. , Left , ABS
) ( UDF
% , + , _ , * , /
28
*
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
Join
PK FK
.
Group By
Having .
Select
Query
.
30
: .
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
: .
, , , , ,
: 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
. 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
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
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
. :
. 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
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
.
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
.
Data Length
Len
15
Char
30
nChar
VarChar
10
nVarChar
(
Select
:
SQL Server .
SQL Server 9
:
Ascii
)Select Ascii (A
65
Char
He
CharIndex
Left
Len
) ( 44
Lower
LTrim
hello
)Hello
( Select LTrim
Hello
unicode
NChar
65535
Replace
Replicate
Reverse
Right
RTrim
Space
xbc
okok
olleH
lo
Hello
Str
10
10
Stuff
Substring
500.3
500.319
Hokllo
ll
) ( 45
Unicode
Unicode
Upper
HELLO
ABS
1.7
124
)Select Exp (0
-3
100
-
Ceiling
Exp
ex
Floor
x ] [x
) e(
Log
Log10
10
Power
ab
Rand
0 1
) ( Select Rand
Round
123.4600
-1
25.0
1.2
Sign
+1 , -1 , 0
SQuare
) Select SQuare ( -5
SQrt
) ( 46
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
90
) ( Select PI
3.141592
3.141592
1.0
0.999999
Degrees
PI
Radians
Sin
Tan
) Select Tan ( PI ( ) /4
) ( APP_Name
Cast
Convert
Cast
) ( 47
Current _User
Owner
DataLength
@@Error
) ( Host _ID
) ( Host_Name
IsNull
IsNumeric
NewID
@@RowCount
RowCount_Big
Null
UniquIdentifier
) ( Select NewID
System_User
SQL Server
Current_User
User_Name
User
)Select User_Name (1
CheckSum
) ( 48
Check
Sum (
.
Select
( 49 )
: 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
( 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
( View V2 .
No
View :
] ][ Union [All
Select
.
.
.
( Union .
Union :
City
Select * From
Union
City
Select
* From
Union All :
City
Select * From
Union All
City
Select
* From
) ( 52
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
: 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
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
) ( 55
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
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 (
.
.
.
Create Trigger I_Prs On Person
After Insert AS
Insert Into LogFile Select I AS LogType,
PrsID ,
GetDate ( ) AS LogDate
From Inserted
Trigger
Trigger (
Trigger
Check
Trigger .
: Index
PK FK
View
:
Index
) ] Desc ] [ ,
] NonClustered
[ Asc (
[ Unique ] [ Clustered
ON
Create
( .
PK
:
Create Unique Clustered Index Ix_PC
) ON Child ( PrsID , ChdID
.
.
:
Select Order By
.
:
.
:
PK .
) ( 60
Where PK
.
.
: Rushmore
.
Rushmore
.
100 1000 .
2217 42818 4/61
) ( 61
0/16 29
. Rushmore
. Where
Order By Rushmore
Rushmore :
Rushmore
Rushmore .
.
Rushmore
.
Rushmore .
Rushmore
Rushmore .
.
) ( 62
(
Order By ChdID
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
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 )
xp_Readmail
xp_Sendmail
xp_msver
Dos Server
Master . . xp_cmdshell
Dir C:\
mail in box
mail
Query
mail
master . . xp_msver
) ( 65
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
) ( 66
: 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
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
:
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
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
( :
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
( 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
SQL Server
-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
)(
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!
()
-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
)(
(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!
()
: :
Index ( 1
Default ( 4
ESP ( 3
-2 ) 1(
Select
-3 ) 2(
-4 SubQuery
) 1/5(
............
...........................
............
............
...........................
...............
)(
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
()
: :
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 .
(
.
( .
)(