Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Read a Temp Table in a stored procedure

Posted on 2006-11-14
6
Medium Priority
?
461 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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
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

NEW Veeam Agent for Microsoft Windows

Backup and recover physical and cloud-based servers and workstations, as well as endpoint devices that belong to remote users. Avoid downtime and data loss quickly and easily for Windows-based physical or public cloud-based workloads!

Question has a verified solution.

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

For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
When trying to connect from SSMS v17.x to a SQL Server Integration Services 2016 instance or previous version, you get the error “Connecting to the Integration Services service on the computer failed with the following error: 'The specified service …
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.
Via a live example, show how to set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.

609 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