Advertisement

05.27.2008 at 08:19AM PDT, ID: 23435122
[x]
Attachment Details

SQL Server 2005: How do you combine stored procedure results without using temp tables?

Asked by kyancy in SQL Server 2005, MS SQL Server

Tags: SQL

Hello. I currently have a stored procedure in SQL Server that returns a correlated set of lab test results. It pulls the needed data from a few different locations combining the results into a single result set. I implemented this functionality in a stored procedure using 'SELECT INTO' with temporary tables.

The stored procedure does exactly what I need, but because of its use of temporary tables and how SQL server performs locking in response to my use of temp tables, the procedure is adversely affecting other applications using the same database (some of these are also using stored procedures that are using temp tables).

Can anyone suggest other ways I could use to accomplish the same thing without the use of temp tables? I have been reading about other approaches but hoping someone on here can point me in the right direction based on their own experiences. Thx in advance!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:
SELECT e.enc_nbr, pt.med_rec_nbr, p.first_name, p.middle_name, p.last_name, l.ufo_num, l.order_num, l.create_timestamp AS l_create_timestamp, l.modify_timestamp AS l_modify_timestamp, r.create_timestamp AS r_create_timestamp, r.modify_timestamp AS r_modify_timestamp, l.enc_id,  
r.req_accession, r.prod_accession, r.obs_batt_id, r.test_desc, r.coll_date_time, r.unique_obr_num
         INTO #lab_tests_tmp  
                  FROM lab_nor l WITH(NOLOCK), lab_results_obr_p r WITH(NOLOCK) ,patient_encounter e WITH(NOLOCK),
patient pt WITH(NOLOCK),person p WITH(NOLOCK)
  WHERE l.order_num = r.ngn_order_num  
AND l.enc_id = e.enc_id
AND r.person_id = p.person_id
AND p.person_id = pt.person_id
  AND (l.delete_ind <> 'Y' OR l.delete_ind IS NULL)  
  AND (r.delete_ind <> 'Y' OR r.delete_ind IS NULL)
AND ((l.create_timestamp > @pi_date) OR 
(r.create_timestamp > @pi_date))  
        
 CREATE CLUSTERED INDEX p_l_r_obr_tmp ON #lab_tests_tmp ( unique_obr_num )  
       
 SELECT distinct unique_obr_num  
         INTO #xlab  
         FROM #lab_tests_tmp r WITH(NOLOCK)  
  
 SELECT x.unique_obr_num,  
             x.obs_id, x.obs_sub_id, x.result_desc, x.observ_value, x.abnorm_flags, x.units, x.ref_range, x.obx_seq_num,   
             x.signed_off_ind, x.obs_date_time, x.create_timestamp, x.modify_timestamp, x.delete_ind, x.result_seq_num, x.comment_ind   
         INTO #lab_results_tmp  
         FROM #xlab r, lab_results_obx x WITH(NOLOCK)  
         WHERE r.unique_obr_num = x.unique_obr_num 
  AND (x.delete_ind <> 'Y' OR x.delete_ind IS NULL)  
  
  DROP TABLE #xlab  
       
  SELECT  r.enc_nbr, r.med_rec_nbr, r.first_name, r.middle_name, r.last_name,  r.ufo_num, r.req_accession, r.prod_accession, r.order_num, r.obs_batt_id, r.test_desc, x.obs_id, x.obs_sub_id, x.result_desc, x.observ_value, x.abnorm_flags,   
        r.coll_date_time, r.l_create_timestamp, r.l_modify_timestamp, r.r_create_timestamp, r.r_modify_timestamp,  x.create_timestamp AS x_create_timestamp, x.modify_timestamp AS x_modify_timestamp, x.units, x.ref_range, x.obx_seq_num, r.unique_obr_num,   
        x.signed_off_ind, x.obs_date_time, r.enc_id, x.result_seq_num, x.comment_ind   
         FROM #lab_results_tmp x, #lab_tests_tmp r  
         WHERE r.unique_obr_num = x.unique_obr_num  
			ORDER BY r.req_accession, r.unique_obr_num, x.obx_seq_num
        
 DROP TABLE #lab_tests_tmp  
 DROP TABLE  #lab_results_tmp
 
Loading Advertisement...
 
[+][-]05.27.2008 at 08:37AM PDT, ID: 21652790

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.

 
[+][-]05.27.2008 at 08:37AM PDT, ID: 21652793

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.

 
[+][-]05.27.2008 at 10:03AM PDT, ID: 21653567

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.

 
[+][-]05.27.2008 at 12:04PM PDT, ID: 21654615

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.

 
[+][-]05.27.2008 at 12:06PM PDT, ID: 21654626

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.

 
[+][-]05.27.2008 at 12:15PM PDT, ID: 21654715

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
Sign Up Now!
Solution Provided By: chapmandew
Participating Experts: 1
Solution Grade: B
 
 
 
Loading Advertisement...
20080716-EE-VQP-32 / EE_QW_2_20070628