SQL Server 總結溫習(一)。本站提示廣大學習愛好者:(SQL Server 總結溫習(一))文章只能為提供參考,不一定能成為您想要的結果。以下是SQL Server 總結溫習(一)正文
1. TVP, 表變量,暫時表,CTE 的差別
TVP和暫時表都是可以索引的,老是存在tempdb中,會增長體系數據庫開支,而表變量和CTE只要在內存溢出時才會被寫入tempdb中。關於數據量年夜,而且重復應用,重復停止查詢聯系關系的,建議應用暫時表或TVP,數據量小,應用表變量或CTE比擬適合
2. sql_variant 全能類型
可以寄存一切數據類型,相當於C#中的object數據類型
3. datetime, datetime2, datetimeoffset
datetime 時光有用期較小,在1753-1-1 之前就不克不及應用了,精度為毫秒級別,而datetime2 數據規模相當於C#中的datetime ,精度到達了秒前面小數點後7位,datetimeoffset則是斟酌是時區的日期類型
4. MERGE的用法
語法很簡略就不說了,重要是處置兩張表某些字段比較後的操作,需留意 when not matched (by target) 與 when not matched by source的差別,前者是是針對比較後目的表不存在的記載,可以選擇insert操作,爾後者則是針對比較後目的表多出來的記載,可以選擇delete或update操作
5. rowversion 類型
取代之前的timestamp,時光戳,8字節二進制值,經常使用來停止處理並發操作的成績
6. Sysdatetime()
前往datetime2類型,精度比datetime高
7. with cube , with rollup , grouping sets 運算符
都可與group by 後連用,with cube 表現匯總一切級其余組合,with rollup 則是按級別匯總,從上面的代碼可以具體看出差別。留意,匯總行,null可以算作一切值
而grouping sets運算符,則僅前往每一個分組頂級匯總行,在查詢匯總行中 可以使用grouping(字段名) = 1來斷定,該運算符可和rollup, cube連用,表現依照grouping by sets和依照rollup/cube處置的成果集union all
示例代碼以下:
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 運算符
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. 公用表達式 CTE
特色:可嵌套應用,取代聯接表中的子查詢,構造條理加倍清楚,也可用來遞歸查詢,別的經由過程奇妙的常量列掌握遞歸條理
示例代碼以下:
--公用表達式CTE Common table expression
--用CTE完成遞歸算法
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)
--不報錯設置級聯聯系關系遞歸
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
前者用外行轉列,留意:必需用聚合函數與PIVOT一路應用,盤算聚首時將不斟酌湧現在值列中的任何空值;普通情形下,可以用列上的子查詢來調換pivot語句,然則如許做效力不高
後者用在列轉行,留意:假如某些列中有null值,將會被過濾失落,不發生新行;語法上For前指定的新列,對應原表指定列名中的值,For後指定的新列對應原表指定列名中的題目的值
二者都有的個性:語法上最初必需要有別號;IN外面指定的列類型必需是分歧的。
示例代碼以下:
pivot與unpivot
--關於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
--行轉列
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
----------------
--列轉行
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]))