将数据表传递到存储过程有没有更好的办法?

问题描述

您可以在SQL中创建用户定义的表类型。然后,在存储过程中,接受类型(您用户定义的表类型)的参数,并将数据表作为其值传递给存储过程。

这是来自http://msdn.microsoft.com/zh- cn/library/bb675163.aspx的一些示例:

在SQL中:

CREATE TYPE dbo.CategoryTableType AS TABLE
    ( CategoryID int, CategoryName nvarchar(50) )

然后:

// Assumes connection is an open SqlConnection object.
using (connection)
{
// Create a DataTable with the modified rows.
DataTable addedCategories =
  CategoriesDataTable.GetChanges(DataRowState.Added);

// Configure the SqlCommand and SqlParameter.
SqlCommand insertCommand = new SqlCommand(
    "usp_InsertCategories", connection);
insertCommand.CommandType = CommandType.StoredProcedure;

SqlParameter tvpParam = insertCommand.Parameters.AddWithValue(
    "@tvpNewCategories", addedCategories);
tvpParam.SqlDbType = SqlDbType.Structured;

// Execute the command.
insertCommand.ExecuteNonQuery();
}

解决方法

可以将数据表以某种方式传递到SQL Server 2005或2008吗?

我知道将XML传递到SP的标准方法。而且,可以通过某种方式轻松地将数据表转换为XML。

将.NET对象传递到SP中怎么办?那可能吗 ?

我记得曾经听说过SQL和CLR在2008年以某种方式一起工作,但我从未理解。。也许这意味着您可以在存储过程中引用.NET对象?

相关问答

依赖报错 idea导入项目后依赖报错,解决方案:https://blog....
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下...
错误1:gradle项目控制台输出为乱码 # 解决方案:https://bl...
错误还原:在查询的过程中,传入的workType为0时,该条件不起...
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct...