Solved

Read a Temp Table in a stored procedure

Posted on 2006-11-14
6
455 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
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
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 
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 125 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

VMware Disaster Recovery and Data Protection

In this expert guide, you’ll learn about the components of a Modern Data Center. You will use cases for the value-added capabilities of Veeam®, including combining backup and replication for VMware disaster recovery and using replication for data center migration.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
SQL SERVER - Index skipped a number 2 25
Database ERD 4 26
job schedule 8 17
SQL Recursion 6 16
Having an SQL database can be a big investment for a small company. Hardware, setup and of course, the price of software all add up to a big bill that some companies may not be able to absorb.  Luckily, there is a free version SQL Express, but does …
Slowly Changing Dimension Transformation component in data task flow is very useful for us to manage and control how data changes in SSIS.
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…

809 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