Download counter (Stored Procedure)

A stored procedure I used as a download script. You pass this procedure a parameter (filename), it checks the database if a record for this exists. If so it increments the record by one. If the record doesnt exist it creates it for you.

For this to work I used a table with two columns.

filename varchar(255)
hits numeric (9)


IF EXISTS (
	SELECT name FROM dbo.sysobjects
	WHERE name = 'updateStats'
	AND type = 'P'
	)DROP PROCEDURE dbo.[updateStats]
GO
	CREATE PROCEDURE updateStats
	(
		@filename varchar(25)
	)
AS
IF EXISTS(SELECT 'filename' FROM kav_dlstats WHERE filename = @filename)
BEGIN
	--If it exists, update the record!
	UPDATE kav_dlstats SET hits=hits+1 WHERE filename = @filename
END
ELSE
	BEGIN
	--Else lets make a new record
	INSERT into kav_dlstats(filename, hits) VALUES(@filename, '1')
END
Did you like this? Share it:

No related posts.



Leave a Reply

You must be logged in to post a comment.