将两个数组传递给SQL Server存储过程

时间:2014-05-05 12:33:40

标签: sql-server-2008 stored-procedures

我的.net项目中有一项要求是使用存储过程将数组存储到数据库中。我在网上搜索,但结果却很混乱。有一些tut.s像This和一个称为表值参数的概念。我想将这两个数组插入到具有相应值的表中。

我的两个数组看起来像:

stop={Hydrabad, Banglore, Chennai} 

time={15:00, 00:11, 4:45}

我的存储过程:

ALTER proc [dbo].[InsertRoutes] 
    @RouteNo nvarchar(10),
    @RouteName nvarchar(30),
    @StartingTime time(7),
    @MorningCollege time(7),
    @EveningCollege time(7),
    @Stop nvarchar(200),
    @Time nvarchar(200)
AS
BEGIN
    insert into tbl_Routes([RouteNo], [RouteName], [StartingTime],  
                           [MorningToCollege], [EveningFromCollege])
    values(@RouteNo, @RouteName, @StartingTime,
           @MorningCollege, @EveningCollege)
END

停止时间应插入另一个表tbl_stops

我在sql中不是那么多亲,请一些机构建议好的教程或工作代码

更新无论如何,我可以通过使用dbo.split将一个数组保存到一列来设置我自己

       insert into tbl_Stop(RouteName)
       select DATA from dbo.Split('1212,323,34532',',') 

但我无法进一步插入两列,如:

       insert into tbl_stop(RouteName,Stop)
       ................................
       ................................

上面写的是什么????

1 个答案:

答案 0 :(得分:0)

您提供的链接包含将数组传递给存储过程并处理它们的各种方法。如果您正确地遵循该教程,您可以解决您的问题。

但是,您可以浏览this article,其中显示了如何使用存储过程在表中插入多个值(数组)。

表值参数是从Sql Server 2008开始的另一个选项。查看解释TVP的this教程。 This教程展示了如何在asp.net中使用TVP。

相关问题