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.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Using stored procedure with a table list

Author  Topic 

wielgeraats
Starting Member

3 Posts

Posted - 2013-03-28 : 07:20:30
I have to make a history table from stock mutations form articles of every day. I wrote a script which does everything well (creating a record with article numer, date and several summary fields) with a local variable called @mutationdate. So i made a stored procedure
with @mutationdate as variable.
Now the goal is that I have to create these records for every day that the stock has changed.
The table of available dates has been made by:
SELECT DISTINCT SM.MUTATIONDATE FROM STOCKMUTATION AS SM

I use sql 2005

James K
Master Smack Fu Yak Hacker

3873 Posts

Posted - 2013-03-28 : 08:25:38
Post the query you currently have. Hard to discern what you are trying to do without that.

Not related to the question you are asking, but simply out of curiosity - what is a stock mutation? Is it like a stock split or other corporate actions?
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2013-03-28 : 10:38:28
sounds like a cross join to me. cross join with query to create record for every value returned by it

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

wielgeraats
Starting Member

3 Posts

Posted - 2013-03-28 : 11:00:21
The stored procedure:

create procedure sp_bywerken_voorrcumdag @mutdatum datetime
as

set nocount on -- Uitzetten berichten

insert into VOORRCUMDAG
(product,
datum,
vvp,
voorraad_ve,
voorraad_kg,
voorraad_wrd,
gereserveerd_ve,
gereserveerd_kg,
gereserveerd_wrd,
levering_klant_ve,
levering_klant_kg,
levering_klant_wrd,
productie_verbruikt_ve,
productie_verbruikt_kg,
productie_verbruikt_wrd,
factregels,
omzet,
winst)
select
p.id,
@mutdatum,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0

from product as p
WHERE (P.Affiliate = '1fffdda0-a5dc-44f1-bdec-971c46a5c8d0')
AND (P.IsGroup = 0) and
(SELECT SUM(TS.Weight) FROM Stockmutation TS WHERE TS.Product = P.Id AND TS.Affiliate = '1fffdda0-a5dc-44f1-bdec-971c46a5c8d0' and
TS.modified < @mutdatum ) <> 0
group by p.id


update VOORRCUMDAG
set voorraad_ve =
isnull(( select convert(integer, sum(st.quantity)) from stockmutation as st
where st.product = VOORRCUMDAG.product
and VOORRCUMDAG.datum = @mutdatum
and st.modified < @mutdatum ),0)
where diverse_tabellen_views.dbo.VOORRCUMDAG.datum = @mutdatum


update diverse_tabellen_views.dbo.VOORRCUMDAG
set voorraad_kg =
isnull(( select convert(integer, sum(st.weight)) from stockmutation as st
where st.product = VOORRCUMDAG.product
and VOORRCUMDAG.datum = @mutdatum
and st.modified < @mutdatum ),0)
where VOORRCUMDAG.datum = @mutdatum


The stored procedure has more update statements in it but they have no additional value for the question.

In another table dates are defined. The storage procedure has to be called for every date.
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2013-03-28 : 11:19:54
oh...that would require calling sp in a cursor or loop structure. Is it possible for you to rewrite sp in such a way that it receives a list of dates instead?

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

wielgeraats
Starting Member

3 Posts

Posted - 2013-03-29 : 09:17:45
I have solved this problem by making a Visual Basic program, which reads all unique dates in a table and executes the stored procedure with the given date.
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2013-03-29 : 13:44:16
ok...cool

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page
   

- Advertisement -