Download counter (Stored Procedure)

By Jamsi • Jan 17th, 2006 • Category: SQL

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

No related posts.


FREE NEWSLETTER -> Want Tech Tips Sent Straight to your Inbox?

Grab our Newsletter to Ensure your PC is Running Smooth!

Jamsi is currently studying a full time Bachelor of Computer/Business degree and working part time as an Internet Security Consultant.
Email this author | All posts by Jamsi

Leave a Reply