?
Solved

Read a Temp Table in a stored procedure

Posted on 2006-11-14
6
Medium Priority
?
460 Views
Last Modified: 2008-02-01
In sql server 2000.
I have a temp table in a stored procedure,
I want to read through the temp table and examine each column in each of the rows of that table.
How would I do that.
I also need to determine if the table is empty or not before I start reading it.

Thanks

Laurence

 
0
Comment
Question by:misdevelopers
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
6 Comments
 
LVL 11

Expert Comment

by:rw3admin
ID: 17943368
You mean inside the proc? you can read the temp table just like regular table, you cannot reference this table outside the proc as Temp table is only local to the proc, actually local only to the session in which proc ran so you cannot say

select * from sMyProc.#TmpTable
0
 

Author Comment

by:misdevelopers
ID: 17943378
Yes, I want to reference it inside the proc.
But I don't want to just create a recordset from the temp table.
I want to read through the temp table and examine each row and each column in each row.

I want to pass back just a few values from the recordset as OUPUT parameters,
rather than having to pass back the recordset (that was created from the temp table).

This is called thousands of times, for a report.

Laurence  
0
 
LVL 75

Expert Comment

by:Anthony Perkins
ID: 17943620
>>But I don't want to just create a recordset from the temp table.<<
The big question is: Why are you doing this?  This is the least efficient way of doing things using T-SQL.  However if you insist look up DECLARE CURSOR in BOL.
0
How Blockchain Is Impacting Every Industry

Blockchain expert Alex Tapscott talks to Acronis VP Frank Jablonski about this revolutionary technology and how it's making inroads into other industries and facets of everyday life.

 
LVL 1

Expert Comment

by:Yogeshup
ID: 17944641
acperkins is right. Maybe you should try assigning the values required to variables using TSQL. That would be much faster. What would be the size of the temporary table? Is it necessary that you do it row-by-row and column-by-column? Maybe I can help if you ca explain what exactly you are looking for.
0
 
LVL 28

Expert Comment

by:imran_fast
ID: 17946719
hi misdevelopers,
Read about table variables and functions inside books online may be that is what you are looking for.
0
 
LVL 11

Accepted Solution

by:
rw3admin earned 500 total points
ID: 17948248
Laurence
>>I want to read through the temp table and examine each row and each column in each row.<<
I assume there is no human interaction involved in analyzing each row and each column in each row.
you definetly need some programming to logic to take different action for different values in rows/columns.
You can do all of that programatically and in the very same procedure.
Cursors are best at stepping through records and analyzing each column in the current fetched row. Thats exactly what experts here.
If editing current proc is an issue and you dont want to change code in this one, and this proc is just returning data from a temp table or set of temp and physical tables (joining them), then you can still create another proc, create a temp table in that proc and capture data returned from first proc into your second procs temp table.... for example.. and this is very very basic example.

your first proc looks like

create proc sTest_1
as
Select 1 as ID, 2 as Val

You can create second proc as

create proc sTest_2
as
Create Table #T (ID int, Val int) -- create temp table in second proc
Insert #T -- now insert data set returned from 1st proc (sTest_1)
Exec sTest_1
--------- now add your whatever logic here....
declare cursor.......
....
...
..
..

rw3admin



0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Everyone has problem when going to load data into Data warehouse (EDW). They all need to confirm that data quality is good but they don't no how to proceed. Microsoft has provided new task within SSIS 2008 called "Data Profiler Task". It solve th…
Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
Using examples as well as descriptions, and references to Books Online, show the documentation available for date manipulation functions and by using a select few of these functions, show how date based data can be manipulated with these functions.
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.
Suggested Courses

771 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question