除了基本的数据库,数据表,数据记录操作之外,SQL-Server还为我们提供了比较丰富的其他对象元素。函数,过程,触发器,序列,映射服务器,
以及对各种元素的系统表信息读取与判断。
--先加一个利用递归查询树形节点的例子,待整理
CREATE TABLE smMenuTest
(
Menuid INT,
MenuCaption VARCHAR(255),
MenuKind INT,
SuperId INT
)
SELECT * FROM smMenuTest ORDER BY MenuKind,SuperID,Menuid
INSERT smMenuTest VALUES (1,'销售管理',1,0)
INSERT smMenuTest VALUES (2,'计划管理',1,0)
INSERT smMenuTest VALUES (3,'报价管理',2,1)
INSERT smMenuTest VALUES (4,'订单管理',2,1)
INSERT smMenuTest VALUES (5,'主计划',2,2)
INSERT smMenuTest VALUES (6,'制定报价单',3,3)
INSERT smMenuTest VALUES (7,'报价单查询',3,3)
INSERT smMenuTest VALUES (8,'订单录入',3,4)
INSERT smMenuTest VALUES (9,'订单审核',3,4)
INSERT smMenuTest VALUES (10,'MRP计划',3,5)
GO
;
--DELETE smMenuTest
WITH Menu as
(SELECT A.Menuid,A.MenuCaption,A.SuperId,iLevel=0 FROM smMenuTest A(NOLOCK)
WHERE A.MenuCaption='订单审核'
UNION ALL
SELECT A.Menuid,A.MenuCaption,A.SuperId,iLevel=B.iLevel+1 FROM smMenuTest A(NOLOCK) JOIN Menu B on B.SuperId=A.Menuid )
SELECT A.MenuCaption+'-' FROM Menu A FOR XML PATH ('')
--用让变量参与动态语句运算, 并取值(待整理)
- CREATE TABLE #tmp(t INT NOT NULL,it DECIMAL(18,4),pt DECIMAL(18,4));
- INSERT INTO #tmp(t,it,pt) VALUES(1,10.00,15.00);
- INSERT INTO #tmp(t,it,pt) VALUES(2,20.00,30.00);
- DECLARE @sql NVARCHAR(2000) = N'SELECT @it=it,@pt=pt FROM #tmp WHERE t=@tN;';
- DECLARE @Param NVARCHAR(500) = N'@tN INT,@it DECIMAL(18,4) OUTPUT,@pt DECIMAL(18,4) OUTPUT';
- DECLARE @tN INT,@it DECIMAL(18,4),@pt DECIMAL(18,4);
- SET @tN = 1;
- --@tN=@tN,第一个@tN是@Param里的声明,而第二个@tN则是外部的声明,其他类似
- EXEC sp_executesql @sql,@Param,@tN=@tN,@it=@it OUTPUT,@pt=@pt OUTPUT;
- SELECT @it,@pt;
- DROP TABLE #tmp;