T-SQL基础--chp10可编程对象学习笔记

--用DECLARE声明变量;用SET给变量赋值
--要注意,如果声明的变量类型不是SQL所属的,就会报错。
DECLARE @j AS INT;
SET @j = 10;

DECLARE @i AS float;
SET @i = 10.0;

DECLARE @m AS CHAR;
SET @m=M;

DECLARE @n AS DATETIME =2014-9-28 9:18:16;

USE TSQLFundamentals2008
DECLARE @empname AS NVARCHAR(61)
SET @empname = (SELECT firstname+N +lastname FROM HR.Employees WHERE empid=9)
SELECT @empname AS hah;

DECLARE @FN AS NVARCHAR(20),@LN AS NVARCHAR(40)
SET @FN = (SELECT firstname FROM HR.Employees WHERE empid = 7)
SET @LN = (SELECT lastname FROM HR.Employees WHERE empid = 7)
SELECT @FN AS 姓,@LN AS 名;

--用赋值SELECT语句赋值,结果只会输出最后一行;用SET赋值更安全
DECLARE @emp_name AS NVARCHAR(60)
SELECT @emp_name=firstname+ +lastname FROM HR.Employees WHERE mgrid = 2
SELECT @emp_name AS 雇员名字

--批处理
PRINT fb--显示在消息里
SELECT * FROM HR.Employees --显示在结果里
GO
PRINT fb2
SELECT * FROM Sales.Orders
GO
PRINT fb3
SELECT * FROM Sales.Shippers
GO
--下面的程序会报错!注意原因!其次还包括视图,rule等。。。
if OBJECT_ID([dbo].[HwocaoSA],p) IS NOT NULL
drop procedure [dbo].[HwocaoSA]
--GO
CREATE PROCEDURE HwocaoSA
AS
BEGIN
select * from [HR].[Employees]
END
--【有点疑问】书P324[书上说下面这段代码是不可行的,但是事实上是可行的。why?]
IF OBJECT_ID(Table_1,U) IS NOT NULL
DROP TABLE Table_1 
create table Table_1(hihi int)
ALTER TABLE Table_1 ADD HEHE INT
SELECT hihi,HEHE FROM Table_1
GO 100

--流程控制元素
--IF...ELSE...
IF YEAR(CURRENT_TIMESTAMP) <> YEAR(DATEADD(day,1,CURRENT_TIMESTAMP))--<>是不等于,正式,!=不正式
PRINT Today is the last day of year;
ELSE
PRINT Today is not the last day of year;

--WHILE循环
DECLARE @i AS INT;
SET @i = 1;
WHILE @i <=10
BEGIN
PRINT @i;
SET @i = 1+@i;--最开始这刚没加‘SET‘ 报错。SET是赋值的意思啊亲!!
END
--跳出当前循环:BREAK
DECLARE @F AS INT;
SET @F = 10;
WHILE @F > 0
BEGIN
IF (@F < 2) BREAK;
PRINT @F;
SET @F = @F -1;
END

--if else和while结合使用
SET NOCOUNT ON;
IF OBJECT_ID(TABLE_1,U) IS NOT NULL --如果用户表TABLE_1不为空
DROP TABLE TABLE_1
CREATE TABLE TABLE_1(num1 INT NOT NULL PRIMARY KEY)
GO
DECLARE @i AS INT;
SET @i = 1;
WHILE @i < 124
BEGIN
INSERT INTO TABLE_1(num1) VALUES (@i)
SET @i = @i +1;
END
GO
SELECT * FROM TABLE_1
GO

--用游标来计算Sales.CustOrders视图中每个客户每个月的连续订货量
--如果不知道是怎么执行的,可以单步调试下
SET NOCOUNT ON;
USE TSQLFundamentals2008;

DECLARE @Result TABLE
(
  custid INT,
  ordermonth DATETIME,
  qty INT,
  runqty INT,
  PRIMARY KEY(custid,ordermonth)
);

DECLARE
  @custid AS INT,
  @prvcustid AS INT,
  @ordermonth AS DATETIME,
  @qty AS INT,
  @runqty AS INT; --runqty为客户当前连续总订货量
  
DECLARE C CURSOR FAST_FORWARD FOR  --基于一个查询声明了一个游标
  SELECT custid,ordermonth,qty
  FROM Sales.CustOrders
  ORDER BY custid,ordermonth;

