SQL Server Forums
Profile | Register | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Using stored procedure with a table list
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

wielgeraats
Starting Member

Netherlands
3 Posts

Posted - 03/28/2013 :  07:20:30  Show Profile  Reply with Quote
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
Flowing Fount of Yak Knowledge

3699 Posts

Posted - 03/28/2013 :  08:25:38  Show Profile  Reply with Quote
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

India
52317 Posts

Posted - 03/28/2013 :  10:38:28  Show Profile  Reply with Quote
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

Netherlands
3 Posts

Posted - 03/28/2013 :  11:00:21  Show Profile  Reply with Quote
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

India
52317 Posts

Posted - 03/28/2013 :  11:19:54  Show Profile  Reply with Quote
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

Netherlands
3 Posts

Posted - 03/29/2013 :  09:17:45  Show Profile  Reply with Quote
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

India
52317 Posts

Posted - 03/29/2013 :  13:44:16  Show Profile  Reply with Quote
ok...cool

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

Go to Top of Page
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.05 seconds. Powered By: Snitz Forums 2000