Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Add user session

Author  Topic 

chrvik
Starting Member

7 Posts

Posted - 2012-11-16 : 05:07:33
How do I add a user session in a SQL statement?

Using Dreamweaver/Vb/Access.
Wanting to count how many times a user has made an entry to a forum and since the page is dynamic I have to do the count based on user session (id)

"sunitabeck" suggested I created a stored procedure with a parameter (such as @UserId) and then call that stored procedure from the client code, passing in the userId of the user who is viewing the page.

Only problem is I'm quite new to SQL. Thinking I have to call it in the WHERE clause, just not sure how.

nigelrivett
Master Smack Fu Yak Hacker

3385 Posts

Posted - 2012-11-16 : 06:19:23
The SP is called with the parameter and it returns the count.

The SP would be something like
create proc s_CountUserEntries
@UserID int ,
@count in out
as
select @count = count(*)
from ForumPosts
where UserID = @UserID
go

then call it with

declare @Count int
declare @UserID int
select @UserID = MyUserID ...
exec s_CountUserEntries @UserID = @UserID, @Count = @Count int out
select @Count


==========================================
Cursors are useful if you don't know sql.
SSIS can be used in a similar way.
Beer is not cold and it isn't fizzy.
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2012-11-16 : 22:32:29
quote:
Originally posted by nigelrivett

The SP is called with the parameter and it returns the count.

The SP would be something like
create proc s_CountUserEntries
@UserID int ,
@count int out
as
select @count = count(*)
from ForumPosts
where UserID = @UserID
go

then call it with

declare @Count int
declare @UserID int
select @UserID = MyUserID ...
exec s_CountUserEntries @UserID = @UserID, @Count = @Count int out
select @Count


==========================================
Cursors are useful if you don't know sql.
SSIS can be used in a similar way.
Beer is not cold and it isn't fizzy.


small typo fixed

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2012-11-16 : 22:33:48
also see various ways of returning values from procedures here

http://www.sqlteam.com/article/stored-procedures-returning-data

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

chrvik
Starting Member

7 Posts

Posted - 2012-11-17 : 10:39:12
Thank you both for responding!

I'm trying to twist my head around this but I can't get it to work.
Thought it might be easier. Dreamweaver is throwing all kinds of errors and that is probably because I've never created a SP before and used it later in a recordset.

What is the absolute easiest way to do this?
Have to start at the bottom on build my way up in order to understand.
Go to Top of Page
   

- Advertisement -