Somewhat complicated SQL problem

Status
Not open for further replies.

russ-iha

Ars Praefectus
4,407
Subscriptor
OK, here's the deal. I am trying to write a stored procedure to handle this. It gets passed in a single ntext field that has a list of names to search for, separated by pipes.


ex: "user1|user2|user3|user4"


So the first task is to separate that out somehow.


The second task, which is really the kicker, is to return any of the names on the list that are found in the table.


ex:

Table has three rows, with field named "username".
Row1: user1
Row2: User2
Row3: User3

So the results of the query, using the example above would be a recordset with three rows, user1, user2, and user3. Notice that since user4 was not a value of the 'username' field in the table, it was not returned.

The deal is that it is a table of actively logged in users. Each user has a row in the table. Now I need to take this list of possible users and basically intersect it with the table of logged in users, and return only the matches (i.e. users that are actively logged in off the list given.)


Hints, suggestions, etC?
 

Aeirould

Ars Tribunus Angusticlavius
7,535
Subscriptor
This one's odd, but it'll work (ensure that you have | at the beginning and end as well as in the middle or partial names could come up):

Select UserName From tblWhatever Where @ntext Like '%|' + UserName + '|%'

BTW, why ntext? Is the list ever >8000 characters? Is Unicode ever used? I would suggest using a different type... I've only ever used this type of approach with VarChar, not sure if it matters.
 

russ-iha

Ars Praefectus
4,407
Subscriptor
well i could use a varchar... I just didn't wanna put any hard limits on what the size of the list of names could be.


Aeirould, I too am not sure if that will work. Assume that @next="|myfriend|sillyperson|user5|johnd|sally|" and only user5 and johnd have records in ACTIVE. The first row comparison is going to be

"|myfriend|sillyperson|user5|johnd|sally|" Like "%|user5|%"


I would appreciate it very much moneygrip ;)
 

russ-iha

Ars Praefectus
4,407
Subscriptor
Hey I wonder if I could do this, assuming I used nvarchar for @users:

code:<HR>
Code:
 SELECT Username 
 FROM WAITING
 WHERE Username IN (@users)

My code would have to do a quick Replace on it to change all the |'s to ,'s but it should work.

[edit] it didn't work, and I'm not sure why. I don't get any errors, but nothing is returned.

[This message was edited by russ-iha on February 17, 2002 at 13:05.]
 

moneygrip

Ars Tribunus Militum
2,658
Subscriptor++
This should do it, put this code in a stored procedure with the ntext field as an input variable (@text in this case).

Make sure the string includes the "|"'s only at the end of each user, i.e. "user1|user2|user3|."

code:<HR>
Code:
<P>create table #temp(userid nvarchar(255) null)<P>while @text <> ''
 
   begin
 
      insert into #temp
      select substring(@text, 1, patindex('%|%', @text)-1)
 
      set @text = substring(@text, patindex('%|%', @text)+1, len(@text))
 
   end
 
delete #temp
where userid not in (select user_id from userTable) 
 
select * from #temp

That should do it. The important part is to use substring() and patindex() to strip out each user, in case this doesn't do exactly what you want.

[This message was edited by moneygrip on February 17, 2002 at 14:03.]
 

russ-iha

Ars Praefectus
4,407
Subscriptor
I was talking with StoneTable in IRC and he came up with this:

code:<HR>
Code:
CREATE PROCEDURE GetWatched
	@users nvarchar(3000)
 AS<P>DECLARE @sql nvarchar(4000)
set @sql = "SELECT username FROM active WHERE username in (" + @users + ")"
EXEC SP_EXECUTESQL @sql<P>GO


Now the question is.... which query would be faster you think? In this case, the server program is doing the string manipulation and passing @users in. In your case, the database is doing it.

Comments?
 

indigo

Well-known member
5,606
Just build a logging table?

logging.time
logging.userID
logging.programID

Call a stored procedure on every page.

This way, not only can you tell how many users are logged on at once (by where time > (getdate() - 20)), you know what they're doing, and who they are...

The above is really simple, but adding Data1, Data2, etc is a good idea, because then you can store Form data, onclicks, stuff like that.

Yes this is alot more intensive... it shouldn't really matter unless you're running on a PII300 or something...
 

Aeirould

Ars Tribunus Angusticlavius
7,535
Subscriptor
What part of "i've used this" wasn't understood?

It works, it works fast, and it works without any subtables, subqueries or the like.

You are conditioned to use the Like keyword in a certain order, however, it doesn't care if the field is first or last and won't flip 'em for you.

Like returns a result if and when the mask on the right can be found on the left.
 

indigo

Well-known member
5,606
And?

Track keystrokes, log ons, log offs, Status changes, file xfers, file content...

A fully fledged logging program can be usefull...

I don't know why you'd want to use your db to track current sessions otherwise... wouldn't that sort of information be more easily gleaned from whatever server application you're writing to handle the app? If it's a web-based app then the logging is (IMO) superior, if it's not, why the db interaction?

Keep in mind I'm not trying to attack you. I've never writen an IM, so they could all be database driven for all I know... I'd be curious to find out what aspects are or are not db driven...

[Edit - Start]

Wait a sec... the server is tracking messages? So you already have logging... are you TimeStamping the messages? If so you already have your answer... you're using the db already... Have a table like this:

Users
Users.UserName
Users.UserId
Users.Status (binary 4)
Users.StatusTimeStamp

Have the client hit the Status whenever a message is sent, set the timeout to whatever.

Select UserName From Users Where Status = 0001 And DateDiff (mm, StatusTimeStamp, GetDate()) < 20)

If you're writing every msg to the db, and you're using ntext, it doesn't seem like speed is really a concern anyways?

[Edit - End]

[This message was edited by indigo on February 18, 2004 at 19:39.]

[Edit2 - Start]

Actually... nm that other table...

EventsTable
EventsTable.UserId
EventsTable.EventId
EventsTable.Data
EventsTable.TimeStamp

ReferencesTable
ReferencesTable.EventId
ReferencesTable.EventDescription

Insert into ReferencesTable EventId, EventDescription values (0001, 'Logoff')

Select Distinct UserId, TimeStamp From EventsTable

Blah blah... you know how it works...

[Edit2 - End]

[This message was edited by indigo on February 18, 2002 at 19:51.]
 
Status
Not open for further replies.