Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2000 Forums
 SQL Server Development (2000)
 Help with dynamically generated SQl

Author  Topic 

label
Posting Yak Master

197 Posts

Posted - 2003-10-15 : 11:34:12
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 Datagrid
Declare cursid Cursor for
select item_Id from aims_salelist_columns
where display_item=1
order by display_order

Declare @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 @itemID
while (@@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 @itemID
End

deallocate Cursid
--print 'Final columns = ' + @finalcolumns

set @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 @finalquery
exec(@finalquery)

End
GO


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?

nr
SQLTeam MVY

12543 Posts

Posted - 2003-10-15 : 11:51:40
1. You will need to use select into with openquery or openrowset. This will need another connection to the server so you will have to be caredul about locking.
It's probably worth deciding if that's feasible before going further.

p.s. I think your cursor can be replaced by a single sql statement.

Be careful what you allow into the variables as it may have security implications

==========================================
Cursors are useful if you don't know sql.
DTS can be used in a similar way.
Beer is not cold and it isn't fizzy.
Go to Top of Page

nr
SQLTeam MVY

12543 Posts

Posted - 2003-10-15 : 11:53:50
Actually why have a second temp table. You can incorporate this in the dynamic sql - maybe then you won't need to put the output into a temp table and it can just be a select.

==========================================
Cursors are useful if you don't know sql.
DTS can be used in a similar way.
Beer is not cold and it isn't fizzy.
Go to Top of Page

label
Posting Yak Master

197 Posts

Posted - 2003-10-16 : 08:48:41
quote:
Originally posted by nr

Actually why have a second temp table. You can incorporate this in the dynamic sql - maybe then you won't need to put the output into a temp table and it can just be a select.



Alright, I'm reading about openquery and openrowset and I'm not really clear how that helps me.

Isn't there a way I can just prepend something like "Insert into #tempTable select * from myTable" where the #tempTable is created based on the results of the select statement?
Go to Top of Page

Lavos
Posting Yak Master

200 Posts

Posted - 2003-10-16 : 10:33:42
If you use

SELECT * INTO #tempTable
FROM myTable


it will fill and create your table. This locks the system tables during the creation and fill though, and so you might have problems with contention. (Remember, temp tables are created in the temp database, and so it is the system tables of the Temp database that are going to be locked. Not good if you have a lot of procedures using temp tables and the query takes a long time to run.)

Also, while I don't know your tables layout exactly, but it looks like you plan on using a cursor on the temp table to find the results. This could be added to your query directly by using some LEFT JOIN's and a CASE statement.


If you post your table's DDL, I'm sure someone will help you out with adjusting the query.

----------------------
"O Theos mou! Echo ten labrida en te mou kephale!"

"Where theres a will, theres a kludge." - Ken Henderson
Go to Top of Page

nr
SQLTeam MVY

12543 Posts

Posted - 2003-10-16 : 11:57:06
You can create a temp table based on the results of a query but not from dynamic sql (You cannot do select * into #a from exec (@sql). You can create the temp table within the exec statement but it will be dropped when it completes.
Openrowset generates a resultset so you can execute
select * into #a from openrowset
The dynamic sql is executed by the openquery and the select into will generate the temp table.


==========================================
Cursors are useful if you don't know sql.
DTS can be used in a similar way.
Beer is not cold and it isn't fizzy.
Go to Top of Page

Lavos
Posting Yak Master

200 Posts

Posted - 2003-10-16 : 12:17:20
Just a quick question, but doesn't openquery refuse to allow non-constants as an arguement? aka it can't use a variable, meaning that the call with it would be forced to be dynamic sql and thus back to square one again?

Best suggestion, forget temp tables and re-write the query to not need them.

I was bored, so I rewrote your cursor to build the column list also. Just replace your declare's and everything to your deallocate with this:


Declare @finalColumns varchar(2000),
@finalQuery varchar(8000)

set @finalColumns=''

-- Builds your column list
select final_columns = db_table_name + ' as ['+display_name+'], '
from aims_salelist_columns
where display_item=1
order by display_order



(See, now if I knew what you needed, I probably would've done the real query modification you wanted to :)

----------------------
"O Theos mou! Echo ten labrida en te mou kephale!"

"Where theres a will, theres a kludge." - Ken Henderson
Go to Top of Page
   

- Advertisement -