存储过程传入datatable

存储过程传入一般的参数都很简单,今天要说一下存储过程传入datatable 类型

 首先要自定义一个 表类型

CREATE TYPE [dbo].[servicedatableType] AS TABLE
    (
    category int  NULL,
    class int  NULL,
    packname nvarchar(1000)  NULL,
    packid int  NULL ,
    serviceid int  null,
    servicename nvarchar(500)  null,
    serviceprice decimal(18,2) null,
    servicecomment  nvarchar(4000)  null,
    servicecategory int  null,
    sstate int null,
    iscarray int null
    )

GO

我们自定义了表类型以后 在存储过程中就可以 直接用了

@servicollection servicedatableType readonly

这里我们定义了一个 表结构的字段, 在 存储过程调用的时候直接传入 datatable 就行了。

new SqlParameter("@servicollection",dt)

这里再 介绍一个我自己写的例子, 需求是将传入的 databke 遍历  验证是否存在, 不存在则执行写入,此处遍历datable时 用了 游标

create procedure  pr_InsertPackinfoandService(
@category int,
@packid int,
@class int,
@packname nvarchar(500),
@price decimal(18,2),
@comment nvarchar(4000),
@status int,
@chargestatus int,
@conecssionprice decimal(18,2),
@renewprice decimal(18,2),
@statrtime datetime,
@endtime datetime,
@renewyers int,
@renewtimes int,
@buytimes int,
@iscarry int,
@servicollection servicedatableType readonly
)
as
declare @isCount int
declare @pspackedid int
declare @pscategory int
declare @psclass int
declare @pkname nvarchar(100)
declare @serviceid nvarchar(100)
declare @servicename nvarchar(300)
declare @pscomment nvarchar(200)
declare @servicecate int
declare @serviceprice decimal(18,2)
declare @psstatus int
declare @psiscarry int 
begin

 set @isCount=(select COUNT(*) from t_packages_info where pi_category=@category and pi_class=@class  and pi_packageid=@packid  )
 if(@isCount=0) --判断套餐 是否存在
 begin
 --执行添加操作
 insert into t_packages_info (pi_category,pi_class,pi_packageid,pi_packname,pi_price,pi_comment,pi_status,pi_chargestatus,pi_ConcessionalPrice,pi_RenewPrice,pi_AvailableEndTime,pi_AvailableStartTime,pi_RenewYears,pi_RenewTimes,pi_BuyTimes,pi_IsCarray)
 values(@category,@class,@packid,@packname,@price,@comment,@status,@chargestatus,@conecssionprice,@renewprice,@endtime,@statrtime,@renewyers,@renewtimes,@buytimes,@iscarry)
 
 --执行添加服务
 
 declare cur_serList cursor scroll For 
    select category, class,packname,packid,servicename,serviceprice,servicecomment,servicecategory,sstate,iscarray from @servicollection
    fetch first from cur_serList into  @pscategory,@psclass,@pkname ,@serviceid,@servicename,@serviceprice,@pscomment,@servicecate,@psstatus,@psiscarry
   While @@FETCH_STATUS=0 
   if((select COUNT(*) from t_package_service where ps_serviceid=@serviceid and pi_class=@psclass and pi_category=@pscategory )=0)
   begin
   --执行添加操作
    insert into t_package_service(pi_category,pi_packageid,pi_class,pi_packname,ps_serviceid,ps_servicename,ps_serviceprice,ps_comment,ps_ServiceCategory,ps_State,ps_IsCarray)
    values(@pscategory,@pspackedid, @psclass,@pkname,@serviceid,@servicename,@serviceprice,@pscomment,@servicecate,@psstatus,@psiscarry)
   end
   
   fetch next from cur_serList into @pscategory,@psclass,@pkname ,@serviceid,@servicename,@serviceprice,@pscomment,@servicecate,@psstatus,@psiscarry
 end
end
Close cur_serList;----关闭游标
deallocate cur_serList ------删除游标

 

转载于:https://www.cnblogs.com/zplvpp520/p/6978254.html

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值