Preciso declarar 12 variáveis decimais, correspondentes ao ano de cada mês, com um cursor soma os valores a essas variáveis, depois atualizo algumas informações de vendas.
Não sei se o servidor sql tem essa sintaxe
Declare MonthsSale(1 to 12) as decimal(18,2)
Este código funciona bem. !
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
sql-server-2008
stored-procedures
RicardoBalda
fonte
fonte
Que eu saiba, o T-SQL não oferece suporte a matrizes.
Qual é a estrutura da sua tabela? Você provavelmente poderia criar uma consulta que fizesse isso:
select month, sum(sales) from sales_table group by month order by month
fonte
Ótima pergunta e ótima ideia, mas no SQL você precisará fazer isso:
Para o tipo de dados datetime, algo como este-
declare @BeginDate datetime = '1/1/2016', @EndDate datetime = '12/1/2016' create table #months (dates datetime) declare @var datetime = @BeginDate while @var < dateadd(MONTH, +1, @EndDate) Begin insert into #months Values(@var) set @var = Dateadd(MONTH, +1, @var) end
Se tudo o que você realmente quer são números, faça isso-
create table #numbas (digit int) declare @var int = 1 --your starting digit while @var <= 12 --your ending digit begin insert into #numbas Values(@var) set @var = @var +1 end
fonte