【问题标题】:array in stored procedure存储过程中的数组
【发布时间】:2015-03-07 06:34:54
【问题描述】:

请告诉我存储过程sql server 2008中数组声明的语法。

USE [totalsolution]
GO
/****** Object:  StoredProcedure [dbo].[Fact_Table_coursor4]    Script Date: 03/03/2015 14:36:01 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO

CREATE  PROCEDURE [dbo].[Populate_DW_Blockwise_Areawise_Agewise_Distribution]
AS
BEGIN
    -- Declaration for variables required in the destination table
    DECLARE @State_cd varchar(15),@District_cd varchar(15),@Block_cd varchar(15),
    @Area_cd varchar(15), @Age_in_years int,@No_of_persons int;

    -- Declaration for additional variables required 
    DECLARE @currYear int
    DECLARE @CurrDateTime datetime
    DECLARE @DOB      datetime    
    DECLARE @DOBYear  int    
    DECLARE @Age      int

    DECLARE Statewise_Districtwise_Blockwise_Areawise_cursor CURSOR FOR
       select x.state_cd,y.district_cd,z.block_cd,a.area_cd 
              from State x,District y,Block z, Area a
              where x.state_cd=y.state_cd
              and   y.district_cd = z.district_cd
              and   z.block_cd = a.block_cd;

    -- First 4 variables may be obtained from the first table               
    OPEN Statewise_Districtwise_Blockwise_Areawise_cursor 
    FETCH NEXT FROM Statewise_Districtwise_Blockwise_Areawise_cursor 
                    INTO @State_cd,@District_cd,@Block_cd, @Area_cd;

    -- For next 3 variables
    WHILE(@@FETCH_STATUS <> -1)
    BEGIN
          DECLARE AgeArray [100] int;
          DECLARE Areawise_Housewise_HouseOccupancywise_FamilyMemberwise_Personwise_cursor CURSOR FOR
              select x.area_cd,y.house_no,z.uid_head, f.uid_member, p.UID, p.date_of_birth
              from   Area x,House y,  House_occupancy z, Family_member f, Person p
              where x.area_cd = @Area_cd
              and   y.area_cd = @Area_cd
              and   z.area_cd = @Area_cd
              and   y.house_no = z.house_no 
              and   z.to_date = NULL
              and   z.UID_head = f.UID_Head
              and   f.UID_Member = p.UID;

              OPEN Areawise_Housewise_HouseOccupancywise_FamilyMemberwise_Personwise_cursor 
              FETCH NEXT FROM Areawise_Housewise_HouseOccupancywise_FamilyMemberwise_Personwise_cursor 
                          INTO @Area_cd, @House_no, @UID_Head, @UID_Member, @UID_person, @DOB;

              WHILE(@@FETCH_STATUS <> -1)
              BEGIN                                         
                    select DOBYear = Year (@DOB);
                    select GetDate () as CurrDateTime;
                    select currYear = Year (@CurrDateTime);

                   SELECT @Age = @currYear - @DOBYear;
                    AgeArray[Age] = AgeArray [Age]+1;

                    FETCH NEXT FROM Areawise_Housewise_HouseOccupancywise_FamilyMemberwise_Personwise_cursor 
                          INTO @Area_cd, @House_no, @UID_Head, @UID_Member, @UID_person, @DOB;
              END

          DECLARE @i int = 1;

          WHILE (@i <= 100)    
          BEGIN
               select @Age_in_years = @i; 
               @No_of_Persons = AgeArray[i];

               INSERT INTO dbo.DW_Blockwise_Areawise_Agewise_distribution VALUES (@State_cd,@District_cd,@Block_cd,@Area_cd,@Age_in_years,@No_of_persons) ;
          END 
          FETCH NEXT FROM Statewise_Districtwise_Blockwise_Areawise_cursor
                          INTO @State_cd,@District_cd,@Block_cd, @Area_cd;
     END

     CLOSE Statewise_Districtwise_Blockwise_Areawise_cursor;
     DEALLOCATE Statewise_Districtwise_Blockwise_Areawise_cursor;

END

【问题讨论】:

  • 我认为 Areawise_Housewise_HouseOccupancywise_FamilyMemberwise_Personwise_cursor 是我见过的最好的命名光标:D
  • SQL Server 没有数组或列表 - 如果您需要存储多个元素,请使用 ,这就是 RDBMS 用于这些任务的方法。
  • 请改变主意。停止逐行思考。您应该以基于集合的方式处理问题。这里不需要游标和数组。

标签: arrays sql-server stored-procedures


【解决方案1】:

sql server 中没有像 ARRAYS 这样的东西。

您可以使用表类型或临时表来代替数组

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-05-08
    • 2014-12-28
    • 2012-11-15
    • 2016-10-11
    • 2017-12-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多