Create temporary table from cursor data

Asked

Viewed 273 times

0

I created a cursor that shows the date of sale and the total sold value of the day. Follow the code:

declare cursorteste cursor
local forward_only
for select data_venda, sum(valor_unitario) as valor_unitario from venda v, produtoVenda pv
where v.id_venda = pv.id_venda
group by data_venda

open cursorteste
declare @data_venda date, @total decimal(10,2)

fetch cursorteste into @data_venda, @total
    while @@FETCH_STATUS = 0
    begin
        select @data_venda as data_venda, @total as _total
        fetch next from cursorteste into @data_venda, @total
    end

close cursorteste
deallocate cursorteste

So far so good, the problem comes now..

Now from that cursor, I need to create a temporary table that has the date of sale, the total value sold on the day and the situation of the day. For example:

Minor - means that the current day’s sales were lower than the previous day’s sales

Normal - means that the current day’s sales were equal to the previous day’s sales

Greater - means that the current day’s sales were higher than the previous day’s sales

I know that to create temporary tables I need to use #table_temporaria. I’ve looked through several blogs and Microsoft documentation, and I couldn’t locate anything about it or I couldn’t express myself right about what I was looking for.

How do I create this temporary table from this cursor? How do I create this current day situation condition, based on the previous day?

If anyone can share with me. Thanks guys!!

1 answer

0

Oops. Talk man!

A while ago, I went through this. It’s not the best way, but you can try to adapt it here:

This Cursor question with Temporary Table

In my case, I needed to store cursors in a temporary table. I recommend you, see a possible structure of a PROCEDURE.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.