Re: Temp Tables, Transaction and Loops

From: Jan Schoenrock (JanSchoenrock_at_discussions.microsoft.com)
Date: 09/21/04


Date: Tue, 21 Sep 2004 02:25:02 -0700

Hello Uri,

thanks for your answer. I will try it. But I do not understand why I must
use EXEC sp_executesql for simple operations ( in your example updates), if
it runs.
Is it performant and what is the reason for using EXEC sp_executesql in this
case. If I've a string containing SQL it is clear, but in this case with
static SQL ?
Can you tell me about the technical background ?

Thanks Jan

"Uri Dimant" wrote:

> Jan
> I simplified the problem but it should be shed a light for you
> CREATE PROC spmyproc
> AS
> SELECT OrderId INTO #Temp FROM NorthWind.dbo.Orders
> ALTER TABLE #Temp ADD col INT
> UPDATE #Temp SET col=1
>
> GO
>
> EXEC spmyproc
> ----Server: Msg 207, Level 16, State 1, Procedure spmyproc, Line 5
> -----Invalid column name 'col'.
>
> ALTER PROC spmyproc
> AS
> SELECT OrderId INTO #Temp FROM NorthWind.dbo.Orders
> ALTER TABLE #Temp ADD col INT
> EXEC sp_executesql N'UPDATE #Temp SET col=1'
>
> EXEC spmyproc
> ----Now it works
> OR
> ALTER PROC spmyproc
> AS
> SELECT OrderId,CAST(NULL AS INT)as col INTO #Temp FROM NorthWind.dbo.Orders
> UPDATE #Temp SET col=1
>
> EXEC spmyproc
> ----Now it works
>
> drop proc spmyproc
>
>
>
> "Jan Schoenrock" <JanSchoenrock@discussions.microsoft.com> wrote in message
> news:C8CCEFF6-FDAF-478A-84BF-AA46ABC758A1@microsoft.com...
> > Hi,
> >
> > I'm running SLQ Server 2000 and I've had the following Problem:
> >
> > When I'm using a temp table within a stored procedure and call this stored
> > procedure from another stored proc within a loop and a transaction I
> always
> > get a mass of error messages. The content of these messages is always the
> > same: "Ivalid column 'x'" (translated) but x is a valid col of the temp
> > table and the temp table is created.
> >
> > If I call the stored proc from another stored proc without loop and
> > transaction it runs well.
> >
> > Some details to the environment of this problem:
> > I have to find solutions for some optimizing tasks. All these task are in
> a
> > Table T1 and depending on the task it can be a minimizing or a maximizing
> > case.
> > To optimize a stored proc SP1 loops through the rows in T1 and each row
> > treatmend is encapsulated in a transaction.
> > Depending on the case of the row (min or max) within the loop the
> "minimize
> > stored proc" (SP2) or the "maximize stored proc" (SP3) is called. SP2 or
> SP3
> > are very similar but for performance and readability maximizing and
> > minimizing are devided into two stored proc. The beginning of SP2,SP3 is
> to
> > create a temp table #Solution, and at the end of SP2,SP3 #Solution is
> dropped
> > by "DROP TABLE".
> > SP2, SP3 are inserting their results into a regular table and are
> returning
> > only some parameters back to SP1.
> > SP1 commits the transaction and loops to the next row, begins a new
> > transaction, calls SP2 or SP3 and so on.
> >
> > Because I did not find a way to solve this problem by holding on using the
> > temp table version, now I'm using a regular table without further changes
> and
> > it runs very well.
> >
> > Greetings Jan
> >
> >
> >
> >
> > --
> > Jan Schoenrock, Hamburg, Germany
>
>
>



Relevant Pages

  • Re: T-SQL how to deal with results from stored proc
    ... > My attempt was to use INSERT EXEC to fetch te results of several ... In a wrapping stored proc I intended to collect data ... You can also use temp tables, as I discuss in the article I pointed you to. ... Erland Sommarskog, SQL Server MVP, esquel@xxxxxxxxxxxxx ...
    (comp.databases.ms-sqlserver)
  • Re: Temp Tables, Transaction and Loops
    ... CREATE PROC spmyproc ... SELECT OrderId INTO #Temp FROM NorthWind.dbo.Orders ... ALTER TABLE #Temp ADD col INT ... > procedure from another stored proc within a loop and a transaction I ...
    (microsoft.public.sqlserver.programming)
  • Re: full-text contains * issue
    ... You may also want to consider using a stored proc for this situation as it ... EXEC usp_FTSearchPubsInfo '' ... > no data but was concatenation of the fields I wanted to index. ... > This is the SQL ...
    (microsoft.public.sqlserver.fulltext)
  • Re: table datatype in EXEC string
    ... the biggie) Or is this the old, someone said temp tables are bad so I don't ... The EXEC function launches ... > into an execution string and try to run it that I have problems. ... >>> loading the table from a number of insert/select statements in the sp ...
    (microsoft.public.sqlserver.programming)
  • Re: combining results from 3 stored procedures
    ... This example just runs the same exec three time to ... insert into #temp exec sp_helpfile -- first sp ... bring back the results of the executed stored procedures to the main stored ... > Declare @EndDate datetime ...
    (microsoft.public.sqlserver.programming)