Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to declare an array inside MS SQL Server Stored Procedure?

I need to declare 12 decimal variables, corresponding to each month's year, with a cursor I sum values to this variables, then later I Update some sales information.

I don't know if sql server has this syntax

 Declare MonthsSale(1 to 12) as decimal(18,2) 

This code works Ok. !

CREATE PROCEDURE [dbo].[proc_test] AS BEGIN  --SET NOCOUNT ON;  DECLARE @monthsales TABLE ( monthnr int,    amount decimal(18,2)    )   -- PUT YOUR OWN CODE HERE   -- THIS IS TEST CODE -- 1 REPRESENTS JANUARY, ... INSERT @monthsales (monthnr, amount) VALUES (1, 100) INSERT @monthsales (monthnr, amount) VALUES (1, 100)  INSERT @monthsales (monthnr, amount) VALUES (2, 200) INSERT @monthsales (monthnr, amount) VALUES (3, 300) INSERT @monthsales (monthnr, amount) VALUES (4, 400) INSERT @monthsales (monthnr, amount) VALUES (5, 500) INSERT @monthsales (monthnr, amount) VALUES (6, 600) INSERT @monthsales (monthnr, amount) VALUES (7, 700) INSERT @monthsales (monthnr, amount) VALUES (8, 800) INSERT @monthsales (monthnr, amount) VALUES (9, 900) INSERT @monthsales (monthnr, amount) VALUES (10, 1000) INSERT @monthsales (monthnr, amount) VALUES (11, 1100) INSERT @monthsales (monthnr, amount) VALUES (12, 1200)   SELECT monthnr, SUM(amount) AS SUM_MONTH_1 FROM @monthsales WHERE monthnr = 1 GROUP BY monthnr SELECT monthnr, SUM(amount) AS SUM_MONTH_2 FROM @monthsales WHERE monthnr = 2 GROUP BY monthnr SELECT monthnr, SUM(amount) AS SUM_MONTH_3 FROM @monthsales WHERE monthnr = 3 GROUP BY monthnr SELECT monthnr, SUM(amount) AS SUM_MONTH_4 FROM @monthsales WHERE monthnr = 4 GROUP BY monthnr SELECT monthnr, SUM(amount) AS SUM_MONTH_5 FROM @monthsales WHERE monthnr = 5 GROUP BY monthnr SELECT monthnr, SUM(amount) AS SUM_MONTH_6 FROM @monthsales WHERE monthnr = 6 GROUP BY monthnr SELECT monthnr, SUM(amount) AS SUM_MONTH_7 FROM @monthsales WHERE monthnr = 7 GROUP BY monthnr SELECT monthnr, SUM(amount) AS SUM_MONTH_8 FROM @monthsales WHERE monthnr = 8 GROUP BY monthnr SELECT monthnr, SUM(amount) AS SUM_MONTH_9 FROM @monthsales WHERE monthnr = 9 GROUP BY monthnr SELECT monthnr, SUM(amount) AS SUM_MONTH_10 FROM @monthsales WHERE monthnr = 10 GROUP BY monthnr SELECT monthnr, SUM(amount) AS SUM_MONTH_11 FROM @monthsales WHERE monthnr = 11 GROUP BY monthnr SELECT monthnr, SUM(amount) AS SUM_MONTH_12 FROM @monthsales WHERE monthnr = 12 GROUP BY monthnr  -- END TEST CODE END 
like image 223
RicardoBalda Avatar asked Nov 13 '09 23:11

RicardoBalda


People also ask

Can we declare array in stored procedure?

Arrays are a convenient way of passing transient collections of data between an application and a stored procedure or between two stored procedures. Within SQL stored procedures, arrays can be manipulated as arrays in conventional programming languages.

How do I declare an array variable in SQL Server stored procedure?

Create a stored procedure with arrays as parameters. Define arrays as SQL variables. Use the ARRAY_AGG built-in function in a cursor declaration, to assign the rows of a single-column result table to elements of an array. Use the cursor to retrieve the array into an SQL out parameter.

Can we store array in mssql?

Although an array is one of the most common data types in the world of programming, MySQL actually doesn't support saving an array type directly. You can't create a table column of array type in MySQL. The easiest way store array type data in MySQL is to use the JSON data type.


2 Answers

You could declare a table variable (Declaring a variable of type table):

declare @MonthsSale table(monthnr int) insert into @MonthsSale (monthnr) values (1) insert into @MonthsSale (monthnr) values (2) .... 

You can add extra columns as you like:

declare @MonthsSale table(monthnr int, totalsales tinyint) 

You can update the table variable like any other table:

update m set m.TotalSales = sum(s.SalesValue) from @MonthsSale m left join Sales s on month(s.SalesDt) = m.MonthNr 
like image 133
Andomar Avatar answered Sep 20 '22 13:09

Andomar


Is there a reason why you aren't using a table variable and the aggregate SUM operator, instead of a cursor? SQL excels at set-oriented operations. 99.87% of the time that you find yourself using a cursor, there's a set-oriented alternative that's more efficient:

declare @MonthsSale table ( MonthNumber int, MonthName varchar(9), MonthSale decimal(18,2) )  insert into @MonthsSale select     1, 'January', 100.00 union select         2, 'February', 200.00 union select         3, 'March', 300.00 union select         4, 'April', 400.00 union select         5, 'May', 500.00 union select         6, 'June', 600.00 union select         7, 'July', 700.00 union select         8, 'August', 800.00 union select         9, 'September', 900.00 union select         10, 'October', 1000.00 union select         11, 'November', 1100.00 union select         12, 'December', 1200.00  select * from @MonthsSale    select SUM(MonthSale) as [TotalSales] from @MonthsSale 
like image 31
Paul Smith Avatar answered Sep 17 '22 13:09

Paul Smith