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
 SQL Server 2000 Forums
 SQL Server Development (2000)
 Please interpret this SQL Server user defined function

Author  Topic 

AskSQLTeam
Ask SQLTeam Question

0 Posts

Posted - 2003-10-10 : 07:53:12
Asad Zaidi writes "Please can someone help me interpret the following user defined function:

--********************************************************
CREATE function getgvuser()
returns varchar(50)
as
begin
declare @uid varchar(50)
declare @uidvb varbinary(128)
declare @i int
declare @str varchar(100)

select @uidvb = context_info from master..sysprocesses where spid=@@spid

if(@uidvb = 0x00)
begin
set @uid='ADMIN' -- for debugging
end
else
begin
select @uid=convert(varchar(50),@uidvb)
set @str = ''
set @i = 1
while(ascii(substring(@uid,@i,1))!=0)
begin
set @str = @str + substring(@uid,@i,1)
set @i = @i+1
end

--select @uid=upper(replace(@uid,char(0),''))
set @uid=@str
end
return @uid
end
--************************************************************"

robvolk
Most Valuable Yak

15732 Posts

Posted - 2003-10-10 : 07:56:50
Well, I can interpret it as being written by someone who is not a database programmer. This version will also work:

CREATE FUNCTION getgvuser()
RETURNS varchar(50)
AS
DECLARE @uid varchar(50)
SELECT @uid=CAST(CASE WHEN context_info=0x00 THEN 'ADMIN' ELSE context_info END AS varchar(50))
FROM master..sysprocesses WHERE spid=@@spid
RETURN @uid
Go to Top of Page
   

- Advertisement -