Advertisement

07.10.2008 at 10:38AM PDT, ID: 23554659
[x]
Attachment Details

how to call a function in stored procedure in sql server

Asked by rustypoot in SQL Server 2005

Tags: SQL Server

Hi, I have a Stored Proc that gives me All Active Employees for any given date range. I have a function that I need to include in the Stored Proc for user security. I need to enter the user name for function and it should just return the data from Stored Proc for that particular user. The Funtion is written so that if User 'A' runs the report, they should just see all Employees that belong to their Company only. So, the function should return employees from Stored Proc based on the Employee who runs the report and the companies that they have access to. The Function is as below:
set ANSI_NULLS ON
set QUOTED_IDENTIFIER ON
go




ALTER FUNCTION [dbo].[Freedom_GetDataAccess](@p_LoginName VARCHAR(50), @p_Field VARCHAR(100))
  RETURNS VARCHAR(8000)
AS
/************************************************************
   $Archive: $
   Source file: [Freedom_GetDataAccess].sql
 ************************************************************
   Author: Alan Kong
   Date: 07/07/2008
   Purpose:
   $ Revision: $
   Last modified on: 07/09/2008 10:19:24 PM  by: akong
 ************************************************************/
BEGIN
DECLARE @v_List VARCHAR(8000),
        @v_Value VARCHAR(64)

SET @v_List = ''

DECLARE c_List CURSOR FOR
      SELECT distinct
              sq.sqlvalue
      FROM   hrms_site..secusers u,
               hrms_site..secusercontext c,
               EmpComp ec,
                 SecGroups sgr,
               SecQualify sq
      WITH (NOLOCK)
      WHERE  
            u.sususername = @p_LoginName and left(u.susdefaultpwd,6) = ec.EecEmpNo
            and u.sususerid = c.sucuserid
            and c.sucusercontextid = sgr.sgrusercontextid
            and sgr.sgrGroupID = sq.sqlGroupID
            and sq.sqlField = @p_Field

OPEN c_List
FETCH NEXT FROM c_List INTO @v_Value

