SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS ON 
GO

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[cs_referrals_Add]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[cs_referrals_Add]
GO

CREATE Proc [dbo].cs_referrals_Add
(
	@UrlID int,
	@Url nvarchar(512),
	@SettingsID int,
	@SectionID int,
	@PostID int	
)

as
SET Transaction Isolation Level Read UNCOMMITTED

exec cs_Urls_Add @UrlID, @Url

Declare @ReferralID int

Select @ReferralID = ReferralID FROM cs_Referrals where SettingsID = @SettingsID and SectionID = @SectionID and UrlID = @UrlID

if(@ReferralID is null)
Begin
	Insert cs_Referrals (SettingsID, SectionID, PostID, UrlID, Hits, LastDate)
	Values (@SettingsID, @SectionID, @PostID, @UrlID, 1, getdate())
End
Else
Begin
	Update cs_Referrals
	Set 
		Hits = Hits + 1,
		LastDate = getdate()
	Where ReferralID = @ReferralID
End



GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

grant execute on [dbo].[cs_referrals_Add] to public
go