March 1, 2006 at 8:19 pm
I have created a table dynamically. I now need a way to populate the columns. I just don't know how many columns there are until after the table is created. Does anyone know of a way to dynamically insert data into a variably sized temp table?
Any ideas welcome!!!
Thanks,
Eva
March 2, 2006 at 12:26 pm
Interesting. How comes you don't know your tables fields?
_/_/_/ paramind _/_/_/
March 2, 2006 at 4:42 pm
Because the table is being created dynamically. No table exists until a cursor is executed and the table is defined.
March 2, 2006 at 5:05 pm
While executing that cursor you could assemble a retrieval statement at the same time
_/_/_/ paramind _/_/_/
March 2, 2006 at 8:19 pm
Could you give me a brief example of what you mean?
March 3, 2006 at 12:03 am
surely. can you post the cursor?
_/_/_/ paramind _/_/_/
March 3, 2006 at 12:14 pm
Dingbat me realized after posting this what you meant and OF COURSE that was the way to do it 🙂
I added the insert for each time I create a column. It works very nicely except that SQL Server wants to run my initial insert at the end again so I am getting an error. Any idea of how to stop that?
declare @armid bigint
set @armid = 2
CREATE TABLE #Proc (AddOn varchar(50), AddOnID int)
INSERT #Proc
select distinct pao.ProcedureAddOn, apao.ProcedureAddOnID
from tb_ArmProcedureAddOn apao
inner join tb_ProcedureAddOn pao on pao.ProcedureAddOnID = apao.ProcedureAddOnID
inner join tb_ProtocolArmProcedure pap on pap.ProtocolArmProcedureID = apao.ProtocolArmProcedureID
where pap.ProtocolArmID = @armid
order by pao.ProcedureAddOn
DECLARE @AddOn int
DECLARE @proc varchar(50)
DECLARE @procid bigint
declare proc_cur cursor
for
select ProcedureName, ProtocolArmProcedureID from tb_ProtocolArmProcedure pap
where pap.ProtocolArmID = @armid
order by sortorder
open proc_cur
fetch proc_cur into @proc, @procid
While @@fetch_status = 0
begin
exec('alter table #Proc add [' + @proc + '] smallmoney NULL')
exec('update #Proc set [' + @proc + '] =
((select (charge + (charge * (overhead/100))) from tb_ArmProcedureAddOn apao
where apao.ProcedureAddOnID = #Proc.AddOnID
and apao.ProtocolArmProcedureID = ' + @procid + ')*
(select count(ArmProcedureStageID) from tb_ArmProcedureStage aps
where aps.ProtocolArmProcedureID = ' + @procid + '))')
fetch proc_cur into @proc, @procid
end
close proc_cur
deallocate proc_cur
select * from #Proc
DROP TABLE #Proc
It runs the whole thing wonderfully and then trys to run the very first insert again. Of course, that fails since now the number of columns are more than 2. If I allow the error to show up in Query Analyzer and then select * from #Proc is works WONDERFULLY! How do I make it not try the initial insert again???
Thank you!!!!
Eva
March 3, 2006 at 1:42 pm
I got it. I altered that first insert to be a select...into statement and it executes BEAUTIFULLY!!!
March 3, 2006 at 1:46 pm
Dear Eva. Please use a bigger font size next time - I was looking for my glasses since two hours, instead of answering your question
_/_/_/ paramind _/_/_/
March 3, 2006 at 1:51 pm
haha, it was small text....I'd recommend the magic of ctrl + scroll 😉
March 3, 2006 at 1:56 pm
does that work for little brain sizes too?
_/_/_/ paramind _/_/_/
March 3, 2006 at 2:55 pm
I don't personally like using Select Into because if one of your columns has no data in it (all nulls) you will get an error stating that it can not create a column of size 0.
You can generate a simple insert query by querying the Information_Schema.Columns view.
Viewing 12 posts - 1 through 11 (of 11 total)
You must be logged in to reply to this topic. Login to reply