Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
| Author |
Topic |
|
psfaro
Starting Member
49 Posts |
Posted - 2010-10-07 : 08:02:42
|
| Hi,The folowing T-SQL code:--create Temp Table with 2 more fieldsselect *,acumulado=0, row_number() over (order by orderId) as recno into #TEMP from [Order Details]--varsDECLARE @nregs INT DECLARE @rec INT DECLARE @tot INTDECLARE @qt INT --initializeset @tot=0set @rec=0set @nregs= (select COUNT(*) from #TEMP)--loopWHILE @rec <= @nregsBEGIN set @qt= (select quantity from #temp WHERE recno=@rec) set @tot= @tot + @qt UPDATE #TEMP SET acumulado=@tot WHERE recno=@rec set @rec=@rec + 1 END select * from #TEMPdrop table #TEMP-----------------------------Returns:Cannot insert the value NULL into column 'acumulado', table 'tempdb.dbo.#TEMPNOTES:if i change:set @tot= @qt works Fine, what means that @qt doesn't have any NULL value.[Order Details] is the Table of Nortwind DatabaseSQLSERVER R2Regards Pedro |
|
|
pk_bohra
Master Smack Fu Yak Hacker
1182 Posts |
Posted - 2010-10-07 : 08:19:43
|
quote: Originally posted by psfaro Hi,The folowing T-SQL code:--create Temp Table with 2 more fieldsselect *,acumulado=0, row_number() over (order by orderId) as recno into #TEMP from [Order Details]--varsDECLARE @nregs INT DECLARE @rec INT DECLARE @tot INTDECLARE @qt INT --initializeset @tot=0set @rec=0set @nregs= (select COUNT(*) from #TEMP)--loopWHILE @rec <= @nregsBEGIN set @qt= (select quantity from #temp WHERE recno=@rec) set @tot= @tot + @qt UPDATE #TEMP SET acumulado=@tot WHERE recno=@rec set @rec=@rec + 1 END select * from #TEMPdrop table #TEMP-----------------------------Returns:Cannot insert the value NULL into column 'acumulado', table 'tempdb.dbo.#TEMPNOTES:if i change:set @tot= @qt works Fine, what means that @qt doesn't have any NULL value.[Order Details] is the Table of Nortwind DatabaseSQLSERVER R2Regards Pedro
Please correct me if my understanding is wrong.WHILE @rec <= @nregs --You are starting loop with 0 instead of one.set @qt= (select quantity from #tempWHERE recno=@rec) --This will return null .set @tot= @tot + @qt -- Any concatenation will null returns nullUPDATE #TEMP SET acumulado=@tot --@tot have null so updation failsWHERE recno=@recWhy you are using while loop for aggregation. |
 |
|
|
psfaro
Starting Member
49 Posts |
Posted - 2010-10-07 : 09:25:34
|
| Hi pk_bohraYou absoluty Right !! Thank'sIf i change :set @rec=1 works fine.Still not understand why even in this situation set @rec=0set @tot= @qt //must return NULLdoesn't return any error and "acumulado" column have the same value as Quantity Column,This was my confusion that i forgot to initialize @rec=1Regards Pedro |
 |
|
|
pk_bohra
Master Smack Fu Yak Hacker
1182 Posts |
Posted - 2010-10-07 : 13:58:55
|
Hi Pedro,Happy to hear that explanation worked for you.Regarding the query quote: set @tot= @qt //must return NULL
.I have never come across such strange behavior of SQL.At this point I don't have answer for your query.May be some senior member knows the answer for it.Regards,BohraI am here to learn from Masters and help new bees in learning. |
 |
|
|
|
|
|
|
|