Sunday, May 18, 2008

stored procedures

Stored Procedures are used to grouping works that on a server..
Also ms sql server has built-in stored procedures
we may need to create our user specified procedures ..
here are some code snippets....


create procedure usp_BugunkuSiparisler
as
select k.isim +' '+ k.Soyad,k.email,s.*
from tblSiparis S JOIN tblKullanici k
on s.kullaniciKod=k.kullaniciKod
where siparisTarih < GetDate()-1 and siparisTarih > GetDate()+1
GO

stored procedure with parameters

create procedure uspSepeteAt
(@kullaniciKod int=null,
@urunKod int =null,
@adet int =1)
as
set nocount on
if @kullaniciKod is null or @urunKod is null
return 0
else if exists(select * from tblSepet where
kullaniciKod=@kullaniciKod and urunKod=@urunKod)
update tblSepet
set adet=@adet+adet
where kullaniciKod=@kullaniciKod and urunKod=@urunKod
else
insert into tblSepet
values(@kullaniciKod,@urunKod,@adet)
set nocount off
go

procedures with outputs
create procedure uspOrtalayici
(@sayi1 int,
@sayi2 int,
@sayiOrt decimal(18,2) OUTPUT)
as
set nocount on
select @sayiOrt=(@sayi1+@sayi2)/cast(2 as decimal)
set nocount off
go

No comments: