That worked quite well. Thank you Andrew
"Andrew J. Kelly" wrote:
> First of all that is a table variable and not a temp table. But in either
> case it will not work as you expect. The scoping of variables and temp
> tables that are created inside dynamic sql are limited to that execution
> only. Once they are done the code outside of that dynamic batch will not
> see those objects. Only if you create a temp table (not a table var)
> outside of the dynamic sql batch will it be usable in the batch and then
> again after the batch. Have a look at these:
>
>
>
http://www.aspfaq.com/show.asp?id=2248 Arrays & Lists
>
http://www.sommarskog.se/dynamic_sql.html Dynamic SQL
>
http://www.users.drew.edu/skass/sql/ListToTableProc.sql.txt List to
> Table script
>
>
http://www.support.microsoft.com/?id=262499 Using OutPut Params &
> sp_executeSql
>
>
> --
> Andrew J. Kelly SQL MVP
>
>
> "thejamie" <thejamie@discussions.microsoft.com> wrote in message
> news:BE8B9EC0-7CD2-4E71-BA09-C966895456D8@microsoft.com...
> > Suppose the tmptable needed to be created from a string variable fed into
> > the
> > stored procedure:
> > The last line below does not work, but follows the same logic. Can a
> > temp
> > table be created from an EXEC (@SQL) statement?
> > DECLARE @SQL nvarchar(4000),@BeginPercent tinyint,@EndPercent
> > tinyint,@SQLLength int,@FieldList varchar(4000),@SQLTmpTbl
> > nvarchar(4000),@ComputedField varchar(128)
> > SELECT @SQL=N'SELECT
> > L.item_no,N.item_desc_1,N.item_desc_2,L.usage_ytd,L.avg_cost,L.inv_class,L.loc,L.prod_cat
> > FROM iminvloc_sql L INNER JOIN imitmidx_sql N ON L.loc = N.loc AND
> > L.item_no
> > = N.item_no ORDER BY L.avg_cost * L.usage_ytd DESC'
> > SELECT @ComputedField=N'L.usage_ytd * L.avg_cost'
> > DECLARE @SQLSelect nvarchar(4000),@SQLFROM nvarchar(4000)
> > SELECT @SQLSelect=dbo.instr(@SQL,'FROM')
> > SELECT @SQLFROM=REPLACE(@SQL,@SQLSelect,'')
> > SELECT @FieldList=LTRIM(RTRIM(REPLACE(@SQLSelect,'SELECT','')))
> > SELECT @FieldList=REPLACE(@FieldList,',',' varchar(255),')
> > SELECT @FieldList=@FieldList+' nvarchar(255)'
> > SELECT @SQLTmpTbl=N'DECLARE @tmp TABLE('+@Fieldlist+N')'
> > Print REPLACE(@SQLTmpTbl,'.','_')
> > exec sp_executesql @SQLTmpTbl
> >
> >
> > "Andrew J. Kelly" wrote:
> >
> >> No but you can create a temp table before you call the sp_executesql and
> >> access it from inside the dynamic sql. Those rows that you insert inside
> >> the dynamic sql will still be there after the sp_executesql batch is
> >> done.
> >>
> >> --
> >> Andrew J. Kelly SQL MVP
> >>
> >>
>
>