SQL Server 2008:表值参数的创建和使用

http://tech.ddvip.com   2008年05月27日    社区交流

内容摘要:表值参数(Table-valued parameter)是SQL Server2008的一个新特性,在以前的版本中,没有办法把表变量当作一个参数传递给存储过程。微软在SQL Server2008中引入了表值参数的特性,可以实现这项功能。

  然后,使用以下的DML SQL语句将数据添加到我们上面创建的表中:

  USE[TestDB]
  GO
  insertintoTestLocationTable(Id,shortname,Name)select1,'NA1','NewYork'
  insertintoTestLocationTable(Id,shortname,Name)select2,'NA2','NewYork'
  insertintoTestLocationTable(Id,shortname,Name)select3,'NA3','NewYork'
  insertintoTestLocationTable(Id,shortname,Name)select4,'EU1','London'
  insertintoTestLocationTable(Id,shortname,Name)select5,'EU2','London'
  insertintoTestLocationTable(Id,shortname,Name)select6,'AS1','Tokyo'
  insertintoTestLocationTable(Id,shortname,Name)select7,'AS2','HongKong'
  go

  下一步,我们要创建一个和TestLocationTable表具有相似表结构的表类型(TABLE TYPE),语句如下:

  USE[TestDB]
  GO
  IFEXISTS(SELECT*FROMsys.typesstJOINsys.schemasssONst.schema_id=ss.schema_id
  WHEREst.name=N'OfficeLocation_Tabetype'ANDss.name=N'dbo')
  DROPTYPE[dbo].[OfficeLocation_Tabetype]
  GO
  USE[TestDB]
  GO
  CREATETYPE[dbo].[OfficeLocation_Tabetype]ASTABLE(
  [Id][int]NULL,
  [shortname][char](3)NULL,
  [name][varchar](100)NULL
  )
  GO

  紧接着,我们要创建一个可以将表类型作为一个参数来接受的存储过程,使用的语句如下:

来源:IT专家网    作者:cyw    责编:豆豆技术应用

正在加载评论...