Solved

Read a Temp Table in a stored procedure

Posted on 2006-11-14
6
456 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
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 
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

NFR key for Veeam Backup for Microsoft Office 365

Veeam is happy to provide a free NFR license (for 1 year, up to 10 users). This license allows for the non‑production use of Veeam Backup for Microsoft Office 365 in your home lab without any feature limitations.

Question has a verified solution.

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

JSON is being used more and more, besides XML, and you surely wanted to parse the data out into SQL instead of doing it in some Javascript. The below function in SQL Server can do the job for you, returning a quick table with the parsed data.
In this article we will learn how to fix  “Cannot install SQL Server 2014 Service Pack 2: Unable to install windows installer msi file” error ?
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…
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.

733 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