Thursday, October 8, 2009

SQL-Stored Procedure(Example)

CREATE procedure [dbo].[sp_Example](@tablename varchar(30))
As /*BY Satheesh 14-09-2009 */
begin
DECLARE @userid int,@count int,@id int

DECLARE @Table_Cursor CURSOR
SET @Table_Cursor=CURSOR FAST_FORWARD FOR select distinct userid from memberblog
--close @Table_Cursor

open @Table_Cursor
fetch next from @Table_Cursor into @userid
while @@FETCH_STATUS = 0
BEGIN
select @count=Count(*) from memberblog where userid=@userid
select @id=isnull(min(id),0) from memberactivitycount where userid=@userid

if(@id>0)
begin
--update
update memberactivitycount set blog=@count where id=@id
end
else
begin
--insert
insert into memberactivitycount (userid, blog ) values (@userid,@count)
end
fetch next from @Table_Cursor into @userid
END
close @Table_Cursor
deallocate @Table_Cursor

/* Excecuting another sp --example is -->exec sp_sentmessage */
/* Excecuting a function from sp --example is -->if dbo.FunctionExm(@MemberID,@UserID)=1 */
end

No comments:

Post a Comment