Làm thế nào để khai báo một mảng bên trong MS SQL Server Stored Procedure?


85

Tôi cần khai báo 12 biến thập phân, tương ứng với từng tháng trong năm, bằng con trỏ Tôi tính tổng các giá trị cho các biến này, sau đó tôi Cập nhật một số thông tin bán hàng.

Tôi không biết máy chủ sql có cú pháp này không

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

Mã này hoạt động 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

Câu trả lời:


143

Bạn có thể khai báo một biến bảng (Khai báo một biến kiểu bảng):

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

Bạn có thể thêm các cột bổ sung tùy thích:

declare @MonthsSale table(monthnr int, totalsales tinyint)

Bạn có thể cập nhật biến bảng giống như bất kỳ bảng nào khác:

update m
set m.TotalSales = sum(s.SalesValue)
from @MonthsSale m
left join Sales s on month(s.SalesDt) = m.MonthNr

26

Có lý do gì khiến bạn không sử dụng biến bảng và toán tử SUM tổng hợp, thay vì con trỏ không? SQL vượt trội trong các hoạt động hướng tập hợp. 99,87% thời gian bạn thấy mình đang sử dụng con trỏ, có một giải pháp thay thế theo định hướng thiết lập hiệu quả hơn:

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

12
Rõ ràng trong MSSQL2012, giờ đây bạn có thể chèn ở định dạng này: VALUES (1, 'January', 100.00), (2, 'January', 200.00) - source: blog.sqlauthority.com/2012/10/27/…
andrewb

3
Tính năng này hoàn toàn thoát khỏi thông báo của tôi; rõ ràng nó cũng hoạt động trong SQL 2008.
Paul Smith

8

T-SQL không hỗ trợ các mảng mà tôi biết.

Cấu trúc bảng của bạn là gì? Bạn có thể thiết kế một truy vấn thực hiện điều này thay thế:

select
month,
sum(sales)
from sales_table
group by month
order by month

Chỉ là một nhận xét bên lề, tôi sẽ lưu ý rằng cú pháp T [n] .v ngắn gọn hơn một chút so với (chọn v từ T trong đó Ti = n). Trên thực tế, nó ngắn gọn hơn rất nhiều. Tôi khá muốn thấy T-SQL thêm nó.
debater

3

Câu hỏi hay và ý tưởng tuyệt vời, nhưng trong SQL, bạn sẽ cần thực hiện điều này:

Đối với kiểu dữ liệu datetime, một cái gì đó như thế này-

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

Nếu tất cả những gì bạn thực sự muốn là những con số, hãy làm điều này-

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
Khi sử dụng trang web của chúng tôi, bạn xác nhận rằng bạn đã đọc và hiểu Chính sách cookieChính sách bảo mật của chúng tôi.
Licensed under cc by-sa 3.0 with attribution required.