日期:2014-05-18 浏览次数:20547 次
DECLARE @i int = 4
DECLARE @i INT=2 SELECT TOP 3 @i*=@i FROM sys.objects SELECT @i
SELECT Country,TitleOfCourtesy,COUNT(EmployeeID) 汇总结果 FROM Employees GROUP BY Grouping Sets(Country,TitleOfCourtesy,(Country,TitleOfCourtesy)) Order By Country DESC,TitleOfCourtesy
CREATE TABLE tbSource(C1 INT PRIMARY KEY,C2 NVARCHAR(10)) GO INSERT tbSource VALUES(1,N'甲'),(2,N'乙') GO SELECT * INTO tbDest FROM tbSource DELETE tbSource WHERE c1=1 UPDATE tbSource SET c2=N'乙2' WHERE C1=2 INSERT tbSource VALUES(3,N'丙') MERGE tbDest D USING tbSource s ON D.c1 = S.c1 WHEN MATCHED THEN --修改 UPDATE SET D.c2 = S.c2 WHEN SOURCE NOT MATCHED THEN --删除 DELETE WHEN TARGET NOT MATCHED THEN --新增 INSERT VALUES(c1, c2) OUTPUT $action, INSERTED.c1 [New c1], INSERTED.c2 [New c2], DELETED.c1 [Original c1], DELETED.c2 [Original c2];
-- Step 1: 建立有阶层特征的数据表 -- HierarchyID 可比较,因此可当作主键 CREATE TABLE tbEmployee ( OrgNode HierarchyID PRIMARY KEY CLUSTERED, OrgLevel AS OrgNode.GetLevel(), EmployeeID int UNIQUE NOT NULL, EmpName nvarchar(20) NOT NULL) ; GO -- Step 2: 建立 breadth-first 索引,也就是相同父亲的数据放在一起 -- 以数值 OrgLevel 放在前面,然后才是结点 CREATE UNIQUE INDEX EmployeeOrgNc1 ON tbEmployee(OrgLevel, OrgNode) ; GO -- Step 3: 加载数据 -- 载入根结点 INSERT tbEmployee(OrgNode, EmployeeID, EmpName) VALUES (hierarchyid::GetRoot(), 1, N'甲') ; GO SELECT OrgNode.ToString() [文字描述阶层], OrgNode, OrgLevel, EmployeeID, EmpName FROM tbEmployee ;
--透过 GetDescendant 函数建立第一个子结点 DECLARE @Manager hierarchyid SET @Manager = (SELECT OrgNode FROM tbEmployee WHERE EmployeeID = 1) --加入子结点