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 2008 Forums
 Transact-SQL (2008)
 How to avoid same function calls in SP?

Author  Topic 

nayanancha
Starting Member

27 Posts

Posted - 2012-03-16 : 10:27:19
Below is my stored proc

Its working properly. How can I optimize my sp. I want to avoid the 3 function calls to the same function which returns 3 columns.



CREATE TABLE #PageIndexForUsers
(
IndexId int IDENTITY (1, 1) NOT NULL,
UserName varchar(100),
Email varchar(100),
CompanyId varchar(100),
FullName varchar(100),
User bit,
RoleName varchar(100),
DivList varchar(200),
RegList varchar(200),
FacList varchar(200),
Logout bit,
URL varchar(200)
)

/*--------------------------------------- Insert records into Temp Table ------------------------------------------ */

declare @TempUsername varchar(100),
@TempDivList varchar(200),
@TempRegList varchar(200),
@TempFacList varchar(200),
@TempLogout varchar(20),
@TempURL varchar(200)

set @TempUsername = 'replace(uc.UserName,''' + @UserNamePrefix + ''' + ''_'','''')'
set @TempDivList = '(select DivList from ' + @DBPath + 'fn_GetLists(uc.userid))'
set @TempRegList = '(select RegList from ' + @DBPath + 'fn_GetLists(uc.userid))'
set @TempFacList = '(select FacList from ' + @DBPath + 'fn_GetLists(uc.userid))'
set @TempDeleteOnLogout = 'convert(bit,0)'
set @TempDirectAccessURL = 'dbo.fn_MakeUrl(' + convert(varchar(1),@modeBit) + '
,''' + @CompanyCode + ''', uc.UserId, ' + convert(varchar(1),@Logout) + ')'

set @sql =
'Insert Into #PageIndexForUsers(UserName,Email,CompanyId,FullName,User,RoleName,
DivList,RegList,FacList,Logout,URL)
select '+ @TempUsername +' as UserName,
m.Email,
c.CompanyId,
uc.FullName,
uc.User,
r.RoleName,
'+ @TempDivList +' as DivList,
'+ @TempRegList +' as RegList,
'+ @TempFacList +' as FacList,
'+ @TempLogout +' as Logout,
'+ @TempURL +' as URL
from ' + @Aspnet_Membership + ' m
inner join ' + @User_Config + ' uc
on uc.userid = m.userid
inner join ' + @Company + ' c
on c.CompanyCode = uc.CompanyCode
inner join ' + @Aspnet_UsersInRoles + ' ur
on ur.userid = uc.userid
inner join ' + @Aspnet_Roles + ' r
on r.RoleId = ur.RoleId
where c.CompanyCode = ''' + @CompanyCode + '''
'

If @UserName is NOT NULL
set @sql = @sql + ' and uc.UserName = ''' + @UserName + ''' order by uc.UserName '
Else If @RoleName IS NOT NULL
set @sql = @sql + ' and r.RoleName = ''' + @RoleName + ''' order by uc.UserName'
Else
set @sql = @sql + ' order by uc.UserName'


exec sp_executesql @sql



I want to avoid the below 3 statements, just want to call this function once. Because this functions returns 3 columns which I doing 3 times.

set @TempDivList = '(select DivList from ' + @DBPath + 'fn_GetLists(uc.userid))'
set @TempRegList = '(select RegList from ' + @DBPath + 'fn_GetLists(uc.userid))'
set @TempFacList = '(select FacList from ' + @DBPath + 'fn_GetLists(uc.userid))'

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2012-03-16 : 11:16:40
what does fn_GetLists(uc.userid) do? gives you delimited list?

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

Go to Top of Page

nayanancha
Starting Member

27 Posts

Posted - 2012-03-16 : 11:32:38
It returns 3 columns Divlist, RegList, FacList.

So I am using that function 3 times seperately by calling each of the columns in each function.

So my function remains the same, but my column is changing.

Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2012-03-16 : 11:34:41
why not do it inline without using udf call?

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

Go to Top of Page

nayanancha
Starting Member

27 Posts

Posted - 2012-03-16 : 12:13:53
It gives me a Delimited List for each of those columns. SO I had it in a UDF
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2012-03-16 : 12:20:45
you can do it inline using below approach

see scenario 3 below


http://visakhm.blogspot.com/2010/01/multipurpose-apply-operator.html

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

Go to Top of Page
   

- Advertisement -