Thursday, November 13, 2008

Common Mistake In SQL

In the follwing sql codes, the @FieldValue didn’t get set if the current @Name doesn’t exist in the table PcsValues.

fetch next from curPCSValues into @Name

while @@fetch_status = 0
begin

select @FieldValue = v.Throughput
from PcsValues WHERE pcsName = @Name

fetch next from curPCSValues into @Name
end
The sql code should change to the following in order to make it work as expecting
fetch next from curPCSValues into @Name

while @@fetch_status = 0
begin
set @FieldValue = 0

select @FieldValue = v.Throughput
from PcsValues WHERE pcsName = @Name

fetch next from curPCSValues into @Name
end

0 comments: