--
--4.3.1节示例
--
--DATETIME2不同精度
--定义不同精度的DATETIME2数据类型的变量
DECLARE @D0 datetime2(0) = '1984-12-29 13:14:15.1234567';
DECLARE @D1 datetime2(1) = '1984-12-29 13:14:15.1234567';
DECLARE @D2 datetime2(2) = '1984-12-29 13:14:15.1234567';
DECLARE @D3 datetime2(3) = '1984-12-29 13:14:15.1234567';
DECLARE @D4 datetime2(4) = '1984-12-29 13:14:15.1234567';
DECLARE @D5 datetime2(5) = '1984-12-29 13:14:15.1234567';
DECLARE @D6 datetime2(6) = '1984-12-29 13:14:15.1234567';
DECLARE @D7 datetime2(7) = '1984-12-29 13:14:15.1234567';
--输出各个精度变量的值
PRINT @D0;
PRINT @D1;
PRINT @D2;
PRINT @D3;
PRINT @D4;
PRINT @D5;
PRINT @D6;
PRINT @D7;
--使用DATE类型
DECLARE @d date
SET @d='1984/2/29'
PRINT @d
--TIME不同精度
--定义不同精度的时间类型变量
DECLARE @t0 time(0) = '13:14:15.1234567';
DECLARE @t1 time(1) = '13:14:15.1234567';
DECLARE @t2 time(2) = '13:14:15.1234567';
DECLARE @t3 time(3) = '13:14:15.1234567';
DECLARE @t4 time(4) = '13:14:15.1234567';
DECLARE @t5 time(5) = '13:14:15.1234567';
DECLARE @t6 time(6) = '13:14:15.1234567';
DECLARE @t7 time(7) = '13:14:15.1234567';
--打印时间变量
PRINT @t0;
PRINT @t1;
PRINT @t2;
PRINT @t3;
PRINT @t4;
PRINT @t5;
PRINT @t6;
PRINT @t7;
--DATETIMEOFFSET的使用
DECLARE @d DATETIMEOFFSET(5) = '1984-12-29 13:14:15.1234567 +08:00'
PRINT @d
--
--4.3.3节示例
--
--使用WKT初始化空间数据实例
DECLARE @g geometry;
SET @g = geometry::STGeomFromText('POINT (1 2)', 0);
--等同于:
SET @g = geometry::Parse('PoINT (1 2)');
--将空间数据实例转换为WKT
DECLARE @g geometry;
SET @g = geometry::STGeomFromText('POINT (1 2)', 0);
SELECT @g.ToString()
--
--4.3.4节示例
--
--创建具有表值参数的存储过程
USE AdventureWorks;
GO
CREATE TYPE myTableType AS TABLE (id INT); --创建表类型
GO
--接下来创建使用表值参数的存储过程
CREATE PROCEDURE myProc (@tvp myTableType READONLY)
AS
SELECT *
FROM Person.AddressType at
INNER JOIN @tvp AS tvp --将表值参数当表对象来使用
ON at.AddressTypeID=tvp.id
GO
--调用具有表值参数的存储过程
DECLARE @list AS myTableType;
INSERT INTO @list VALUES (1),(3),(5); --向表类型变量中插入数据
EXEC myProc @list;