General SQL Development
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
 
User Name:
Password:
Remember me
 
Go Back   Dev Articles Community ForumsDatabasesGeneral SQL Development

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Display Modes
 
Unread Dev Articles Community Forums Sponsor:
  #1  
Old May 21st, 2008, 01:15 PM
paraglidersd paraglidersd is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: May 2008
Posts: 1 paraglidersd User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 18 m 41 sec
Reputation Power: 0
Question Temp table manipulation in Stored Procedure

I am pretty inexperienced when it comes to sql, but I am an experienced engineer.

Want to create a stored procedure. Within that procedure I am going to create a temp table with certain attributes read in from 2 other permanent tables. After that, within the stored procedure, I want to read the resulting rows of the temp table one by one (one of the attributes will be a key that I will use to access yet another permanent table). I dont know how to do this.

select key1, att1 into #temp_table
from permTable1
where
key > xx
date > 999999

insert #temp_table
select key1, att1 from permTable1History
where
key > xx
date > 999999


....this is where I am stuck....how do I now read the contents of the #temp_table within the stored procedure one by one, to pull out that key attribute, to use to read from another permanent table?

thanks,
Bill

Reply With Quote
  #2  
Old June 16th, 2008, 03:09 PM
dragonbite dragonbite is offline
Contributing User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Jan 2008
Location: Connecticut, USA
Posts: 34 dragonbite User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 9 h 37 m 44 sec
Reputation Power: 1
selecting with a temp table?

Here are some ideas, assuming I am understanding your needs right.

1. Use UNION instead of combining in a temp table. This can also be placed in a view any stored procedures can access.
Code:
SELECT key1, att1
FROM permTable1
WHERE key > xx  AND date > 999999

UNION ALL

SELECT key1, att1
FROM permTable1History
WHERE key > xx  AND date > 999999
2. Combine the stored procedures so this code is in the beginning of the code and then you process it without going into another stored procedure.

3. Instead of #temp_table use 2 "#"s, (##temp_table) so it's accessible from outside procedures (use with caution)
Code:
select key1, att1 into ##temp_table 
from permTable1 
where
key > xx
date > 999999

insert ##temp_table
select key1, att1 from permTable1History
where 
key > xx
date > 999999
The procedure actually just makes and populates the table but doesn't return any values. The calling procedure will have to run the procedure to populate the temporary table like so: (let's assume the above stored procedure is called sproc_make_temp_table)
Code:
-- populate temp table
EXEC sproc_make_temp_table

-- select values (or process as necessary)

SELECT key1, att1
FROM ##temp_table

-- clean up table or it will stay there and can make a mess

DROP TABLE ##temp_table

Reply With Quote
Reply

Viewing: Dev Articles Community ForumsDatabasesGeneral SQL Development > Temp table manipulation in Stored Procedure


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 4 hosted by Hostway
Stay green...Green IT