最近研究了一下SQLSERVER數據庫技術,現拿出來跟大夥分享,首先給出T-SQL腳本。html
創建一張測試表:sql
CREATE
TABLE
TestTable (
Column1
varchar
(
1
),
Column2
int
)
加入一些測試數據:數據庫
INSERT
INTO
TestTable
VALUES
(
'
A
'
,
1
);
INSERT
INTO
TestTable
VALUES
(
'
A
'
,
1
);
--
重複
INSERT
INTO
TestTable
VALUES
(
'
A
'
,
2
);
INSERT
INTO
TestTable
VALUES
(
'
B
'
,
1
);
INSERT
INTO
TestTable
VALUES
(
'
B
'
,
2
);
INSERT
INTO
TestTable
VALUES
(
'
B
'
,
2
);
--
重複
INSERT
INTO
TestTable
VALUES
(
'
C
'
,
2
)
使用sqlserver2005新特性刪除重複記錄:
DELETE
FROM
TestTable
WHERE
TestTable.
%%
lockres
%%
NOT
IN
(
SELECT
MIN
(b.
%%
lockres
%%
)
FROM
TestTable b
GROUP
BY
b.column1, b.Column2)
sqlserver2008新特性刪除重複記錄:ide
DELETE
FROM
TestTable
WHERE
TestTable.
%%
physloc
%%
NOT
IN
(
SELECT
MIN
(b.
%%
physloc
%%
)
FROM
TestTable b
GROUP
BY
b.column1, b.Column2);
固然了若是TestTable 具有主鍵(假設主鍵爲ID且identity(1,1)),咱們能夠經過傳統的T-SQL語句獲取不重複的記錄:函數
--
創建主鍵
alter
table
TestTable
with
nocheck
add
id
int
identity
(
1
,
1
)
primary
key
go
--
取惟一記錄
select
min
(ID)
from
TestTable
group
by
column1, column2
go
SQLSERVER2005隨機取數據:sqlserver
select
top
3
*
from
TestTable
order
by
newid
()
上述語句每次從TestTable內取出的3條隨機記錄。測試
SQLSERVER2005 row_number 函數:spa
該函數主要用於sql分頁方面,給出簡單範例代碼:code
select
*
from
(
select
id, column1, column2, row_number()
OVER
(
order
by
id
desc
)
as
row
from
TestTable
) a
where
row
between
1
and
3
這裏id爲 TestTable 的自增主鍵,每次按主鍵id降序後取前三條數據行。server
查看SQLSERVER2005版本:
SELECT
SERVERPROPERTY(
'
productversion
'
), SERVERPROPERTY (
'
productlevel
'
), SERVERPROPERTY (
'
edition
'
)
這裏給出不一樣版本的SQLSERVER2005版本:
RTM版:
sp3版:
SQLSERVER2005全部版本:
發行版 |
Sqlservr.exe |
RTM |
2005.90.1399 |
SQL Server 2005 Service Pack 1 |
2005.90.2047 |
SQL Server 2005 Service Pack 2 |
2005.90.3042 |
SQL Server 2005 Service Pack 3 |
2005.90.4035 |
最後,但願本文能夠給您帶來幫助。
原文連接:
http://www.cnblogs.com/ryanding/archive/2011/02/22/1961278.html