Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Rhinorhino on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

sql puzzle: (yelworcm 's UDF) 2

Status
Not open for further replies.

maswien

Technical User
Joined
Sep 24, 2003
Messages
1,286
Location
CA

I think this a interesting question and pretty much buried in the original thread. It's posted by yelworcm:

Code:
What function returns 0, if called first thing after connecting to SQL Server, but never returns 0 after that initial call.

I can think of a way using procedure to achieve it, better let someone else change it to a UDF, following is my procedure

Code:
CREATE procedure getst
@cnt int output
AS
BEGIN
set nocount on 
if not exists(select * from tempdb..sysobjects where name like '##ses%')
 create table ##ses(ses_id binary(128), cnt int)  

declare @ses_id as binary(128)
set @ses_id = ( select context_info from sysprocesses where spid = @@spid)

update ##ses set cnt = cnt + 1 
   where ses_id = @ses_id  
if (@@rowcount = 0)
insert into ##ses values(@ses_id, 0 )
 
select @cnt = cnt from ##ses

set nocount off
END

to test it:

Code:
declare @test int
exec getst @test output
select @test
 
Credit still goes to an unknown (actually unremembered) poster from DBForums.com. In other words, I had to look this up in BOL. So there is a hint for you, the function is listed in BOL.
 
In other words: this is built-in function that returns 0 only once - and only when called first in session?

------
"There's a man... He's bald and wears a short-sleeved shirt, and somehow he's very important to me. I think his name is Homer."
(Jack O'Neill, Stargate)
[banghead]
 

I assume this is a SQL puzzle, so someone need write a UDF, if it's a builtin function, then I would like to say it's a BOL puzzle ;)
 
select @@rowcount will return 0 only if you call it as the first thing when establishing a new connection. After that it will always return a value greater than 0.

 
ooooops. I shall "yield" my start back. I'll go back to the drawing board.
 
Actually, @@rowcount, and its brother rowcount_big() was the function that I had in mind. If you simply call @@rowcount, you get 1 row returned. I don't think the original poster of the question thought of the possiblity of calling it in a more "complex" query than select @@rowcount.
 
[banghead]

set datefirst 7
select @@rowcount

Same thing. [wink]

------
"There's a man... He's bald and wears a short-sleeved shirt, and somehow he's very important to me. I think his name is Homer."
(Jack O'Neill, Stargate)
[banghead]
 

vongrunt ans SQLDennis point to the same issue of the function, we need to find a way : no matter what happened, as long as the user stay in the connection, this function won't return 0.
 
Thanks for posting back yelworcm. I was going crazy trying to find a function that did as the question asked, and this was the closest I could find (excluding functions that were in regard to server restarting). At least I can go to sleep without feeling guilty that Denis had yielded me a star. :)
 
> I was going crazy trying to find a function that did as the question asked, and this was the closest I could find

... and I almost decompiled bloody BOL .chm files [smile]

------
"There's a man... He's bald and wears a short-sleeved shirt, and somehow he's very important to me. I think his name is Homer."
(Jack O'Neill, Stargate)
[banghead]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top