Rineau
asked on
Using the "IF EXISTS" conditional statement.
I'm trying to gather some user statistics based on 3 conditions. First I want to check if the referring querystring is already in the database. If not insert it into the db. Second, if the querystring is already in the database, then check if the ip-address of the user is already in the database. If it is, then check if the ip address was inserted today. If not, update the "refCountIn" field with +1. The problem lies in the third condition where we check if the ip-address was inserted today and if false, update the "refCountIn" field with +1 and if true, do nothing.Below is the code I have until now:
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
' *** Declare the variables
Dim getStatCmd As String
Dim updStatCmd As String
Dim myRef As String = Request.QueryString("ref")
Dim myQueryString As String = Request.ServerVariables("Q UERY_STRIN G").Replac e("ref=", "")
Dim myRemoteAddr As String = Request.ServerVariables("R EMOTE_ADDR ")
Dim myHttpReferer As String = Request.ServerVariables("H TTP_REFERE R")
Dim dtNow As Date = DateTime.Now
Dim dtToday As Date = DateTime.Today
' *** Conditional INSERT command
getStatCmd = _
"IF EXISTS(SELECT 'True' FROM tblReferers WHERE robotName = '" & myQueryString & "' AND refIpAddress = '" & myRemoteAddr & "') " & _
"BEGIN " & _
"SELECT 'This clickin has already been recorded!'" & _
"END ELSE BEGIN " & _
"SELECT 'Clickin recorded' " & _
"INSERT INTO tblReferers(robotName, refIpAddress, refReferer, refTime) " & _
"VALUES(" & _
"'" + myQueryString + "'," & _
"'" + myRemoteAddr + "'," & _
"'" + myHttpReferer + "'," & _
"'" + dtToday + "')" & _
"END "
' *** Conditional UPDATE command
updStatCmd = _
"IF EXISTS(SELECT 'True' FROM tblReferers WHERE robotName = '" & myQueryString & "' AND refIpAddress = '" & myRemoteAddr & "' AND refTime > '" & dtToday & "') " & _
"UPDATE tblReferers " & _
"SET refCountIn = refCountIn + 1, refTime = '" & dtToday & "' " & _
"WHERE refIpAddress = '" & myRemoteAddr & "' AND robotName = '" & myRef & "'"
Dim insConnCmd As New SqlCommand(getStatCmd, New SqlConnection(connSD))
Dim updConnCmd As New SqlCommand(updStatCmd, New SqlConnection(connSD))
insConnCmd.Connection.Open ()
insConnCmd.ExecuteNonQuery ()
insConnCmd.Connection.Clos e()
updConnCmd.Connection.Open ()
updConnCmd.ExecuteNonQuery ()
updConnCmd.Connection.Clos e()
End Sub
Anyone with an idea on how to solve this one? I think I need to write a subquery for the third condition, but I don't have a clue on how to handle this.Thanks in advance for your help!
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
' *** Declare the variables
Dim getStatCmd As String
Dim updStatCmd As String
Dim myRef As String = Request.QueryString("ref")
Dim myQueryString As String = Request.ServerVariables("Q
Dim myRemoteAddr As String = Request.ServerVariables("R
Dim myHttpReferer As String = Request.ServerVariables("H
Dim dtNow As Date = DateTime.Now
Dim dtToday As Date = DateTime.Today
' *** Conditional INSERT command
getStatCmd = _
"IF EXISTS(SELECT 'True' FROM tblReferers WHERE robotName = '" & myQueryString & "' AND refIpAddress = '" & myRemoteAddr & "') " & _
"BEGIN " & _
"SELECT 'This clickin has already been recorded!'" & _
"END ELSE BEGIN " & _
"SELECT 'Clickin recorded' " & _
"INSERT INTO tblReferers(robotName, refIpAddress, refReferer, refTime) " & _
"VALUES(" & _
"'" + myQueryString + "'," & _
"'" + myRemoteAddr + "'," & _
"'" + myHttpReferer + "'," & _
"'" + dtToday + "')" & _
"END "
' *** Conditional UPDATE command
updStatCmd = _
"IF EXISTS(SELECT 'True' FROM tblReferers WHERE robotName = '" & myQueryString & "' AND refIpAddress = '" & myRemoteAddr & "' AND refTime > '" & dtToday & "') " & _
"UPDATE tblReferers " & _
"SET refCountIn = refCountIn + 1, refTime = '" & dtToday & "' " & _
"WHERE refIpAddress = '" & myRemoteAddr & "' AND robotName = '" & myRef & "'"
Dim insConnCmd As New SqlCommand(getStatCmd, New SqlConnection(connSD))
Dim updConnCmd As New SqlCommand(updStatCmd, New SqlConnection(connSD))
insConnCmd.Connection.Open
insConnCmd.ExecuteNonQuery
insConnCmd.Connection.Clos
updConnCmd.Connection.Open
updConnCmd.ExecuteNonQuery
updConnCmd.Connection.Clos
End Sub
Anyone with an idea on how to solve this one? I think I need to write a subquery for the third condition, but I don't have a clue on how to handle this.Thanks in advance for your help!
I suggest you use encapsulate your SQL code in stored procedures instead of leaving it in the API code. It will make it more readable and debuggable...
what you have will not work as an inline sql statement. Create a stored procedure like Racimo suggested
ASKER
Thanks guys!
I have just written my very first stored procedure :) Is this correct?
ALTER PROCEDURE sdstats.spGetReferers
(
@myQueryString varchar(50),
@myRemoteAddr varchar(12),
@myHttpReferer varchar(155),
@dtToday datetime
)
AS
BEGIN
SET NOCOUNT ON
IF EXISTS(SELECT 'True' FROM tblReferers WHERE robotName = @myQueryString AND refIpAddress = @myRemoteAddr)
BEGIN
SELECT 'This clickin has already been recorded!'
END ELSE BEGIN
SELECT 'Clickin recorded'
INSERT INTO tblReferers(robotName, refIpAddress, refReferer, refTime)
VALUES(@myQueryString, @myRemoteAddr, @myHttpReferer, @dtToday)
END
IF EXISTS(SELECT 'True' FROM tblReferers WHERE robotName = @myQueryString AND refIpAddress = @myRemoteAddr AND refTime > @dtToday)
UPDATE tblReferers SET refCountIn = refCountIn + 1, refTime = @dtToday
WHERE refIpAddress = @myRemoteAddr AND robotName = @myQueryString
END
RETURN
How do I pass data/execute the stored procedure from the codebehind? Any good tutorials or something?
I have just written my very first stored procedure :) Is this correct?
ALTER PROCEDURE sdstats.spGetReferers
(
@myQueryString varchar(50),
@myRemoteAddr varchar(12),
@myHttpReferer varchar(155),
@dtToday datetime
)
AS
BEGIN
SET NOCOUNT ON
IF EXISTS(SELECT 'True' FROM tblReferers WHERE robotName = @myQueryString AND refIpAddress = @myRemoteAddr)
BEGIN
SELECT 'This clickin has already been recorded!'
END ELSE BEGIN
SELECT 'Clickin recorded'
INSERT INTO tblReferers(robotName, refIpAddress, refReferer, refTime)
VALUES(@myQueryString, @myRemoteAddr, @myHttpReferer, @dtToday)
END
IF EXISTS(SELECT 'True' FROM tblReferers WHERE robotName = @myQueryString AND refIpAddress = @myRemoteAddr AND refTime > @dtToday)
UPDATE tblReferers SET refCountIn = refCountIn + 1, refTime = @dtToday
WHERE refIpAddress = @myRemoteAddr AND robotName = @myQueryString
END
RETURN
How do I pass data/execute the stored procedure from the codebehind? Any good tutorials or something?
I suggest you use following style in coding...(sp are very practical. You may either return an output parameter or a dataset or both:))
ALTER PROCEDURE sdstats.spGetReferers
(
@myQueryString varchar(50),
@myRemoteAddr varchar(12),
@myHttpReferer varchar(155),
@dtToday datetime,
@return_value int output
)
AS
BEGIN
SET NOCOUNT ON
IF NOT EXISTS(SELECT 1 FROM tblReferers WHERE robotName = @myQueryString AND refIpAddress = @myRemoteAddr)
BEGIN
set @return_value = 0
END
ELSE
BEGIN
INSERT INTO tblReferers(robotName, refIpAddress, refReferer, refTime)
VALUES(@myQueryString, @myRemoteAddr, @myHttpReferer, @dtToday)
set @return_value = 1
END
IF EXISTS (SELECT 1 FROM tblReferers WHERE robotName = @myQueryString AND refIpAddress = @myRemoteAddr AND refTime > @dtToday)
BEGIN
UPDATE tblReferers SET refCountIn = refCountIn + 1, refTime = @dtToday
WHERE refIpAddress = @myRemoteAddr AND robotName = @myQueryString
set @return_value
END
RETURN
ALTER PROCEDURE sdstats.spGetReferers
(
@myQueryString varchar(50),
@myRemoteAddr varchar(12),
@myHttpReferer varchar(155),
@dtToday datetime,
@return_value int output
)
AS
BEGIN
SET NOCOUNT ON
IF NOT EXISTS(SELECT 1 FROM tblReferers WHERE robotName = @myQueryString AND refIpAddress = @myRemoteAddr)
BEGIN
set @return_value = 0
END
ELSE
BEGIN
INSERT INTO tblReferers(robotName, refIpAddress, refReferer, refTime)
VALUES(@myQueryString, @myRemoteAddr, @myHttpReferer, @dtToday)
set @return_value = 1
END
IF EXISTS (SELECT 1 FROM tblReferers WHERE robotName = @myQueryString AND refIpAddress = @myRemoteAddr AND refTime > @dtToday)
BEGIN
UPDATE tblReferers SET refCountIn = refCountIn + 1, refTime = @dtToday
WHERE refIpAddress = @myRemoteAddr AND robotName = @myQueryString
set @return_value
END
RETURN
ASKER
Thank you Racimo, but there seems to be an error in the syntax of the stored procedure. It's in the update part of the code. Visual Studio returns the error: "Incorrect syntax near the keyword 'END'".
I also made a start with the code for exectuting the strored procedure. Does it appear to be correct?
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Dim sqlConnSD As New SqlConnection("connStrSD")
Dim trackCmd As New SqlCommand("trackReferer", sqlConnSD)
Dim myQueryString As String = Request.ServerVariables("Q UERY_STRIN G").Replac e("ref=", "")
Dim myRemoteAddr As String = Request.ServerVariables("R EMOTE_ADDR ")
Dim myHttpReferer As String = Request.ServerVariables("H TTP_REFERE R")
Dim dtToday As Date = DateTime.Today
trackCmd.CommandType = CommandType.StoredProcedur e
trackCmd.Parameters("@myQu eryString" ).Value = myQueryString
trackCmd.Parameters("@myRe moteAddr") .Value = myRemoteAddr
trackCmd.Parameters("@myHt tpReferer" ).Value = myHttpReferer
trackCmd.Parameters("@dtTo day").Valu e = dtToday
sqlConnSD.Open()
trackCmd.ExecuteNonQuery()
sqlConnSD.Close()
End Sub
ps: for what it's worth; I raised the number of points to 500
I also made a start with the code for exectuting the strored procedure. Does it appear to be correct?
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Dim sqlConnSD As New SqlConnection("connStrSD")
Dim trackCmd As New SqlCommand("trackReferer",
Dim myQueryString As String = Request.ServerVariables("Q
Dim myRemoteAddr As String = Request.ServerVariables("R
Dim myHttpReferer As String = Request.ServerVariables("H
Dim dtToday As Date = DateTime.Today
trackCmd.CommandType = CommandType.StoredProcedur
trackCmd.Parameters("@myQu
trackCmd.Parameters("@myRe
trackCmd.Parameters("@myHt
trackCmd.Parameters("@dtTo
sqlConnSD.Open()
trackCmd.ExecuteNonQuery()
sqlConnSD.Close()
End Sub
ps: for what it's worth; I raised the number of points to 500
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
For more info...on SP calls
SP calls from VB
http://support.microsoft.com/?scid=kb%3Ben-us%3B164485&x=15&y=16
http://www.developersdex.com/gurus/code/479.asp
SP calls from VB NET
http://sqlserveradvisor.com/dMSAccess001.nsf/776b0b4ca4d0c3a58825670400547583/165cc9cc85aa9a17f243d07ff9b90611?OpenDocument
SP calls from VB
http://support.microsoft.com/?scid=kb%3Ben-us%3B164485&x=15&y=16
http://www.developersdex.com/gurus/code/479.asp
SP calls from VB NET
http://sqlserveradvisor.com/dMSAccess001.nsf/776b0b4ca4d0c3a58825670400547583/165cc9cc85aa9a17f243d07ff9b90611?OpenDocument
ASKER
Thanks very much Racimo! Greatly appreciated!