行列互轉
create
table
test(id
int
,name
varchar
(
20
),quarter
int
,profile
int
)
insert
into
test
values
(
1
,
'
a
'
,
1
,
1000
)
insert
into
test
values
(
1
,
'
a
'
,
2
,
2000
)
insert
into
test
values
(
1
,
'
a
'
,
3
,
4000
)
insert
into
test
values
(
1
,
'
a
'
,
4
,
5000
)
insert
into
test
values
(
2
,
'
b
'
,
1
,
3000
)
insert
into
test
values
(
2
,
'
b
'
,
2
,
3500
)
insert
into
test
values
(
2
,
'
b
'
,
3
,
4200
)
insert
into
test
values
(
2
,
'
b
'
,
4
,
5500
)
select
*
from
test
--
行轉列
select
id,name,
[
1
]
as
"一季度",
[
2
]
as
"二季度",
[
3
]
as
"三季度",
[
4
]
as
"四季度",
[
5
]
as
"
5
"
from
test
pivot
(
sum
(profile)
for
quarter
in
(
[
1
]
,
[
2
]
,
[
3
]
,
[
4
]
,
[
5
]
)
)
as
pvt
create
table
test2(id
int
,name
varchar
(
20
), Q1
int
, Q2
int
, Q3
int
, Q4
int
)
insert
into
test2
values
(
1
,
'
a
'
,
1000
,
2000
,
4000
,
5000
)
insert
into
test2
values
(
2
,
'
b
'
,
3000
,
3500
,
4200
,
5500
)
select
*
from
test2
--
列轉行
select
id,name,quarter,profile
from
test2
unpivot
(
profile
for
quarter
in
(
[
Q1
]
,
[
Q2
]
,
[
Q3
]
,
[
Q4
]
)
)
as
unpvt
sql替換字符串 substring replace
--
例子1:
update
tbPersonalInfo
set
TrueName
=
replace
(TrueName,
substring
(TrueName,
2
,
4
),
'
**
'
)
where
ID
=
1
--
例子2:
update
tbPersonalInfo
set
Mobile
=
replace
(Mobile,
substring
(Mobile,
4
,
11
),
'
********
'
)
where
ID
=
1
--
例子3:
update
tbPersonalInfo
set
Email
=
replace
(Email,
'
chinamobile
'
,
'
******
'
)
where
ID
=
1
SQL查詢一個表內相同紀錄 having
若是一個ID能夠區分的話,能夠這麼寫
select
*
from
表
where
ID
in
(
select
ID
from
表
group
by
ID
having
sum
(
1
)
>
1
)
若是幾個ID才能區分的話,能夠這麼寫
select
*
from
表
where
ID1
+
ID2
+
ID3
in
(
select
ID1
+
ID2
+
ID3
from
表
group
by
ID1,ID2,ID3
having
sum
(
1
)
>
1
)
其餘回答:數據表是zy_bho,想找出ZYH字段名相同的記錄
--
方法1:
SELECT
*
FROM
zy_bho a
WHERE
EXISTS
(
SELECT
1
FROM
zy_bho
WHERE
[
PK
]
<>
a.
[
PK
]
AND
ZYH
=
a.ZYH)
--
方法2:
select
a.
*
from
zy_bho a
join
zy_bho b
on
(a.
[
pk
]
<>
b.
[
pk
]
and
a.zyh
=
b.zyh)
--
方法3:
select
*
from
zy_bbo
where
zyh
in
(
select
zyh
from
zy_bbo
group
by
zyh
having
count
(zyh)
>
1
)
--
其中pk是主鍵或是 unique的字段。
把多行SQL數據變成一條多列數據,即新增列
Select
DeptName
=
O.OUName,
'
9G
'
=
Sum
(
Case
When
PersonalGrade
=
9
Then
1
Else
0
End
),
'
8G
'
=
Sum
(
Case
When
PersonalGrade
=
8
Then
1
Else
0
End
),
'
7G4
'
=
Sum
(
Case
When
PersonalGrade
=
7
AND
JobGrade
=
4
Then
1
Else
0
End
),
'
7G3
'
=
Sum
(
Case
When
PersonalGrade
=
7
AND
JobGrade
=
3
Then
1
Else
0
End
),
'
6G
'
=
Sum
(
Case
When
PersonalGrade
=
6
Then
1
Else
0
End
),
'
5G3
'
=
Sum
(
Case
When
PersonalGrade
=
5
AND
JobGrade
=
3
Then
1
Else
0
End
),
'
5G2
'
=
Sum
(
Case
When
PersonalGrade
=
5
AND
JobGrade
=
2
Then
1
Else
0
End
),
'
4G
'
=
Sum
(
Case
When
PersonalGrade
=
4
Then
1
Else
0
End
),
'
3G2
'
=
Sum
(
Case
When
PersonalGrade
=
3
AND
JobGrade
=
2
Then
1
Else
0
End
),
'
3G1
'
=
Sum
(
Case
When
PersonalGrade
=
3
AND
JobGrade
=
1
Then
1
Else
0
End
),
'
2G
'
=
Sum
(
Case
When
PersonalGrade
=
2
Then
1
Else
0
End
),
'
1G
'
=
Sum
(
Case
When
PersonalGrade
=
1
Then
1
Else
0
End
),
--
' 未定級'=Sum(Case When PersonalGrade=NULL Then 1 Else 0 End)
表複製
insert
into
PhoneChange_Num (
[
IMSI
]
,Num)
SELECT
[
IMSI
]
,
count
(
[
IMEI
]
)
as
num
FROM
[
Test
]
.
[
dbo
]
.
[
PhoneChange
]
group
by
[
IMSI
]
order
by
num
desc
語法1:Insert INTO table(field1,field2,...) values(value1,value2,...)javascript
語法2:Insert into Table2(field1,field2,...) select value1,value2,... from Table1(要求目標表Table2必須存在,因爲目標表Table2已經存在,因此咱們除了插入源表Table1的字段外,還能夠插入常量。)java
語法3:SELECT vale1, value2 into Table2 from Table1(要求目標表Table2不存在,由於在插入時會自動建立表Table2,並將Table1中指定字段數據複製到Table2中。)sql
語法4:使用導入導出功能進行全表複製。若是是使用【編寫查詢以指定要傳輸的數據】,那麼在大數據表的複製就會有問題?由於複製到必定程度就再也不動了,內存爆了?它也沒有寫入到表中。而使用上面3種語法直接執行是會立刻刷新到數據庫表中的,你刷新一下mdf文件就知道了。 數據庫
利用帶關聯子查詢Update語句更新數據
--
方法1:
Update
Table1
set
c
=
(
select
c
from
Table2
where
a
=
Table1.a)
where
c
is
null
--
方法2:
update
A
set
newqiantity
=
B.qiantity
from
A,B
where
A.bnum
=
B.bnum
--
方法3:
update
(
select
A.bnum ,A.newqiantity,B.qiantity
from
A
left
join
B
on
A.bnum
=
B.bnum)
AS
C
set
C.newqiantity
=
C.qiantity
where
C.bnum
=
XX
鏈接遠程服務器
--
方法1:
select
*
from
openrowset
(
'
SQLOLEDB
'
,
'
server=192.168.0.67;uid=sa;pwd=password
'
,
'
SELECT * FROM BCM2.dbo.tbAppl
'
)
--
方法2:
select
*
from
openrowset
(
'
SQLOLEDB
'
,
'
192.168.0.67
'
;
'
sa
'
;
'
password
'
,
'
SELECT * FROM BCM2.dbo.tbAppl
'
)
Date 和 Time 樣式
不帶世紀數位 (yy) (1) |
帶世紀數位 (yyyy) |
標準 |
輸入/輸出 (3) |
-express |
0 或 100 (1,2)緩存 |
默 認服務器 |
mon dd yyyy hh:miAM(或 PM)post |
1性能 |
101測試 |
美 國 |
mm/dd/yyyy |
2 |
102 |
ANSI |
yy.mm.dd |
3 |
103 |
英 國/法國 |
dd/mm/yyyy |
4 |
104 |
德 國 |
dd.mm.yy |
5 |
105 |
意 大利 |
dd-mm-yy |
6 |
106(1) |
- |
dd mon yy |
7 |
107(1) |
- |
mon dd, yy |
8 |
108 |
- |
hh:mi:ss |
- |
9 或 109 (1,2) |
默 認設置 + 毫秒 |
mon dd yyyy hh:mi:ss:mmmAM(或 PM) |
10 |
110 |
美 國 |
mm-dd-yy |
11 |
111 |
日 本 |
yy/mm/dd |
12 |
112 |
ISO |
yymmdd yyyymmdd |
- |
13 或 113 (1,2) |
歐 洲默認設置 + 毫秒 |
dd mon yyyy hh:mi:ss:mmm(24h) |
14 |
114 |
- |
hh:mi:ss:mmm(24h) |
- |
20 或 120 (2) |
ODBC 規範 |
yyyy-mm-dd hh:mi:ss(24h) |
- |
21 或 121 (2) |
ODBC 規範(帶毫秒) |
yyyy-mm-dd hh:mi:ss.mmm(24h) |
- |
126 (4) |
ISO8601 |
yyyy- mm-ddThh:mi:ss.mmm(無空格) |
- |
127(6, 7) |
帶時區 Z 的 ISO8601。 |
yyyy-mm-ddThh:mi:ss.mmmZ (無 空格) |
- |
130 (1,2) |
回曆 (5) |
dd mon yyyy hh:mi:ss:mmmAM |
- |
131 (2) |
回曆 (5) |
dd/mm/yy hh:mi:ss:mmmAM |
--
語句及查詢結果:
SELECT
CONVERT
(
varchar
(
100
),
GETDATE
(),
0
):
05
16
2006
10
:57AM
SELECT
CONVERT
(
varchar
(
100
),
GETDATE
(),
1
):
05
/
16
/
06
SELECT
CONVERT
(
varchar
(
100
),
GETDATE
(),
2
):
06.05
.
16
SELECT
CONVERT
(
varchar
(
100
),
GETDATE
(),
3
):
16
/
05
/
06
SELECT
CONVERT
(
varchar
(
100
),
GETDATE
(),
4
):
16.05
.
06
SELECT
CONVERT
(
varchar
(
100
),
GETDATE
(),
5
):
16
-
05
-
06
SELECT
CONVERT
(
varchar
(
100
),
GETDATE
(),
6
):
16
05
06
SELECT
CONVERT
(
varchar
(
100
),
GETDATE
(),
7
):
05
16
,
06
SELECT
CONVERT
(
varchar
(
100
),
GETDATE
(),
8
):
10
:
57
:
46
SELECT
CONVERT
(
varchar
(
100
),
GETDATE
(),
9
):
05
16
2006
10
:
57
:
46
:827AM
SELECT
CONVERT
(
varchar
(
100
),
GETDATE
(),
10
):
05
-
16
-
06
SELECT
CONVERT
(
varchar
(
100
),
GETDATE
(),
11
):
06
/
05
/
16
SELECT
CONVERT
(
varchar
(
100
),
GETDATE
(),
12
):
060516
SELECT
CONVERT
(
varchar
(
100
),
GETDATE
(),
13
):
16
05
2006
10
:
57
:
46
:
937
SELECT
CONVERT
(
varchar
(
100
),
GETDATE
(),
14
):
10
:
57
:
46
:
967
SELECT
CONVERT
(
varchar
(
100
),
GETDATE
(),
20
):
2006
-
05
-
16
10
:
57
:
47
SELECT
CONVERT
(
varchar
(
100
),
GETDATE
(),
21
):
2006
-
05
-
16
10
:
57
:
47.157
SELECT
CONVERT
(
varchar
(
100
),
GETDATE
(),
22
):
05
/
16
/
06
10
:
57
:
47
AM
SELECT
CONVERT
(
varchar
(
100
),
GETDATE
(),
23
):
2006
-
05
-
16
SELECT
CONVERT
(
varchar
(
100
),
GETDATE
(),
24
):
10
:
57
:
47
SELECT
CONVERT
(
varchar
(
100
),
GETDATE
(),
25
):
2006
-
05
-
16
10
:
57
:
47.250
SELECT
CONVERT
(
varchar
(
100
),
GETDATE
(),
100
):
05
16
2006
10
:57AM
SELECT
CONVERT
(
varchar
(
100
),
GETDATE
(),
101
):
05
/
16
/
2006
SELECT
CONVERT
(
varchar
(
100
),
GETDATE
(),
102
):
2006.05
.
16
SELECT
CONVERT
(
varchar
(
100
),
GETDATE
(),
103
):
16
/
05
/
2006
SELECT
CONVERT
(
varchar
(
100
),
GETDATE
(),
104
):
16.05
.
2006
SELECT
CONVERT
(
varchar
(
100
),
GETDATE
(),
105
):
16
-
05
-
2006
SELECT
CONVERT
(
varchar
(
100
),
GETDATE
(),
106
):
16
05
2006
SELECT
CONVERT
(
varchar
(
100
),
GETDATE
(),
107
):
05
16
,
2006
SELECT
CONVERT
(
varchar
(
100
),
GETDATE
(),
108
):
10
:
57
:
49
SELECT
CONVERT
(
varchar
(
100
),
GETDATE
(),
109
):
05
16
2006
10
:
57
:
49
:437AM
SELECT
CONVERT
(
varchar
(
100
),
GETDATE
(),
110
):
05
-
16
-
2006
SELECT
CONVERT
(
varchar
(
100
),
GETDATE
(),
111
):
2006
/
05
/
16
SELECT
CONVERT
(
varchar
(
100
),
GETDATE
(),
112
):
20060516
SELECT
CONVERT
(
varchar
(
100
),
GETDATE
(),
113
):
16
05
2006
10
:
57
:
49
:
513
SELECT
CONVERT
(
varchar
(
100
),
GETDATE
(),
114
):
10
:
57
:
49
:
547
SELECT
CONVERT
(
varchar
(
100
),
GETDATE
(),
120
):
2006
-
05
-
16
10
:
57
:
49
SELECT
CONVERT
(
varchar
(
100
),
GETDATE
(),
121
):
2006
對上面進行動態生成字符串:
declare
@sql1
nvarchar
(
200
),
@sql2
nvarchar
(
200
)
declare
@count
nvarchar
(
100
);
set
@sql1
=
'
SELECT CONVERT(varchar(100), GETDATE(), 0)
'
set
@sql2
=
'
SELECT @count = CONVERT(varchar(100), GETDATE(), 0)
'
exec
sp_executesql
@sql2
,N
'
@count nvarchar(50) out
'
,
@count
out
print
@sql1
+
'
:
'
+
@count
--
SQL Server 僅保證往返轉換(即從原始數據類型進行轉換後又返回原始數據類型的轉換)在各版本間產生相同值。
DECLARE
@myval
decimal
(
5
,
2
)
SET
@myval
=
193.57
SELECT
CAST
(
CAST
(
@myval
AS
varbinary
(
20
))
AS
decimal
(
10
,
5
))
--
Or, using CONVERT
SELECT
CONVERT
(
decimal
(
10
,
5
),
CONVERT
(
varbinary
(
20
),
@myval
))
--
輸出193.57000
--
輸出193.57000
--
bigint數據類型的字段截取(其它類型也同樣)
select
substring
(
CONVERT
(
varchar
(
15
),字段名),
11
,
9
)
from
表名
select
substring
(
cast
(字段名
as
varchar
(
50
),
6
,
9
))
from
表名
SQL中的相除
--
SQL中的相除
SELECT
CASE
WHEN
ISNULL
(A
+
B,
0
)
<>
0
THEN
LTRIM
(
CONVERT
(
DEC
(
18
,
2
),A
*
100.0
/
(A
+
B)))
+
'
%
'
ELSE
''
END
AS
'
百分數
'
FROM
TB
--
百分比的不一樣格式
select
LTRIM
(
CONVERT
(
DEC
(
18
,
2
),
42
*
100.0
/
96
))
+
'
%
'
AS
'
百分數string
'
,
--
DEC=decimal
CONVERT
(
decimal
(
10
,
2
),
42
*
100.0
/
96
)
AS
'
百分數dec
'
,
--
100 與 100.0是不同的
CONVERT
(
decimal
(
10
,
2
),
42
*
100
/
96
)
AS
'
沒有保留到小數點
'
--
from Tb
order
by
百分數dec
desc
--
43.75% 43.75 43.00
--
方法二:
Select
(
Convert
(
varchar
(
50
),
Round
(
42
*
100.0
/
96
,
3
))
+
'
%
'
)
as
百分比
--
from A
--
43.750000%
四捨五入
/*
ROUND ( numeric_expression , length [ ,function ] )
function 必須爲 tinyint、smallint 或 int。
若是省略 function 或其值爲 0(默認值),則將舍入 numeric_expression。
若是指定了0之外的值,則將截斷 numeric_expression。
*/
SELECT
ROUND
(
150.45648
,
2
);
--
保留小數點後兩位,須要四捨五入
SELECT
ROUND
(
150.45648
,
2
,
0
);
--
保留小數點後兩位,0爲默認值,表示進行四捨五入
SELECT
ROUND
(
150.45648
,
2
,
1
);
--
保留小數點後兩位,不須要四捨五入,這裏除0之外都是有一樣的效果
SELECT
ROUND
(
150.45648
,
2
,
2
);
--
保留小數點後兩位,不須要四捨五入,這裏除0之外都是有一樣的效果
--
150.46000
--
150.45000
--
150.45000
--
150.45000
對字段出現NULL值的處理
--
判斷某些字段是否爲空
--
case
select
case
when
'
字段名
'
is
null
then
'
\N
'
else
convert
(
varchar
(
20
),
'
字段名
'
)
end
as
'
NewName
'
select
case
when
null
is
null
then
'
\N
'
else
convert
(
varchar
(
20
),
null
)
end
as
'
NewName
'
--
SQL Server 2005:coalesce
select
coalesce
(
'
字符串類型字段
'
,
'
\N
'
)
as
'
NewName
'
select
coalesce
(
convert
(
varchar
(
20
),
'
非字符串類型字段
'
),
'
\N
'
)
as
'
NewName
'
select
coalesce
(
convert
(
varchar
(
20
),
null
),
'
\N
'
)
as
'
NewName
'
--
coalesce,返回其參數中的第一個非空表達式
select
Coalesce
(
null
,
null
,
1
,
2
,
null
)
union
select
Coalesce
(
null
,
11
,
12
,
13
,
null
)
union
select
Coalesce
(
111
,
112
,
113
,
114
,
null
)
count的幾種狀況
--
第一種
select
count
(
*
)
from
tablename
--
第二種
select
count
(ID)
from
tablename
--
第三種,1換成其它值也是能夠的
select
count
(
1
)
from
tablename
/*
--第四種,這個不存在性能問題
idint 表ID(若是 indid = 0 或255)。不然爲索引所屬表的ID
Indid smallint 索引ID:
0=表
1=聚簇索引
>1=非聚簇索引
255=具備text或image數據的表條目。
rows int 基於indid=0 和 indid=1地數據級行數,該值對於indid>1重複。若是indid=255,rows設置爲0。
當表沒有聚簇索引時,Indid = 0 不然爲 1。
*/
select
rows,indid
from
sysindexes
where
id
=
object_id
(
'
tablename
'
)
and
indid
in
(
0
,
1
)
Union all
--
把兩個相同結構的表union後插入到一個新表中
select
*
into
table_now
from
table_1
union
all
select
*
from
table_2
--
Truncate table table_now
查看數據庫緩存的SQL
--
適用MSSQL2000、MSSQL2005
use
master
declare
@dbid
int
Select
@dbid
=
dbid
from
sysdatabases
where
name
=
'
Test
'
--
修改爲數據庫的名稱
select
dbid,UseCounts ,RefCounts,CacheObjtype,ObjType,
DB_Name
(dbid)
as
DatabaseName,SQL
from
syscacheobjects
where
dbid
=
@dbid
order
by
dbid,useCounts
desc
,objtype
刪除計劃緩存
--
刪除整個數據庫的計劃緩存
DBCC
FREEPROCCACHE
--
刪除某個數據庫的計劃緩存
USE
master
DECLARE
@dbid
INT
SELECT
@dbid
=
dbid
FROM
sysdatabases
WHERE
NAME
=
'
表名
'
DBCC
FLUSHPROCINDB (
@dbid
)
導出時加入特殊字符
狀況一:所有字段都須要加字符,在這裏設置【文本限定符】就能夠了。
狀況二:
--
某些特殊的字段須要加單引號(或者其它符號)
SELECT
[
ID
]
,
''''
+
convert
(
varchar
(
25
),
[
ts
]
,
121
)
+
''''
as
[
ts
]
,
''''
+
convert
(
varchar
(
25
),
[
otherParty
]
,
121
)
+
''''
as
[
otherParty
]
,
''''
+
convert
(
varchar
(
25
),
[
StartTime
]
,
121
)
+
''''
as
[
StartTime
]
,
[
CcCause
]
,
[
RrCause
]
FROM
[
表
]
效果:74983006,'2010-03-09 23:59:10.000'
newid()的妙用
--
生成測試數據
SET
ANSI_NULLS
ON
GO
SET
QUOTED_IDENTIFIER
ON
GO
SET
ANSI_PADDING
ON
GO
CREATE
TABLE
[
dbo
]
.
[
test
]
(
[
name
]
[
varchar
]
(
20
)
NULL
,
[
quarter
]
[
int
]
NULL
,
[
profile
]
[
int
]
NULL
,
[
dates
]
[
smallint
]
NULL
,
[
ID
]
[
int
]
IDENTITY
(
1
,
1
)
NOT
NULL
,
CONSTRAINT
[
PK_test
]
PRIMARY
KEY
CLUSTERED
(
[
ID
]
ASC
)
WITH
(PAD_INDEX
=
OFF
, STATISTICS_NORECOMPUTE
=
OFF
, IGNORE_DUP_KEY
=
OFF
, ALLOW_ROW_LOCKS
=
ON
, ALLOW_PAGE_LOCKS
=
ON
)
ON
[
PRIMARY
]
)
ON
[
PRIMARY
]
GO
SET
ANSI_PADDING
OFF
GO
SET
IDENTITY_INSERT
[
dbo
]
.
[
test
]
ON
INSERT
[
dbo
]
.
[
test
]
(
[
name
]
,
[
quarter
]
,
[
profile
]
,
[
dates
]
,
[
ID
]
)
VALUES
(N
'
a
'
,
1
,
1000
,
421
,
1
)
INSERT
[
dbo
]
.
[
test
]
(
[
name
]
,
[
quarter
]
,
[
profile
]
,
[
dates
]
,
[
ID
]
)
VALUES
(N
'
Aa
'
,
2
,
2000
,
421
,
2
)
INSERT
[
dbo
]
.
[
test
]
(
[
name
]
,
[
quarter
]
,
[
profile
]
,
[
dates
]
,
[
ID
]
)
VALUES
(N
'
AA
'
,
3
,
4000
,
421
,
3
)
INSERT
[
dbo
]
.
[
test
]
(
[
name
]
,
[
quarter
]
,
[
profile
]
,
[
dates
]
,
[
ID
]
)
VALUES
(N
'
a
'
,
4
,
5000
,
421
,
4
)
INSERT
[
dbo
]
.
[
test
]
(
[
name
]
,
[
quarter
]
,
[
profile
]
,
[
dates
]
,
[
ID
]
)
VALUES
(N
'
b
'
,
1
,
3000
,
421
,
5
)
INSERT
[
dbo
]
.
[
test
]
(
[
name
]
,
[
quarter
]
,
[
profile
]
,
[
dates
]
,
[
ID
]
)
VALUES
(N
'
b
'
,
2
,
3500
,
421
,
6
)
INSERT
[
dbo
]
.
[
test
]
(
[
name
]
,
[
quarter
]
,
[
profile
]
,
[
dates
]
,
[
ID
]
)
VALUES
(N
'
b
'
,
3
,
4200
,
421
,
7
)
INSERT
[
dbo
]
.
[
test
]
(
[
name
]
,
[
quarter
]
,
[
profile
]
,
[
dates
]
,
[
ID
]
)
VALUES
(N
'
b
'
,
4
,
5500
,
421
,
8
)
INSERT
[
dbo
]
.
[
test
]
(
[
name
]
,
[
quarter
]
,
[
profile
]
,
[
dates
]
,
[
ID
]
)
VALUES
(N
'
a
'
,
1
,
1000
,
421
,
9
)
SET
IDENTITY_INSERT
[
dbo
]
.
[
test
]
OFF
--從數據庫中隨機取出N條記錄的方法:newid()
select
top
5
*
from
test
select
top
5
*
from
test
order
by
newid
()
(圖:效果圖)
查詢時區分大小寫
--
生成測試數據
SET
ANSI_NULLS
ON
GO
SET
QUOTED_IDENTIFIER
ON
GO
SET
ANSI_PADDING
ON
GO
CREATE
TABLE
[
dbo
]
.
[
test
]
(
[
name
]
[
varchar
]
(
20
)
NULL
,
[
quarter
]
[
int
]
NULL
,
[
profile
]
[
int
]
NULL
,
[
dates
]
[
smallint
]
NULL
,
[
ID
]
[
int
]
IDENTITY
(
1
,
1
)
NOT
NULL
,
CONSTRAINT
[
PK_test
]
PRIMARY
KEY
CLUSTERED
(
[
ID
]
ASC
)
WITH
(PAD_INDEX
=
OFF
, STATISTICS_NORECOMPUTE
=
OFF
, IGNORE_DUP_KEY
=
OFF
, ALLOW_ROW_LOCKS
=
ON
, ALLOW_PAGE_LOCKS
=
ON
)
ON
[
PRIMARY
]
)
ON
[
PRIMARY
]
GO
SET
ANSI_PADDING
OFF
GO
SET
IDENTITY_INSERT
[
dbo
]
.
[
test
]
ON
INSERT
[
dbo
]
.
[
test
]
(
[
name
]
,
[
quarter
]
,
[
profile
]
,
[
dates
]
,
[
ID
]
)
VALUES
(N
'
a
'
,
1
,
1000
,
421
,
1
)
INSERT
[
dbo
]
.
[
test
]
(
[
name
]
,
[
quarter
]
,
[
profile
]
,
[
dates
]
,
[
ID
]
)
VALUES
(N
'
Aa
'
,
2
,
2000
,
421
,
2
)
INSERT
[
dbo
]
.
[
test
]
(
[
name
]
,
[
quarter
]
,
[
profile
]
,
[
dates
]
,
[
ID
]
)
VALUES
(N
'
AA
'
,
3
,
4000
,
421
,
3
)
INSERT
[
dbo
]
.
[
test
]
(
[
name
]
,
[
quarter
]
,
[
profile
]
,
[
dates
]
,
[
ID
]
)
VALUES
(N
'
A
'
,
4
,
5000
,
421
,
4
)
INSERT
[
dbo
]
.
[
test
]
(
[
name
]
,
[
quarter
]
,
[
profile
]
,
[
dates
]
,
[
ID
]
)
VALUES
(N
'
b
'
,
1
,
3000
,
421
,
5
)
INSERT
[
dbo
]
.
[
test
]
(
[
name
]
,
[
quarter
]
,
[
profile
]
,
[
dates
]
,
[
ID
]
)
VALUES
(N
'
b
'
,
2
,
3500
,
421
,
6
)
INSERT
[
dbo
]
.
[
test
]
(
[
name
]
,
[
quarter
]
,
[
profile
]
,
[
dates
]
,
[
ID
]
)
VALUES
(N
'
b
'
,
3
,
4200
,
421
,
7
)
INSERT
[
dbo
]
.
[
test
]
(
[
name
]
,
[
quarter
]
,
[
profile
]
,
[
dates
]
,
[
ID
]
)
VALUES
(N
'
b
'
,
4
,
5500
,
421
,
8
)
INSERT
[
dbo
]
.
[
test
]
(
[
name
]
,
[
quarter
]
,
[
profile
]
,
[
dates
]
,
[
ID
]
)
VALUES
(N
'
A
'
,
1
,
1000
,
421
,
9
)
SET
IDENTITY_INSERT
[
dbo
]
.
[
test
]
OFF
--
sql server 查詢區分大小寫 collate Chinese_PRC_CS_AS_WS
select
*
from
test
where
[
name
]
=
'
A
'
collate Chinese_PRC_CS_AI
select
*
from
test
where
ascii
(name)
=
ascii
(
'
A
'
)
(圖:效果圖)
SQL換行
/*
SQL的換行
製表符 CHAR(9)
換行符 CHAR(10)
回車 CHAR(13)
*/
PRINT
'
Test
'
+
CHAR
(
13
)
+
'
Name
'
PRINT
'
Test
'
+
CHAR
(
10
)
+
'
Age
'
PRINT
'
Test
'
+
CHAR
(
9
)
+
'
Tel
'
--
以文本格式顯示結果
SELECT
'
AAA
'
+
CHAR
(
10
)
+
'
BBB
'
--
AAA BBB
SELECT
'
AAA
'
+
CHAR
(
13
)
+
'
BBB
'
--
AAA BBB
SELECT
'
AAA
'
+
CHAR
(
10
)
+
CHAR
(
13
)
+
'
BBB
'
--
AAA BBB
TRUNCATE TABLE [Table Name]
下面是對Truncate語句在MSSQLServer2000中用法和原理的說明:
Truncate是SQL中的一個刪除數據表內容的語句,用法是:
Truncate table 表名 速度快,並且效率高,由於:
TRUNCATE TABLE 在功能上與不帶 WHERE 子句的 DELETE 語句相同:兩者均刪除表中的所有行。但 TRUNCATE TABLE 比 DELETE 速度快,且使用的系統和事務日誌資源少。
DELETE 語句每次刪除一行,並在事務日誌中爲所刪除的每行記錄一項。TRUNCATE TABLE 經過釋放存儲表數據所用的數據頁來刪除數據,而且只在事務日誌中記錄頁的釋放。
TRUNCATE TABLE 刪除表中的全部行,但表結構及其列、約束、索引等保持不變。新行標識所用的計數值重置爲該列的種子。若是想保留標識計數值,請改用 DELETE。若是要刪除表定義及其數據,請使用 DROP TABLE 語句。
對於由 FOREIGN KEY 約束引用的表,不能使用 TRUNCATE TABLE,而應使用不帶 WHERE 子句的 DELETE 語句。因爲 TRUNCATE TABLE 不記錄在日誌中,因此它不能激活觸發器。
TRUNCATE TABLE 不能用於參與了索引視圖的表。
其它
--
查看內存狀態
dbcc
memorystatus
--
查看哪一個引發的阻塞,blk
EXEC
sp_who active
--
查看鎖住了那個資源id,objid
EXEC
sp_lock
--
當前查詢分析器SPID
--
查詢分析器的狀態欄中能夠看到
--
好比sa(57),這就表示當前查詢分析器SPID爲57,這樣在使用profile的時候就能夠指定當前窗體進行監控
--
獲取腳本的執行時間
declare
@timediff
datetime
select
@timediff
=
getdate
()
select
*
from
tablename
print
'
1耗時:
'
+
convert
(
varchar
(
10
),
datediff
(ms,
@timediff
,
getdate
()))