【问题标题】:How do I pass values from a table to a stored procedure as parameters?如何将表中的值作为参数传递给存储过程?
【发布时间】:2018-09-13 12:08:48
【问题描述】:

我正在为看起来很简单的事情而苦苦挣扎,但我没有找到任何解决方案:

  1. 我有一个名为 Hotels 的表,其中的第一列是 = [hotels_id] [int]
  2. 我有一个依赖于 hotels_id 值的存储过程

现在在存储过程中,我只输入了一个值 (1) 作为示例,假设我有 50 行。

有什么方法可以将表中的所有行作为参数传递给 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

【问题讨论】:

    标签: sql sql-server tsql stored-procedures ssms-2017


    【解决方案1】:

    如果你想一一传递值,那么光标是你最好的选择

    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”的位置

    请注意,随着表的增长,游标是单线程的,执行时间会很快增长。

    【讨论】:

      【解决方案2】:

      您可以使用用户定义的表类型(更多信息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
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-03-01
        • 1970-01-01
        • 2010-10-19
        相关资源
        最近更新 更多