1. TVP, 表變量,臨時表,CTE 的區(qū)別
TVP和臨時表都是可以索引的,總是存在tempdb中,會增加系統(tǒng)數(shù)據(jù)庫開銷,而表變量和CTE只有在內(nèi)存溢出時才會被寫入tempdb中。對于數(shù)據(jù)量大,并且反復(fù)使用,反復(fù)進(jìn)行查詢關(guān)聯(lián)的,建議使用臨時表或TVP,數(shù)據(jù)量小,使用表變量或CTE比較合適
2. sql_variant 萬能類型
可以存放所有數(shù)據(jù)類型,相當(dāng)于C#中的object數(shù)據(jù)類型
3. datetime, datetime2, datetimeoffset
datetime 時間有效期較小,在1753-1-1 之前就不能使用了,精度為毫秒級別,而datetime2 數(shù)據(jù)范圍相當(dāng)于C#中的datetime ,精度達(dá)到了秒后面小數(shù)點(diǎn)后7位,datetimeoffset則是考慮是時區(qū)的日期類型
4. MERGE的用法
語法很簡單就不說了,主要是處理兩張表某些字段對比后的操作,需注意 when not matched (by target) 與 when not matched by source的區(qū)別,前者是是針對對比后目標(biāo)表不存在的記錄,可以選擇insert操作,而后者則是針對對比后目標(biāo)表多出來的記錄,可以選擇delete或update操作
5. rowversion 類型
代替以前的timestamp,時間戳,8字節(jié)二進(jìn)制值,常用來進(jìn)行解決并發(fā)操作的問題
6. Sysdatetime()
返回datetime2類型,精度比datetime高
7. with cube , with rollup , grouping sets 運(yùn)算符
都可與group by 后連用,with cube 表示匯總所有級別的組合,with rollup 則是按級別匯總,從下面的代碼可以詳細(xì)看出區(qū)別。注意,匯總行,null可以看成所有值
而grouping sets運(yùn)算符,則僅返回每個分組頂級匯總行,在查詢匯總行中 可使用grouping(字段名) = 1來判斷,該運(yùn)算符可和rollup, cube連用,表示按照grouping by sets和按照rollup/cube處理的結(jié)果集union all
示例代碼如下:
復(fù)制代碼 代碼如下:
With cube, With rollup
--示例代碼
declare @t table(goodsname VARCHAR(max) ,sku1name VARCHAR(max) , sku2name VARCHAR(max), qty INT)
insert @t select '凡客TX','紅色','S',1
insert @t select '凡客TX','黑色','S',2
insert @t select '凡客TX','白色','L',3
insert @t select '京東村山','白色','L',4
insert @t select '京東村山','紅色','S',5
insert @t select '京東村山','黑色','L',6
insert @t select '亞馬遜拖鞋','白色','L',7
insert @t select '亞馬遜拖鞋','紅色','S',8
SELECT * FROM @t
select goodsname,sku1name,sku2name,sum(qty) sumqty
from @t
group by goodsname,sku1name,sku2name with rollup
ORDER BY goodsname,sku1name,sku2name
select goodsname,sku1name,sku2name,sum(qty) sumqty
from @t
group by goodsname,sku1name,sku2name with cube
ORDER BY goodsname,sku1name,sku2name
-----------------------
declare @t table(goodsname VARCHAR(max) ,sku1name VARCHAR(max) , sku2name VARCHAR(max), qty INT)
insert @t select '凡客TX','紅色','S',1
insert @t select '凡客TX','黑色','S',2
insert @t select '凡客TX','白色','L',3
insert @t select '京東村山','白色','L',4
insert @t select '京東村山','紅色','S',5
insert @t select '京東村山','黑色','L',6
insert @t select '亞馬遜拖鞋','白色','L',7
insert @t select '亞馬遜拖鞋','紅色','S',8
--GROUPING SETS 運(yùn)算符
SELECT goodsname,sku1name,sku2name, SUM(qty) FROM @t GROUP BY GROUPING SETS(goodsname,sku1name,sku2name)
SELECT goodsname, sku1name, sku2name ,SUM(qty) FROM @t
GROUP BY GROUPING SETS(goodsname), ROLLUP(sku1name,sku2name)
ORDER BY goodsname,sku1name,sku2name
SELECT goodsname, sku1name, sku2name ,SUM(qty) FROM @t
GROUP BY ROLLUP(goodsname,sku1name,sku2name)
ORDER BY goodsname,sku1name,sku2name
SELECT CASE WHEN GROUPING(goodsname) = 1 THEN '[ALL]' ELSE goodsname END goodsname,
CASE WHEN GROUPING(sku1name) = 1 THEN '[ALL]' ELSE sku1name END sku1name,
CASE WHEN GROUPING(sku2name) = 1 THEN '[ALL]' ELSE sku2name END sku2name ,SUM(qty) FROM @t
GROUP BY GROUPING SETS(goodsname), ROLLUP(sku1name,sku2name)
ORDER BY goodsname,sku1name,sku2name
8. 一些快捷的語法 例如 Declare @id int = 0
雖然有時很快捷,但DBA不建議這樣使用,Declare @id = select top 1 id from 表名,建議聲明和查表賦值分開
9. 公用表達(dá)式 CTE
特點(diǎn):可嵌套使用,代替聯(lián)接表中的子查詢,結(jié)構(gòu)層次更加清晰,也可用來遞歸查詢,另外通過巧妙的常量列控制遞歸層次
示例代碼如下:
復(fù)制代碼 代碼如下:
使用CTE
--公用表達(dá)式CTE Common table expression
--用CTE實(shí)現(xiàn)遞歸算法
CREATE TABLE EMPLOYEETREE(
EMPLOYEE INT PRIMARY KEY,
employeename nvarchar(50),
reportsto int
)
insert into EMPLOYEETREE values(1,'Richard',null)
insert into EMPLOYEETREE values(2,'Stephen',1)
insert into EMPLOYEETREE values(3,'Clemens',2)
insert into EMPLOYEETREE values(4,'Malek',2)
insert into EMPLOYEETREE values(5,'Goksin',4)
insert into EMPLOYEETREE values(6,'Kimberly',1)
insert into EMPLOYEETREE values(7,'Ramesh',5)
----------------------
--確定哪些員工向Stephen報告的遞歸查詢
with employeeTemp as
(
select EMPLOYEE, employeename, reportsto from EMPLOYEETREE where EMPLOYEE = 2
union all
select a.EMPLOYEE, a.employeename, a.reportsto from EMPLOYEETREE as a
inner join employeeTemp as b on a.reportsto = b.EMPLOYEE
)
select * from employeeTemp where EMPLOYEE > 2 --option(maxrecursion 2)
--不報錯設(shè)置級聯(lián)關(guān)聯(lián)遞歸
with employeeTemp as
(
select EMPLOYEE, employeename, reportsto,0 as sublevel from EMPLOYEETREE where EMPLOYEE = 2
union all
select a.EMPLOYEE, a.employeename, a.reportsto,sublevel+1 from EMPLOYEETREE as a
inner join employeeTemp as b on a.reportsto = b.EMPLOYEE
)
select * from employeeTemp where EMPLOYEE > 2 and sublevel =2 --option(maxrecursion 2)
10. pivot 與 unpivot
前者用在行轉(zhuǎn)列,注意:必須用聚合函數(shù)與PIVOT一起使用,計(jì)算聚會時將不考慮出現(xiàn)在值列中的任何空值;一般情況下,可以用列上的子查詢來替換pivot語句,但是這樣做效率不高
后者用在列轉(zhuǎn)行,注意:如果某些列中有null值,將會被過濾掉,不產(chǎn)生新行;語法上For前指定的新列,對應(yīng)原表指定列名中的值,F(xiàn)or后指定的新列對應(yīng)原表指定列名中的標(biāo)題的值
兩者都有的共性:語法上最后必須要有別名;IN里面指定的列類型必須是一致的。
示例代碼如下:
復(fù)制代碼 代碼如下:
pivot與unpivot
--關(guān)于PIVOT的操作
CREATE TABLE #test
(
NAME VARCHAR(max),
SCORE INT
)
INSERT INTO #test VALUES ('張三','97')
INSERT INTO #test VALUES ('李四','28')
INSERT INTO #test VALUES ('王五','33')
INSERT INTO #test VALUES ('神人','78')
--NAME SCORE
--張三 97
--李四 28
--王五 33
--神人 78
--行轉(zhuǎn)列
SELECT --'成績單' AS SCORENAME ,
[張三], [李四], [王五]
FROM #test
PIVOT (AVG(SCORE) FOR NAME IN ([張三], [李四], [王五])) b
-----------------------------------------
CREATE TABLE VendorEmployee(
VendorId INT,
Emp1Order INT,
Emp2Order INT,
Emp3Order INT,
Emp4Order INT,
Emp5Order INT,
)
GO
INSERT INTO VendorEmployee VALUES(1,4,3,5,4,4)
INSERT INTO VendorEmployee VALUES(2,4,1,5,5,5)
INSERT INTO VendorEmployee VALUES(3,4,3,5,4,4)
INSERT INTO VendorEmployee VALUES(4,4,2,5,4,4)
INSERT INTO VendorEmployee VALUES(5,5,1,5,5,5)
SELECT * FROM VendorEmployee
----------------
--列轉(zhuǎn)行
SELECT * FROM (
SELECT VendorId,[Emp1Order],[Emp2Order],[Emp3Order],[Emp4Order],[Emp5Order] FROM VendorEmployee) AS unpiv
UNPIVOT (orders FOR elyid IN ([Emp1Order],[Emp2Order],[Emp3Order],[Emp4Order],[Emp5Order])) AS child
ORDER BY elyid
SELECT * FROM VendorEmployee
UNPIVOT (orders FOR elyid IN ([Emp1Order],[Emp2Order],[Emp3Order],[Emp4Order],[Emp5Order])) AS child
ORDER BY elyid
SELECT * FROM VendorEmployee UNPIVOT ( ORDERS FOR [操作員名字] IN ([Emp1Order],[Emp2Order],[Emp3Order],[Emp4Order],[Emp5Order]))