Search code examples
sql-servert-sqlstored-proceduressql-server-2005-express

How to make a sql procedure?


Can you give me some tips of how and IF I can make some stored procedures out of this,please?

-- Declarare variabile
Declare @p1 float,@p2 float,@p3 float, @p4 float, @CostFix  float,@CostVar float,@suma float;
select @p1=sum(Cantitate)from Productie_iulie_2014 where id_r='1';
select @p2=sum(Cantitate)from Productie_iulie_2014 where id_r='2';
select @p3=sum(Cantitate)from Productie_iulie_2014 where id_r='3';
select @p4=sum(Cantitate)from Productie_iulie_2014 where id_r='4';
Set @CostFix='50000';
Set @CostVar='1';
Select @suma= Sum(Cost)from Intrari_Materii;

--Costul total de productie a fiecarui produs 
Select Produse.Nume,Case 
When Pret.Id_r='1' then (PretNet*@p1)
When Pret.Id_r='2' Then (PretNet*@p2)
When Pret.Id_r='3' Then (PretNet*@p3)
When Pret.Id_r='4' Then (PretNet*@p4)
End as 'Cost productie'
from Pret
left join Produse on Pret.Id_r=Produse.Id_r

--Costul Total de productie la sfarsitul lunii
Select sum(Case 
When Pret.Id_r='1' then (PretNet*@p1)
When Pret.Id_r='2' Then (PretNet*@p2)
When Pret.Id_r='3' Then (PretNet*@p3)
When Pret.Id_r='4' Then (PretNet*@p4)
End )as 'Cost productie general'
from Pret
left join Produse on Pret.Id_r=Produse.Id_r

--Ce cantitate s-a vandut din fiecare produs si la ce pret 
Select Produse.Nume, Pret.PretBrut, SUM(Iesiri_Factura.Cantitate*Ambalare.Total_kilograme)
 as 'Vanzare Total Per Produs(kilograme)' from Iesiri_Factura
left join Ambalare on Iesiri_Factura.Id_r=Ambalare.Id_r
left join Produse on Ambalare.Id_r=Produse.Id_r
left join Pret on Produse.Id_r=Pret.Id_r
group by Pret.PretBrut,Produse.Nume,Produse.Id_r,Pret.Id_r

--Incasari totale/luna
Declare @Sibiana float,@Bitter float,@bomboane float,@Praline float;
Select @Sibiana=SUM(Iesiri_Factura.Cantitate*Ambalare.Total_kilograme)*Pret.PretBrut
from Iesiri_Factura
left join Ambalare on Iesiri_Factura.Id_r=Ambalare.Id_r
left join Produse on Ambalare.Id_r=Produse.Id_r
left join Pret on Produse.Id_r=Pret.Id_r
where Produse.Id_r='1'
group by Pret.PretBrut;
Select @Bitter=SUM(Iesiri_Factura.Cantitate*Ambalare.Total_kilograme)*Pret.PretBrut
from Iesiri_Factura
left join Ambalare on Iesiri_Factura.Id_r=Ambalare.Id_r
left join Produse on Ambalare.Id_r=Produse.Id_r
left join Pret on Produse.Id_r=Pret.Id_r
where Produse.Id_r='2'
group by Pret.PretBrut;
Select @bomboane=SUM(Iesiri_Factura.Cantitate*Ambalare.Total_kilograme)*Pret.PretBrut
from Iesiri_Factura
left join Ambalare on Iesiri_Factura.Id_r=Ambalare.Id_r
left join Produse on Ambalare.Id_r=Produse.Id_r
left join Pret on Produse.Id_r=Pret.Id_r
where Produse.Id_r='3'
group by Pret.PretBrut;

Select @Praline=SUM(Iesiri_Factura.Cantitate*Ambalare.Total_kilograme)*Pret.PretBrut
from Iesiri_Factura
left join Ambalare on Iesiri_Factura.Id_r=Ambalare.Id_r
left join Produse on Ambalare.Id_r=Produse.Id_r
left join Pret on Produse.Id_r=Pret.Id_r
where Produse.Id_r='4'
group by Pret.PretBrut;

select distinct @Sibiana as 'Incasari Sibiana',@Bitter as 'Incasari Bitter',
@bomboane as'Incasari Bomboane Craciun',@Praline as 'Incasari Praline',
(@Sibiana+@Bitter+@bomboane+@Praline)as'Incasari totale/luna'
 from Produse;

--Cantitatea totala de ciocolata vanduta 
Select SUM(Iesiri_Factura.Cantitate*Ambalare.Total_kilograme)
 as 'Cantitate vanduta(kg)' from Iesiri_Factura
left join Ambalare on Iesiri_Factura.Id_r=Ambalare.Id_r
left join Produse on Ambalare.Id_r=Produse.Id_R

--Stocul,ce marfa a ramas la sfarsitul lunii dupa productie-vanzare
Select Produse.Nume,
case    when Produse.Id_r='1' then @p1
        when Produse.Id_r='2' then @p2
        when Produse.Id_r='3' then @p3
        when Produse.Id_r='4' then @p4
end -(sum(Iesiri_Factura.Cantitate*Ambalare.Total_kilograme)) as Stoc   
from Produse
left join Ambalare on  Produse.Id_r=Ambalare.Id_r
left join Iesiri_Factura on Ambalare.Id_r=Iesiri_Factura.Id_r
Group by Produse.Nume,Produse.Id_r

--Cate Kg s-au produs in total luna respectiva si Cheltuielile survenite la furnizor
--Cheltuielile generale la sfarsitul lunii(Costul fix+Costul variabil+cheltuieli furnizor)
Select distinct (@p1+@p2+@p3+@p4)as 'Productie Totala(Kg)',
@suma as 'Cheltuieli Materii/luna',
(@p1+@p2+@p3+@p4)*@CostVar +@suma+@CostFix as 'Cheltuieli generale/luna' 
from Produse left join Pret on Produse.Id_r=Pret.Id_r
left join Retete on Pret.Id_r=Retete.Id_r
left join Intrari_Materii on Retete.Id_m=Intrari_Materii.Id_m 

--Profitul firmei la sfarsitul lunii
Declare @x float,@y float;
Select @x=sum(Case 
When Pret.Id_r='1' then (PretNet*@p1)
When Pret.Id_r='2' Then (PretNet*@p2)
When Pret.Id_r='3' Then (PretNet*@p3)
When Pret.Id_r='4' Then (PretNet*@p4)
End )
from Pret
left join Produse on Pret.Id_r=Produse.Id_r;

Set @y=(@p1+@p2+@p3+@p4)*@CostVar +@suma+@CostFix;

select distinct(@x+@y)as 'Cheltuieli totale/luna'
from Iesiri_Factura
left join Ambalare on Iesiri_Factura.Id_r=Ambalare.Id_r
left join Produse on Ambalare.Id_r=Produse.Id_r
left join Pret on Produse.Id_r=Pret.Id_r

Select distinct(@Sibiana+@Bitter+@bomboane+@Praline)-(@x+@y) as'Profit/luna' from Iesiri_Factura

Solution

  • Put

    create proc myproc
    as
    begin
    

    at the start, and

    end
    

    at the end.