WHILE @@FETCH_STATUS <> -1
  BEGIN
  IF @@FETCH_STATUS <> -2
    BEGIN
    SET @v_List = @v_List + ',''' + RTRIM(@v_Value) + ''''
    END
  FETCH NEXT FROM c_List INTO @v_Value
  END

CLOSE c_List
DEALLOCATE c_List

RETURN substring(@v_List, 2, 8000)
END



Start Free Trial
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
65:
66:
67:
68:
69:
70:
71:
72:
73:
74:
75:
76:
77:
78:
79:
80:
81:
82:
83:
84:
85:
86:
87:
88:
89:
90:
91:
92:
93:
94:
95:
96:
97:
98:
99:
100:
101:
102:
103:
104:
105:
106:
107:
108:
109:
110:
111:
112:
113:
114:
115:
116:
117:
118:
119:
120:
121:
122:
123:
124:
125:
126:
127:
128:
129:
130:
131:
132:
133:
134:
135:
136:
137:
138:
139:
140:
141:
142:
set ANSI_NULLS ON
set QUOTED_IDENTIFIER ON
go
ALTER Procedure [dbo].[sproc_FTE_HeadCount]
(
@countdate datetime
)
AS
BEGIN
SET NOCOUNT ON
if OBJECT_ID('tempdb..#temp_emp_hist') is not null 
begin
	drop table #temp_emp_hist
end 
create table #temp_emp_hist
(
coid char(12),
eeid char(12),
emplstatus char(1),
jobeffdate datetime,
reasondesc varchar(25),
datetimecreated datetime
)
insert into #temp_emp_hist
select 	b.ejhcoid,b.ejheeid,b.ejhemplstatus,case b.ejhreason when 'TRO' then dateadd(d,-1,b.ejhjobeffdate) else b.ejhjobeffdate end as ejhreason,
		case b.ejhemplstatus when 'A' then c.jchdesc else d.tchdesc end as reasondesc, b.ejhdatetimecreated
  from 	emphjob b left outer join jobchrsn c
    on 	b.ejhreason = c.jchcode
		left outer join trmreasn d
    on 	b.ejhreason = d.tchcode    
where ((b.ejhemplstatus = 'A' and b.ejhreason in ('CONV','100','101','TRI','TRB')) or
		b.ejhemplstatus = 'T')
order by b.ejheeid,b.ejhjobeffdate
 
--Getting multiple associates in date order
if OBJECT_ID('tempdb..#temp_emp_hire_term') is not null 
begin
	drop table #temp_emp_hire_term
end
create table #temp_emp_hire_term
(
coid char(12),
eeid char(12),
dateoflasthire datetime,
dateoftermination datetime,
Activereasondesc varchar(25),
Termreasondesc varchar(25)
)
declare
@UPcoid char(12),
@UPeeid char(12),
@UPemplstatus char(1),
@UPjobeffdate datetime,
@UPreasondesc varchar(25),
@reccount int,
@Preeid char(12),
@Premplstatus char(1)
DECLARE emp_cursor CURSOR FOR 
select a.coid,a.eeid,a.emplstatus,a.jobeffdate,a.reasondesc from #temp_emp_hist a 
order by a.eeid,a.jobeffdate,a.datetimecreated
OPEN emp_cursor
FETCH NEXT FROM emp_cursor
INTO @UPcoid,@UPeeid,@UPemplstatus,@UPjobeffdate,@UPreasondesc
WHILE @@FETCH_STATUS = 0
BEGIN
	set @reccount = 1
	if @UPemplstatus = 'A'
		begin
		select @reccount = count(*) from #temp_emp_hire_term where coid = @UPcoid and eeid = @UPeeid and dateoflasthire = @UPjobeffdate 
		end	
		if @reccount = 0 
		begin
			if (@UPeeid = @Preeid and @UPemplstatus = 'A' and @Premplstatus = 'T') or (@UPeeid <> @Preeid)
				begin
				insert into #temp_emp_hire_term 
				(coid,eeid,dateoflasthire,dateoftermination,Activereasondesc)
					values
		(@UPcoid,@UPeeid,@UPjobeffdate,null,@UPreasondesc)
				end
		 end
	else
		begin
			if @UPeeid = @Preeid and @UPemplstatus = 'T' and @Premplstatus = 'A'
			begin
				update	#temp_emp_hire_term
				   set	dateoftermination = @UPjobeffdate,
						termreasondesc = @UPreasondesc
				 where	coid = @UPcoid and
						eeid = @UPeeid and
						dateoftermination is null
			end
		end 
	
	set @Preeid = @UPeeid
	set @Premplstatus = @UPemplstatus
				FETCH NEXT FROM emp_cursor
	INTO @UPcoid,@UPeeid,@UPemplstatus,@UPjobeffdate,@UPreasondesc
END
CLOSE emp_cursor
DEALLOCATE emp_cursor
select	e.cmpcompanyname,
		f.LocDesc,
		b.ejhorglvl2 + ' ' + g.OrgDesc,
		d.eecempno,
		(rtrim(c.eepnamelast) + ', '+rtrim(c.eepnamefirst)+ case when c.eepnamemiddle is null then '' else  ' '+rtrim(c.eepnamemiddle) end) as empname,
		b.ejhjobcode,
		b.ejhjobdesc,
		b.ejhFullTimeOrPartTime,
		case b.ejhFullTimeOrPartTime when 'F' then 1 when 'P' then 0.5 else 0 end as FTE,
		dateoflasthire, -- Included for testing only
		dateoftermination -- Included for testing only
  from	#temp_emp_hire_term a join 
		(select m.ejhcoid,m.ejheeid,m.ejhreason,m.ejhlocation,m.ejhemplstatus,m.ejhFullTimeOrPartTime,m.ejhOrgLvl2,m.ejhJobcode,m.ejhjobdesc
		   from emphjob m join
				(
				select ejhcoid,ejheeid,max(ejhdatetimecreated) as ejhdatetimecreated 
				from emphjob 
				where ejhemplstatus <> 'T' and ejhjobeffdate < dateadd(d,1,@countdate)
				group by ejhcoid,ejheeid
				) n
		on m.ejhcoid = n.ejhcoid and
		   m.ejheeid = n.ejheeid and
		   m.ejhdatetimecreated = n.ejhdatetimecreated 
		) b
	on	a.coid = b.ejhcoid and
		a.eeid = b.ejheeid
		join emppers c
	on	a.eeid = c.eepeeid
		join empcomp d
    on	a.coid = d.eeccoid and
		a.eeid = d.eeceeid
		join company e
    on	d.eeccoid = e.cmpcoid
		join location f
    on	d.eecLocation = f.loccode
		left outer join orglevel g
    on	b.ejhorglvl2 = g.orgcode and
		g.orglvl = 2
 where	((dateoflasthire <= @countdate and dateoftermination is null) or (@countdate between dateoflasthire and dateoftermination)) and
		d.eecempno not like 'T%'
order by 1,2,5 asc 
END
[+][-]07.11.2008 at 12:09AM PDT, ID: 21980452

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]07.11.2008 at 12:09AM PDT, ID: 21980455

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]07.11.2008 at 10:25AM PDT, ID: 21984288

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]07.14.2008 at 10:14AM PDT, ID: 22000036

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]07.14.2008 at 11:02AM PDT, ID: 22000422

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]07.14.2008 at 03:19PM PDT, ID: 22002635

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]07.14.2008 at 09:15PM PDT, ID: 22004095

View this solution now by starting your 7-day free trial. Setting up your free trial is quick, easy, and secure. We will return you to this solution, unlocked, when you're done.

 

About this solution

Zone: SQL Server 2005
Tags: SQL Server
Sign Up Now!
Solution Provided By: angelIII
Participating Experts: 1
Solution Grade: A
 
 
 
Loading Advertisement...
20080716-EE-VQP-32 / EE_QW_2_20070628