批量删除,批量插入不再烦人!

目前的批量删除和批量更新,很是烦人,解决方案无非三种:

XML,SQL自定义函数split,和CLR实现split。这几种都比较烦人,代码很多,维护麻烦,很不爽。

现在sql2008新增的一个功能,我也不知道中文名怎么翻译,暂且叫他表参数吧。

大家可以看看示例:

这个就是用户定义的表类型:

 

 

然后给他定义一个类型:

 

ContractedBlock.gif ExpandedBlockStart.gif Code
-- ================================
--
 Create User-defined Table Type
--
 ================================
USE Test
GO

-- Create the data type
CREATE TYPE dbo.MyType AS TABLE 
(
    col1 
int NOT NULL
    col2 
varchar(20NULL
    col3 
datetime NULL
    
PRIMARY KEY (col1)
)
GO

 

可以看到,生成的表类型的组成情况,并且居然可以给表类型建立索引,呵呵

 

这个是操作 表类型的脚本:

 

ContractedBlock.gif ExpandedBlockStart.gif Code
DECLARE @MyTable MyType

INSERT INTO @MyTable(col1,col2,col3)
VALUES (1,'abc','1/1/2000'),
    (
2,'def','1/1/2001'),
    (
3,'ghi','1/1/2002'),
    (
4,'jkl','1/1/2003'),
    (
5,'mno','1/1/2004')
    
SELECT * FROM @MyTable 

 

 

下面演示如何将表参数作为一个存储过程参数传递,以及ADO.NET的代码

sql部分:

 

ContractedBlock.gif ExpandedBlockStart.gif Code
USE [Test]
GO

CREATE TABLE [dbo].[MyTable] (
    
[col1] [int] NOT NULL PRIMARY KEY,
    
[col2] [varchar](20NULL,
    
[col3] [datetime] NULL,
    
[UserID] [varchar] (20NOT NULL
    )    

GO

CREATE PROC usp_AddRowsToMyTable @MyTableParam MyType READONLY,
    
@UserID varchar(20AS
    
    
INSERT INTO MyTable([col1],[col2],[col3],[UserID])
    
SELECT [col1],[col2],[col3],@UserID    
    
FROM @MyTableParam
    
GO

 

 

如何在sql中调用此存储过程:

 

ContractedBlock.gif ExpandedBlockStart.gif Code
DECLARE @MyTable MyType

INSERT INTO @MyTable(col1,col2,col3)
VALUES (1,'abc','1/1/2000'),
    (
2,'def','1/1/2001'),
    (
3,'ghi','1/1/2002'),
    (
4,'jkl','1/1/2003'),
    (
5,'mno','1/1/2004')

EXEC usp_AddRowsToMyTable @MyTableParam = @MyTable@UserID = 'Kathi'

SELECT * FROM MyTable

 

其中还涉及到一个权限问题,需要执行以下代码:

 

ContractedBlock.gif ExpandedBlockStart.gif Code
GRANT EXECUTE ON TYPE::dbo.MyType TO TestUser;

 

从.net app那调用此存储过程:

 

ContractedBlock.gif ExpandedBlockStart.gif Code
'Create a local table
Dim table As New DataTable("temp")
Dim col1 As New DataColumn("col1", System.Type.GetType("System.Int32"))
Dim col2 As New DataColumn("col2", System.Type.GetType("System.String"))
Dim col3 As New DataColumn("col3", System.Type.GetType("System.DateTime"))
table.Columns.Add(col1)
table.Columns.Add(col2)
table.Columns.Add(col3)
        
'Populate the table
For i As Integer = 20 To 30
    
Dim vals(2As Object
    vals(
0= i
    vals(
1= Chr(i + 90)
    vals(
2= System.DateTime.Now
    table.Rows.Add(vals)
Next

 

 

 

ContractedBlock.gif ExpandedBlockStart.gif Code
'Create a command object that calls the stored proc
Dim command As New SqlCommand("usp_AddRowsToMyTable", conn)
command.CommandType = CommandType.StoredProcedure

'Create a parameter using the new type
Dim param As SqlParameter = command.Parameters.Add("@MyTableParam", SqlDbType.Structured)
command.Parameters.AddWithValue("@UserID""Kathi")

 

 

ContractedBlock.gif ExpandedBlockStart.gif Code
'Set the value of the parameter
param.Value = table

'Execute the query
command.ExecuteNonQuery()

 

详情可以参看:

http://www.sqlteam.com/article/sql-server-2008-table-valued-parameters

 

转载于:https://www.cnblogs.com/perfectdesign/archive/2008/08/11/sql-server-2008-table-valued-parameters.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值