OPEN C   --打开游标

FETCH NEXT FROM C INTO @custid,@ordermonth,@qty;--将游标移到下一条并相应操作

SELECT @prvcustid = @custid,@runqty=0;

WHILE @@FETCH_STATUS = 0  --当还没有超出游标的最后一行时,通过循环来遍历每个记录
BEGIN
  IF @custid <> @prvcustid
    SELECT @prvcustid = @custid,@runqty = 0;
    
  SET @runqty = @runqty + @qty;
  
  INSERT INTO @Result VALUES(@custid, @ordermonth, @qty, @runqty);
  
  FETCH NEXT FROM C INTO @custid,@ordermonth,@qty;
END

CLOSE C;

DEALLOCATE C;

SELECT 
  custid,
  CONVERT(VARCHAR(7), ordermonth, 121) AS ordermonth,
  qty,
  runqty
FROM @Result
ORDER BY custid, ordermonth;

--局部临时表
USE TSQLFundamentals2008;
IF OBJECT_ID(tempdb.dbo.#MyOrderTotalsYear) IS NOT NULL
  DROP TABLE dbo.#MyOrderTotalsYear;
GO

SELECT 
  YEAR(O.orderdate) AS orderyear,
  SUM(OD.qty) AS qty
INTO dbo.#MyOrderTotalsByYear
FROM Sales.Orders AS O
  JOIN Sales.OrderDetails AS OD
    ON OD.orderid = O.orderid
GROUP BY YEAR(orderdate);

SELECT Cur.orderyear,Cur.qty AS curyearqty,Prv.qty AS prvyearqty
FROM dbo.#MyOrderTotalsByYear AS Cur
  LEFT OUTER JOIN dbo.#MyOrderTotalsByYear AS Prv
    ON Cur.orderyear = Prv.orderyear + 1;
    
--全局临时表
CREATE TABLE dbo.##Globals--创建一个全局临时表
(
  id sysname NOT NULL PRIMARY KEY,--SQL SERVER在内部用这个类型来代表标识符
  val SQL_VARIANT NOT NULL --一种通用的数据类型,差不多可以保存任何基础类型的值
)
--任何人都可以向全局临时表中插入数据行
INSERT INTO dbo.##Globals(id,val)VALUES(Ni,CAST(10 AS INT));

--表变量[只对当前批处理可见]
DECLARE @MyOrderTotalsByYear TABLE
(
  orderyear INT NOT NULL PRIMARY KEY,
  qty INT NOT NULL
);

INSERT INTO @MyOrderTotalsByYear(orderyear, qty)
  SELECT 
    YEAR(O.orderdate) AS orderyear,
    SUM(OD.qty) AS qty  
  FROM Sales.Orders AS O 
    JOIN Sales.OrderDetails AS OD
      ON OD.orderid = O.orderid
  GROUP BY YEAR(orderdate);
  
SELECT Cur.orderyear,Cur.qty AS curyearqty, Prv.qty AS prvyearqty
FROM @MyOrderTotalsByYear AS Cur
  LEFT OUTER JOIN @MyOrderTotalsByYear AS Prv
    ON Cur.orderyear = Prv.orderyear +1;
    
--表类型【额,原样复制代码,但是竟然报错了……】
USE TSQLFundamentals2008;
IF TYPE_ID(dbo.OrderTotalsByYear) IS NOT NULL
  DROP TYPE dbo.OrderTotalsByYear;
  
CREATE TYPE dbo.OrderTotalsByYear AS TABLE
(
  orderyear INT NOT NULL PRIMARY KEY,
  qty INT NOT NULL
);

DECLARE @MyOrderTotalsByYears AS dbo.OrderTotalsByYear;

INSERT INTO @MyOrderTotalsByYears(orderyear, qty)
  SELECT 
    YEAR(O.orderdate) AS orderyear,
    SUM(OD.qty) AS qty
  FROM Sales.Orders AS O
    JOIN Sales.OrderDetails AS OD
      ON O.orderid = OD.orderid
  GROUP BY YEAR(orderdate);
  
SELECT orderyear,qty FROM @MyOrderTotalsByYears;

--P338动态SQL待续。。。

 

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。