Welcome to dbFreaks.com!
FAQFAQ    SearchSearch      ProfileProfile    Private MessagesPrivate Messages   Log inLog in

How to loop a temp table?

 
   Database Help (Home) -> Programming RSS
Next:  quoted binary  
Author Message
Daniel Badger

External


Since: Jun 14, 2007
Posts: 1



(Msg. 1) Posted: Thu Jun 14, 2007 5:34 pm
Post subject: How to loop a temp table?
Archived from groups: microsoft>public>sqlserver>programming (more info?)

I have a statement in my stored procedure that has the following as an
example -

INSERT #TempTableOutput
EXEC (@select + @from + @where + @order)

How could I then loop through the records of #TempTableOutput to update some
blank fields that were created in the intial select statement?

 >> Stay informed about: How to loop a temp table? 
Back to top
Login to vote
xyb

External


Since: Jun 04, 2007
Posts: 1



(Msg. 2) Posted: Thu Jun 14, 2007 6:34 pm
Post subject: Re: How to loop a temp table? [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

On 6 15 , 8 34 , Daniel Badger
wrote:
> I have a statement in my stored procedure that has the following as an
> example -
>
> INSERT #TempTableOutput
> EXEC (@select + @from + @where + @order)
>
> How could I then loop through the records of #TempTableOutput to update some
> blank fields that were created in the intial select statement?

You can use cursor to loop through records first although it is not
recommented:).
declare mcur as cursor for ....look it up at BOL.
And second,you should use a WHILE loop and another temp table.
And last, why not use set update Smile
I hope this can help you.

 >> Stay informed about: How to loop a temp table? 
Back to top
Login to vote
M A Srinivas

External


Since: Jun 05, 2007
Posts: 1



(Msg. 3) Posted: Thu Jun 14, 2007 7:54 pm
Post subject: Re: How to loop a temp table? [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

On Jun 15, 5:34 am, Daniel Badger
wrote:
> I have a statement in my stored procedure that has the following as an
> example -
>
> INSERT #TempTableOutput
> EXEC (@select + @from + @where + @order)
>
> How could I then loop through the records of #TempTableOutput to update some
> blank fields that were created in the intial select statement?


Create an identity column in #TempTableOutput and loop through
Provide column names in INSERT #TempTableOutput (col1,ol2 ..) .
Don't put the identity column in the column list
 >> Stay informed about: How to loop a temp table? 
Back to top
Login to vote
--CELKO--

External


Since: Jun 14, 2007
Posts: 4



(Msg. 4) Posted: Thu Jun 14, 2007 7:54 pm
Post subject: Re: How to loop a temp table? [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

>> INSERT INTO #TempTableOutput
EXEC (@select + @from + @where + @order);

How could I then loop through the records [sic] of #TempTableOutput to
update some blank fields [sic] that were created in the intial select
statement? <<

Columns are not anything like fields; rows are not anything like
records. Materializing a temp table is a bad programming practice to
be avoided. Dynamic SQL is a bad programming practice to be avoided.
You have no idea what the result will be until run time! You cannot
even give this thing a valid data element name (is it Briteny Spears,
automobiles or squid this execution?)

Wha you have is an attempt to mimic a scrath tape the way we did with
1950's-60'S programming languages when storage was limited. This is
not RDBMS at all!

What are you actually trying to do? You probably need more help than
you will find in a newsgroup, but we can try.
 >> Stay informed about: How to loop a temp table? 
Back to top
Login to vote
David Portas

External


Since: Jul 04, 2007
Posts: 5



(Msg. 5) Posted: Thu Jun 14, 2007 11:12 pm
Post subject: Re: How to loop a temp table? [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

On 15 Jun, 01:34, Daniel Badger
wrote:
> I have a statement in my stored procedure that has the following as an
> example -
>
> INSERT #TempTableOutput
> EXEC (@select + @from + @where + @order)
>
> How could I then loop through the records of #TempTableOutput to update some
> blank fields that were created in the intial select statement?

This is a bad idea. Either change the INSERT ... SELECT statement so
that the table is populated with the right data to start with, OR use
a single UPDATE statement to update the temp table.

--
David Portas, SQL Server MVP

Whenever possible please post enough code to reproduce your problem.
Including CREATE TABLE and INSERT statements usually helps.
State what version of SQL Server you are using and specify the content
of any error messages.

SQL Server Books Online:
http://msdn2.microsoft.com/library/ms130214(en-US,SQL.90).aspx
--
 >> Stay informed about: How to loop a temp table? 
Back to top
Login to vote
Display posts from previous:   
Related Topics:
inserting efficiently into a temp table/table variable - I am a newbie to Sql Server programming, and I was hoping that someone might be able to point me in the proper direction. I am trying to write a C function that takes an array of strings and insert them into either a temporary table or a table variable....

Table variable performance vs. temp table - Hi, I've got an identical query, one with temp tables and one with table variables that results in two completely different execution times. The plans look virtually the same but execution time is around 2 minutes with the variables and less than a..

Temp Table - I have created a temp table using the following query: CREATE TABLE #temptbl1 (Col1 varchar(50), Col2 int) When I run the following query: USE tempdb IF EXISTS (SELECT * FROM sysobjects WHERE NAME = '#temptbl1') PRINT 'TABLE EXISTS' ELSE PRINT 'TABL...

Temp Table's - Hello All, I use several local temp tables in an application. These tables store information that is built from some text files by the user. This data cannot be mixed up by another user logging in at the same time. Is there a chance that another user can...

Loop through Table Records - I'm working on a script that creates a temp table called #tmpList. How can Loop through the records of #tmpList? So far in my using SQL, I've always written SQL statements to just return records and I did my looping with VB. I need to be able to loop....
   Database Help (Home) -> Programming All times are: Pacific Time (US & Canada)
Page 1 of 1

 
You can post new topics in this forum
You can reply to topics in this forum
You can edit your posts in this forum
You can delete your posts in this forum
You can vote in polls in this forum



[ Contact us | Terms of Service/Privacy Policy ]