Advertisement

06.09.2008 at 08:47AM PDT, ID: 23469476
[x]
Attachment Details

MSSQL :: Stored Procedure :: Select within loop??

Asked by trickyidiot in SQL Server 2005, MS SQL Server

Tags: SQL / MSSQL, MSSQL

I'm trying to set up a stored procedure to perform the following:
1: Perform a select query
2: Loop through results of select
3: for each result, perform a second query
4: based on result of second query, either do, or do not place result row from first query into a result array
5: return the result array

I am (painfully obvious) very green as far as stored procedures with MSSQL go. I have gotten as far as the below query (in attached code snippet), which pretty much has everything except the sub-query within the loop and the if statement based on the result of that second query.

Can someone help me with some direction here?

Thank you in advance for any insight you may be able to provide

Thanks
-=Patrick=-

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:
create type 
	T_REC 
	AS 
	object( 
		emp_code varchar, 
		location_code varchar, 
		demo_data_vchar varchar, 
		standard_hrs_per_day int, 
		hrs_date date, 
		seq_nbr int, 
		wbs_seq_nbr int, 
		reg_hrs int, 
		phase_code varchar, 
		demo_code varchar );
		
create type T_RECS as table of T_REC;
 
create function GetData( vcDate in varchar2 ) return T_RECS is
  Result T_RECS := T_RECS();
begin
  for rec in ( 
	SELECT 
		ei.emp_code, 
		ei.location_code, 
		edd.demo_data_vchar, 
		ei.standard_hrs_per_day, 
		etdd.hrs_date, 
		etdw.seq_nbr, 
		etdd.wbs_seq_nbr, 
		etdd.reg_hrs, 
		etdw.phase_code, 
		edd.demo_code 
	FROM 
		dbo.emp_info AS ei 
		INNER JOIN dbo.emp_demo_data AS edd ON ei.emp_code = edd.emp_code 
		INNER JOIN dbo.ei_tc_doc_detail AS etdd ON ei.emp_code = etdd.emp_code 
		INNER JOIN 
			dbo.ei_tc_doc_wbs AS etdw 
			ON etdd.emp_code = etdw.emp_code 
			AND etdd.per_end_date = etdw.per_end_date 
			AND etdd.wbs_seq_nbr = etdw.wbs_seq_nbr 
			AND etdd.line_nbr = etdw.line_nbr 
	WHERE 
		ei.emp_code = 'VARIABLE_ONE???' 
		AND ei.emp_status_ind = 'A' 
		AND etdd.hrs_date >= 'VARIABLE_TWO???' 
		AND etdd.hrs_date <= 'VARIABLE_THREE???' 
		AND etdd.ignore_ind = 'N' 
		AND edd.demo_code IN ('PTO') 
		AND etdw.phase_code IN ('SICK', 'VAC', 'BEREAV') 
		AND etdd.reg_hrs <> 0 
	ORDER BY 
		1,10,5,6,7,9;
	
	) loop
		-- do check of hours worked for vacation day
		-- if result of 2nd query is NULL, add results from 1st query into the array
    Result.extend;
    Result(Result.count).emp_code := rec.[emp_code];
    Result(Result.count).location_code := rec.[location_code];
		Result(Result.count).demo_data_vchar := rec.[demo_data_vchar];
		Result(Result.count).standard_hrs_per_day := rec.[standard_hrs_per_day];
		Result(Result.count).hrs_date := rec.[hrs_date];
		Result(Result.count).seq_nbr := rec.[seq_nbr];
		Result(Result.count).wbs_seq_nbr := rec.[wbs_seq_nbr];
		Result(Result.count).reg_hrs := rec.[reg_hrs];
		Result(Result.count).phase_code := rec.[phase_code];
		Result(Result.count).demo_code := rec.[demo_code];
  end loop;
  return Result;
end;
 
Loading Advertisement...
 
[+][-]06.09.2008 at 08:54AM PDT, ID: 21744195

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.

 
[+][-]06.09.2008 at 09:07AM PDT, ID: 21744293

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.

 
[+][-]06.09.2008 at 09:13AM PDT, ID: 21744330

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.

 
[+][-]06.09.2008 at 09:13AM PDT, ID: 21744336

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.

 
[+][-]06.09.2008 at 09:22AM PDT, ID: 21744412

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.

 
[+][-]06.09.2008 at 01:23PM PDT, ID: 21746330

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

Zones: SQL Server 2005, MS SQL Server
Tags: SQL / MSSQL, MSSQL
Sign Up Now!
Solution Provided By: angelIII
Participating Experts: 1
Solution Grade: A
 
 
[+][-]06.09.2008 at 02:03PM PDT, ID: 21746656

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.

 
[+][-]06.09.2008 at 02:10PM PDT, ID: 21746713

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.

 
[+][-]06.09.2008 at 02:22PM PDT, ID: 21746792

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.

 
 
Loading Advertisement...
20080716-EE-VQP-32 / EE_QW_2_20070628