Solved

Read a Temp Table in a stored procedure

Posted on 2006-11-14
6
452 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
Comment Utility
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
Comment Utility
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
Comment Utility
>>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
Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

 
LVL 1

Expert Comment

by:Yogeshup
Comment Utility
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
Comment Utility
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
Comment Utility
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

Free Gift Card with Acronis Backup Purchase!

Backup any data in any location: local and remote systems, physical and virtual servers, private and public clouds, Macs and PCs, tablets and mobile devices, & more! For limited time only, buy any Acronis backup products and get a FREE Amazon/Best Buy gift card worth up to $200!

Join & Write a Comment

International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
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…

763 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

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now