SQL 中表值参数的使用

在SQL中,可以使用表值参数作为存储过程的输入参数,这样不用重复定义临时表或者表变量。

表值参数需要采用用户自定义的表类型来声明,所以在使用表值参数之前一定要先定义表类型

下面是敌营表类型的例子:

use Test
go

---------- create table type ----------
create type LocationTableType as table (
	TrustId int
	, TrustCode nvarchar(255)
)
go

定义好表类型LocationTableType之后,就可以声明表值变量了。如下:

declare @TrustInfo LocationTableType

insert into @TrustInfo
values ( 1 , 'AUTO' )
		, ( 2 , 'UNK' )
		, ( 3 ,	'RMBS')

select * from @TrustInfo

下面创建一个存储过程,用表值变量传值

create procedure dbo.usp_GetTrustInfo
	@TrustInfo LocationTableType READONLY
as
begin
	set nocount on

	select * from @TrustInfo
end

完整代码如下


use Test
go

---------- create table type ----------
create type LocationTableType as table (
	TrustId int
	, TrustCode nvarchar(255)
)
go
---------- example ----------

declare @TrustInfo LocationTableType

insert into @TrustInfo
values ( 1 , 'AUTO' )
		, ( 2 , 'UNK' )
		, ( 3 ,	'RMBS')

select * from @TrustInfo

---------- example 1 ----------

if exists ( select  1
            from    sysobjects
            where   id = object_id(N'[dbo].[usp_GetTrustInfo]')
                    and objectproperty(id, N'IsProcedure') = 1 )
    drop procedure dbo.usp_GetTrustInfo
go

create procedure dbo.usp_GetTrustInfo
	@TrustInfo LocationTableType READONLY
as
begin
	set nocount on

	select * from @TrustInfo
end

exec  dbo.usp_GetTrustInfo @TrustInfo

 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值