0
declare @a int, @min date, @max date,@i int = 0, @columnname nvarchar(max),@b nvarchar(7) = 'aa'
select @min = min([Starting Date]) from [Quantities&Planning]
select @max = max([Finish Date]) from [Quantities&Planning]
set @a = DATEDIFF(DAY,@min,@max)
while @i < @a
begin
set @i = @i +1
set @b = str(@i)
set @columnname = 'alter table Test1 add ' + @b + ' decimal(18,2) NULL'
PRINT @columnname
exec (@columnname)
end
我的代碼無法按預期工作。基本上我想要創建一個新的列,如1,2,3,4,5 ...這裏是錯誤代碼:在while循環中向SQL Server表添加新列
警告:空值由集合或其他SET操作消除。 警告:空值由集合或其他SET操作消除。
Msg 2812,Level 16,State 62,Line 11
找不到存儲過程'alter table Test1 add decimal(18,2)NULL'。Msg 2812,Level 16,State 62,Line 11
找不到存儲過程'alter table Test1 add decimal(18,2)NULL'。Msg 2812,Level 16,State 62,Line 11
找不到存儲過程'alter table Test1 add decimal(18,2)NULL'。
現在拋出該異常語句之前:消息173,15級,狀態1,行 列「小數」的定義必須包括一個數據類型。 – Macukadam
@Macukadam變量'@ b'是**空**,表示您爲什麼會收到此錯誤。確保'@ b'變量中有一些名字。使用print來調試查詢 –
alter table Test1 add decimal(18,2)NULL Msg 173,Level 15,State 1,Line 1 'decimal'列的定義必須包含數據類型。 alter table Test1 add decimal(18,2)NULL Msg 173,Level 15,State 1,Line 1 'decimal'列的定義必須包含一個數據類型。 – Macukadam