CREATE TABLE [dbo].[Table_A](
[ID] [int] IDENTITY(1,1) NOT NULL,
[AREA] [varchar](50) NOT NULL,
[Year] [int] NOT NULL,
[Month] [int] NOT NULL,
[Factor] [float] NULL,
[Net] [float] NULL,
[Path] [varchar](255) NULL,
[Created] [smalldatetime] NULL,
[CreatedBy] [varchar](50) NOT NULL,
[LastModified] [varchar](50) NOT NULL,
[LastModifiedBy] [varchar](50) NOT NULL)
)
ALTER TABLE [dbo].[Table_A] ADD DEFAULT ((1.0)) FOR [Factor]
GO
ALTER TABLE [dbo].[Table_A] ADD DEFAULT (sysdatetime()) FOR [Created]
GO
ALTER TABLE [dbo].[Table_A] ADD DEFAULT (suser_name()) FOR [CreatedBy]
GO
ALTER TABLE [dbo].[Table_A] ADD DEFAULT (sysdatetime()) FOR [LastModified]
GO
ALTER TABLE [dbo].[Table_A] ADD DEFAULT (suser_name()) FOR [LastModifiedBy]
GO
我需要使用游标填充表格,其中游标应从 Declare Cur_AREA Cursor For 获取数据
Select Distinct Media From DIM_AREA
注意:DIM_AREA 由德克萨斯、达拉斯、芝加哥和纽瓦克组成。光标应填写 1990 年至 2020 年的数据
我的光标代码是::
Declare @Temp_Year int
Select @Temp_Year = MAX(Year)
While @Temp_Year < DATEPART(YEAR, GETDATE())
Begin
Declare Cur_Media Cursor
For
Select Distinct Media From DIM_AREA
Order by Media
open Cur_Media
Fetch Next From Cur_Media
While @@FETCH_STATUS = 0
Begin
Declare @Temp_Month int
Set @Temp_Month = 1
While @Temp_Month <= 12
Begin
Insert into Table_A (Media, Year, month)
Set @Temp_Month = @Temp_Month + 1
Set @Temp_Year = @Temp_Year + 1
end
end
Close Cur_Media
Deallocate Cur_Media
但我的光标无法正常工作:(