存储过程中的数组

时间:2015-03-07 06:34:54

标签: arrays sql-server stored-procedures

请告诉我存储过程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

1 个答案:

答案 0 :(得分:0)

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

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