Here's my SQL: CREATE PROCEDURE dbo.ap_get_sales_list @YardID varchar(4), @Year varchar(4), @Make varchar(30), @Model varchar(30), @TitleType varchar(20), @DamageType varchar(40), @Dates varchar(20)as Begin set nocount on --First grab all other columns needed to populate DatagridDeclare cursid Cursor for select item_Id from aims_salelist_columns where display_item=1 order by display_orderDeclare @itemID int, @tempValue varchar(200), @curColumn varchar(100), @curName varchar(100), @finalColumns varchar(2000), @finalQuery varchar(8000)set @finalColumns=''Open Cursid fetch next from cursid into @itemIDwhile (@@fetch_status<>-1) Begin select @curColumn=db_table_name, @curName=display_name from aims_salelist_columns where @itemID=item_id set @tempValue = @curColumn + ' as [' + @curName + ']' set @finalcolumns = @finalcolumns + @tempValue + ', 'set @tempValue=''set @curColumn=''set @curName=''fetch next from cursid into @itemIDEnd deallocate Cursid --print 'Final columns = ' + @finalcolumnsset @finalQuery = 'select stockID, imageID, Year, Make, Model, PrimaryDamage, ' + @finalcolumns + 'ISNULL(NewTitleType, OrigTitleType) as TitleType ' set @finalQuery = @finalQuery + 'from aims.dbo.websalelist a join aims.dbo.sale b on a.SaleID=b.SaleID where year like ''' + @year + ''' and make like ''' + @make + ''' and model like ''' + @model + ''' and ISNULL(NewTitleType, OrigTitleType) like ''' + @titletype + '''' set @finalQuery = @finalQuery + 'and (primarydamage like ''' + @DamageType + ''' or secondaryDamage like ''' + @DamageType + ''') and a.SaleID like ''' + @Dates + ''' and b.yardID like ''' + @YardID + ''' and b.saleDate >=getdate()'--print @finalqueryexec(@finalquery)EndGO
What I need to do is this: 1. Populate a Temp table in the Stored Proc with the results of the dynamic SQL I've created in the above statement. (Problem here is that I'll never know how many or which columns I'll be getting on a given day)2. Create a second table ("#BidInfo") that will have 3 columns: StockID, HasBid, HasHighBid. 3. Loop through each one of the Stock ID's from the first Temp table and determine whether a user has the high bid or not and record information in the #Bidinfo table. 4. Do a join on both tables and return all rows as one doing a join on the StockID field. Now, I know how to do the last 3 steps pretty easily. I don't know how to generate a Temp Table automatically based on the resultset. Any ideas how to create a temp table based of a Select statement?