SPetty1979
Technical User
Here is the error that I am getting:
Msg 137, Level 15, State 2, Line 16
Must declare the scalar variable "@animal_colors".
Msg 137, Level 15, State 2, Line 22
Must declare the scalar variable "@animal_colors".
Here is my SQL Statement:
go
declare @animal_colors table (color varchar(50));
declare @i int;
declare @row_count int;
INSERT @animal_colors select distinct description from color
set @i = 0
set @row_count = (select count(color) from @animal_colors)
while @i < @row_count
begin
update animal
set color =
(select top 1 color
from color
where color.description = @animal_colors.color
)
from color
inner join animal on animal.color = color.color
where color.description = @animal_colors.color[@i]
delete from color where color not in (select top 1 color from color
where description = @animal_colors.color[@i]
and description = @animal_colors.color[@i]
set @i = @i +1
end
I'm trying to learn how to write these, and I think I'm pretty close, but can't quite figure out what I am missing.
Thanks
Shawn
Msg 137, Level 15, State 2, Line 16
Must declare the scalar variable "@animal_colors".
Msg 137, Level 15, State 2, Line 22
Must declare the scalar variable "@animal_colors".
Here is my SQL Statement:
go
declare @animal_colors table (color varchar(50));
declare @i int;
declare @row_count int;
INSERT @animal_colors select distinct description from color
set @i = 0
set @row_count = (select count(color) from @animal_colors)
while @i < @row_count
begin
update animal
set color =
(select top 1 color
from color
where color.description = @animal_colors.color
)
from color
inner join animal on animal.color = color.color
where color.description = @animal_colors.color[@i]
delete from color where color not in (select top 1 color from color
where description = @animal_colors.color[@i]
and description = @animal_colors.color[@i]
set @i = @i +1
end
I'm trying to learn how to write these, and I think I'm pretty close, but can't quite figure out what I am missing.
Thanks
Shawn