SQL Server 2008:传递表值参数
SQL Server 2008 则能够把 表值数据类型 传递 到存储过程和功能中,从而大大地简化了编程的工作,因为程序员无需再花心思去组建和解析XML数据了…… 许多人一直希望能够实现把表格变量 传递 到存储过程中,如果变量可以被声明,那么它就应该能够被 传递 。而
SQL Server 2008则能够把表值数据类型传递到存储过程和功能中,从而大大地简化了编程的工作,因为程序员无需再花心思去组建和解析XML数据了……
许多人一直希望能够实现把表格变量传递到存储过程中,如果变量可以被声明,那么它就应该能够被传递。而最新的SQL Server 2008则有这项功能!想知道如何才能把表格变量(包括内含的数据)传递到存储过程和功能中去吗?
为什么要传递表值参数?
用户常常会碰到许多需要把数值容器而非单个数值放到存储过程里的情况。对于大部分的编程语言而言,把容器数据结构传递到例程里或传递出来是很常见而且很必要的功能。TSQL也不例外。
SQL Server 2000通过OPENXML可以实现这个功能,用户可以把数据存储为VARCHAR数据类型然后进行传递。到了SQL Server 2005,随着 XML数据类型以及XQuery的出现,这个功能变得容易一点。但用户仍然需要对XML数据进行组建和粉碎才能够使用它,因此这个功能使用起来并不简单。SQL Server 2008则能够把表值数据类型传递到存储过程和功能中,从而大大地简化了编程的工作,因为程序员无需再花心思去组建和解析XML数据了。该功能还可以让客户方开发员传递客户方数据表格到数据库中。
如何传递表格参数?
以销售为例,首先建立一个 my SalesHistory表格,里面包含了产品销售的信息。写以下脚本就可以在数据库里创建你选择的表格:
IF OBJECT_ID('SalesHistory')>0 DROP TABLE SalesHistory; GO CREATE TABLE [dbo].[SalesHistory] ( [SaleID] [int] IDENTITY(1,1) NOT NULL PRIMARY KEY, [Product] [varchar](10) NULL, [SaleDate] [datetime] NULL, [SalePrice] [money] NULL ) GO |
建立表值参数第一步是创建确切的表格类型,这一步非常重要,因为这样你就可以在数据库引擎里定义表格的结构,让你可以在需要的时候在过程代码里使用该表格。下面的代码创建 SalesHistoryTableType 表格类型定义:
CREATE TYPE SalesHistoryTableType AS TABLE ( [Product] [varchar](10) NULL, [SaleDate] [datetime] NULL, [SalePrice] [money] NULL ) GO |
如果想要查看系统里其他类型的表格类型定义,你可以执行下面这个查询命令,查看系统目录:
SELECT * FROM sys.table_types
我们需要定义用来处理表值参数的存储过程。下面这个程序能够接受指定SalesHistoryTableType类型的表值参数,并加载到SalesHistory中,表值参数在Product列中的值为“BigScreen”:
CREATE PROCEDURE usp_InsertBigScreenProducts ( @TableVariable SalesHistoryTableType READONLY ) AS BEGIN INSERT INTO SalesHistory ( Product, SaleDate, SalePrice ) SELECT Product, SaleDate, SalePrice FROM @TableVariable WHERE Product = 'BigScreen' END GO |
传递的表格变量还可以用做任何其他表格的查询数据。
传递表值参数功能的局限性
在传递表值变量到程序中时必须使用 READONLY从句。表格变量里的数据不能做修改――除了修改你可以把数据用于任何其他的操作。另外,你也不能把表格变量用做OUTPUT参数――只能用做input参数。
使用自己的新表格变量类型
首先,要声明一个变量类型SalesHistoryTableType,不需要再一次定义表格结构,因为在创建这个表格类型的时候已经定义过了。
DECLARE @DataTable AS SalesHistoryTableType The following script adds 1,000 records into my @DataTable table variable: DECLARE @i SMALLINT SET @i = 1 WHILE (@i BEGIN INSERT INTO @DataTable(Product, SaleDate, SalePrice) VALUES ('Computer', DATEADD(mm, @i, '3/11/1919'), DATEPART(ms, GETDATE()) + (@i + 57)) INSERT INTO @DataTable(Product, SaleDate, SalePrice) VALUES('BigScreen', DATEADD(mm, @i, '3/11/1927'), DATEPART(ms, GETDATE()) + (@i + 13)) INSERT INTO @DataTable(Product, SaleDate, SalePrice) VALUES('PoolTable', DATEADD(mm, @i, '3/11/1908'), DATEPART(ms, GETDATE()) + (@i + 29)) SET @i = @i + 1 END |
只要把数据加载到表格变量里,就可以把结构传递到存储过程中。
注意:当表格变量作为参数传递后,表格会在存储在tempdb系统数据库里,而不是传递整个数据集在内存里。因为这样保证高效处理大批量数据。所有服务器方的表格变量参数传递都是通过使用reference调用tempdb中的表格。
EXECUTE usp_InsertBigScreenProducts @TableVariable = @DataTable |
想要查询程序是否和预想效果一样,可以执行以下查询来看记录是否已经插入到 SalesHistory表格中:
以下是引用片段: SELECT * FROM SalesHistory |
总结:
虽然SQL Server 2008的参数传递功能的使用还有一些小小的局限性,比如不能修改参数中的数据和把变量用于output,但是它大大提高了程序性能,它可以减少server往返旅程数、利用表格限制并扩展编程在数据库引擎中的功能。
精彩推荐
|
SQL Server 2008 功能、特性介绍 |

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



HQL and SQL are compared in the Hibernate framework: HQL (1. Object-oriented syntax, 2. Database-independent queries, 3. Type safety), while SQL directly operates the database (1. Database-independent standards, 2. Complex executable queries and data manipulation).

"Usage of Division Operation in OracleSQL" In OracleSQL, division operation is one of the common mathematical operations. During data query and processing, division operations can help us calculate the ratio between fields or derive the logical relationship between specific values. This article will introduce the usage of division operation in OracleSQL and provide specific code examples. 1. Two ways of division operations in OracleSQL In OracleSQL, division operations can be performed in two different ways.

Oracle and DB2 are two commonly used relational database management systems, each of which has its own unique SQL syntax and characteristics. This article will compare and differ between the SQL syntax of Oracle and DB2, and provide specific code examples. Database connection In Oracle, use the following statement to connect to the database: CONNECTusername/password@database. In DB2, the statement to connect to the database is as follows: CONNECTTOdataba

Interpretation of MyBatis dynamic SQL tags: Detailed explanation of Set tag usage MyBatis is an excellent persistence layer framework. It provides a wealth of dynamic SQL tags and can flexibly construct database operation statements. Among them, the Set tag is used to generate the SET clause in the UPDATE statement, which is very commonly used in update operations. This article will explain in detail the usage of the Set tag in MyBatis and demonstrate its functionality through specific code examples. What is Set tag Set tag is used in MyBati

What is Identity in SQL? Specific code examples are needed. In SQL, Identity is a special data type used to generate auto-incrementing numbers. It is often used to uniquely identify each row of data in a table. The Identity column is often used in conjunction with the primary key column to ensure that each record has a unique identifier. This article will detail how to use Identity and some practical code examples. The basic way to use Identity is to use Identit when creating a table.

Solution: 1. Check whether the logged-in user has sufficient permissions to access or operate the database, and ensure that the user has the correct permissions; 2. Check whether the account of the SQL Server service has permission to access the specified file or folder, and ensure that the account Have sufficient permissions to read and write the file or folder; 3. Check whether the specified database file has been opened or locked by other processes, try to close or release the file, and rerun the query; 4. Try as administrator Run Management Studio as etc.

WindowsServerBackup is a function that comes with the WindowsServer operating system, designed to help users protect important data and system configurations, and provide complete backup and recovery solutions for small, medium and enterprise-level enterprises. Only users running Server2022 and higher can use this feature. In this article, we will explain how to install, uninstall or reset WindowsServerBackup. How to Reset Windows Server Backup If you are experiencing problems with your server backup, the backup is taking too long, or you are unable to access stored files, then you may consider resetting your Windows Server backup settings. To reset Windows

i9-12900H is a 14-core processor. The architecture and technology used are all new, and the threads are also very high. The overall work is excellent, and some parameters have been improved. It is particularly comprehensive and can bring users Excellent experience. i9-12900H parameter evaluation review: 1. i9-12900H is a 14-core processor, which adopts the q1 architecture and 24576kb process technology, and has been upgraded to 20 threads. 2. The maximum CPU frequency is 1.80! 5.00ghz, which mainly depends on the workload. 3. Compared with the price, it is very suitable. The price-performance ratio is very good, and it is very suitable for some partners who need normal use. i9-12900H parameter evaluation and performance running scores
