SQL Server 2008可以创自己的表类型,方便应用到重复使用的地方,这样就不必重复定义表代码。
下面是定义表类型的例子,
IF TYPE_ID ('dbo.MyReport') IS NOT NULL
DROP TYPE dbo.MyReport
CREATE TYPE dbo.MyReport AS TABLE
(
[Id] [int],
[OldValue] [decimal](18, 6),
[NewValue] [decimal](18, 6)
)
DROP TYPE dbo.MyReport
CREATE TYPE dbo.MyReport AS TABLE
(
[Id] [int],
[OldValue] [decimal](18, 6),
[NewValue] [decimal](18, 6)
)
接下,我们怎样使用这个表类型呢,它跟使用SQL 标准数据类型很相似,参考下面代码,
代码
--定义一个变量为自己定义表类型
DECLARE @SalaryReport AS MyReport
UPDATE [dbo].[Member] SET [Salary] = [Salary] * (1 + 0.05)
OUTPUT deleted.[MemberId],deleted.[Salary],inserted.[Salary]
INTO @SalaryReport
SELECT * FROM @SalaryReport
DECLARE @SalaryReport AS MyReport
UPDATE [dbo].[Member] SET [Salary] = [Salary] * (1 + 0.05)
OUTPUT deleted.[MemberId],deleted.[Salary],inserted.[Salary]
INTO @SalaryReport
SELECT * FROM @SalaryReport