如何将 table 中的值作为参数传递给存储过程?
How do I pass values from a table to a stored procedure as parameters?
我正在努力解决一些看起来非常简单的问题,但我没有找到任何解决方案:
- 我 Table 称为 Hotels,它的第一列是 = [hotels_id] [int]
- 我有依赖于hotels_id值
的存储过程
现在在存储过程中,我只输入了一个值 (1) 作为示例,假设我有大约 50 行。
有什么方法可以将 table 中的所有行作为参数一次传递给 SP?我是说一个一个。
ALTER PROCEDURE [dbo].[spAvailableRooms]
AS
BEGIN
-- Derived tables
;with
DatesTable as
(SELECT top (100) PERCENT Dates.dates_id as Id, Dates.dates_date as Date FROM Dates order by Dates.dates_id),
AvaliablePerHotel as
(SELECT top (100)percent Available_since AS Since, Available_value AS Value, Available_hotel as Hotel
FROM Available_rooms
where Available_hotel =1 --(HERE I NEED THE VALUES FROM TABLE)
ORDER BY Available_hotel, Available_since),
AllDays as
(Select top (100)percent Id, Date, Value as Rooms, iif(value is null, '0' ,'1') as New, Hotel
From DatesTable left JOIN AvaliablePerHotel ON Id = Since
order by id),
AvailableGroups as
(Select top (100)percent Hotel, Id, Date, Rooms, (sum(isnull(cast(new as float),0))over(order by id)) as RowGroup
From AllDays
order by id)
--
-- Query itself
Select Id, Date, iif(Rooms is null,(first_value(rooms) over (partition by RowGroup order by Id)) , Rooms) as AvailableRooms,
iif(Hotel is null,(first_value(Hotel) over (partition by RowGroup order by Id)) , Hotel) as Hotel
From AvailableGroups
order by id
END
您可以使用用户定义的 Table 类型(更多信息 here)
首先定义一个类型,表示要传递给存储过程的结构:
CREATE TYPE [schemaName].[typeName] AS TABLE(
[Column0] [nvarchar](255) NULL,
[Column1] [nvarchar](255) NULL,
[Column2] [nvarchar](255) NULL,
[Column3] [nvarchar](255) NULL,
[Column4] [nvarchar](255) NULL,
[Column5] [nvarchar](255) NULL,
[Column6] [nvarchar](255) NULL,
...
)
现在您可以创建一个存储过程,它将一个变量作为输入,该变量的类型是用前面的脚本定义的:
CREATE PROCEDURE [schemaName].[SpLoad]
(
@myRows [schemaName].[typeName] READONLY
)
AS
BEGIN
INSERT INTO schemaName.DestinationTable
SELECT * FROM @myRows
END
如果你想像你说的那样一个一个地传递值,那么游标是你最好的选择
declare @id int
DECLARE cursor CURSOR FOR
SELECT hotels_id
FROM Hotels
OPEN cursor
FETCH NEXT FROM cursor
INTO @id
WHILE @@FETCH_STATUS = 0
BEGIN
exec [dbo].[spAvailableRooms] @id
FETCH NEXT FROM cursor
INTO @id
END
CLOSE cursor;
DEALLOCATE cursor;
然后在你的程序中改变传递一个参数
ALTER PROCEDURE [dbo].[spAvailableRooms] @id int
并用 @id
替换您在代码中硬编码“1”的位置
请注意,随着 table 的增长,游标是单线程的,执行时间将增长得非常快。
我正在努力解决一些看起来非常简单的问题,但我没有找到任何解决方案:
- 我 Table 称为 Hotels,它的第一列是 = [hotels_id] [int]
- 我有依赖于hotels_id值 的存储过程
现在在存储过程中,我只输入了一个值 (1) 作为示例,假设我有大约 50 行。
有什么方法可以将 table 中的所有行作为参数一次传递给 SP?我是说一个一个。
ALTER PROCEDURE [dbo].[spAvailableRooms]
AS
BEGIN
-- Derived tables
;with
DatesTable as
(SELECT top (100) PERCENT Dates.dates_id as Id, Dates.dates_date as Date FROM Dates order by Dates.dates_id),
AvaliablePerHotel as
(SELECT top (100)percent Available_since AS Since, Available_value AS Value, Available_hotel as Hotel
FROM Available_rooms
where Available_hotel =1 --(HERE I NEED THE VALUES FROM TABLE)
ORDER BY Available_hotel, Available_since),
AllDays as
(Select top (100)percent Id, Date, Value as Rooms, iif(value is null, '0' ,'1') as New, Hotel
From DatesTable left JOIN AvaliablePerHotel ON Id = Since
order by id),
AvailableGroups as
(Select top (100)percent Hotel, Id, Date, Rooms, (sum(isnull(cast(new as float),0))over(order by id)) as RowGroup
From AllDays
order by id)
--
-- Query itself
Select Id, Date, iif(Rooms is null,(first_value(rooms) over (partition by RowGroup order by Id)) , Rooms) as AvailableRooms,
iif(Hotel is null,(first_value(Hotel) over (partition by RowGroup order by Id)) , Hotel) as Hotel
From AvailableGroups
order by id
END
您可以使用用户定义的 Table 类型(更多信息 here)
首先定义一个类型,表示要传递给存储过程的结构:
CREATE TYPE [schemaName].[typeName] AS TABLE(
[Column0] [nvarchar](255) NULL,
[Column1] [nvarchar](255) NULL,
[Column2] [nvarchar](255) NULL,
[Column3] [nvarchar](255) NULL,
[Column4] [nvarchar](255) NULL,
[Column5] [nvarchar](255) NULL,
[Column6] [nvarchar](255) NULL,
...
)
现在您可以创建一个存储过程,它将一个变量作为输入,该变量的类型是用前面的脚本定义的:
CREATE PROCEDURE [schemaName].[SpLoad]
(
@myRows [schemaName].[typeName] READONLY
)
AS
BEGIN
INSERT INTO schemaName.DestinationTable
SELECT * FROM @myRows
END
如果你想像你说的那样一个一个地传递值,那么游标是你最好的选择
declare @id int
DECLARE cursor CURSOR FOR
SELECT hotels_id
FROM Hotels
OPEN cursor
FETCH NEXT FROM cursor
INTO @id
WHILE @@FETCH_STATUS = 0
BEGIN
exec [dbo].[spAvailableRooms] @id
FETCH NEXT FROM cursor
INTO @id
END
CLOSE cursor;
DEALLOCATE cursor;
然后在你的程序中改变传递一个参数
ALTER PROCEDURE [dbo].[spAvailableRooms] @id int
并用 @id
替换您在代码中硬编码“1”的位置请注意,随着 table 的增长,游标是单线程的,执行时间将增长得非常快。