Hi all
I understand why I'm getting this error but do I overcome it?
I have a stored procedure called IAM_CAC_CHART(will supply code below) which will be used to pass data to the developers to generate a pie report. The proc uses a cursor as well as 2 temp tables, 1 outside the cursor and another within. Within the cursor i use dynamic sql to execute another proc called IAM_CAC. This is used in an INSERT INTO EXEC(@sql) to store the result returned from IAM_CAC into the 1 temp table.
The catch is that IAM_CAC also executes dynamic sql to exec another proc and store the result in a temp table. IAM_CAC calls a proc called IAM_TOTAL_BASE_VALUE as it has to do calculations using the result returned.
Since SQL Server doesn't support nested INSERT EXEC statements I've run into a wall.
The called procs are mainly used for a very important line graph which displays TCO figures, base price, condition assessment code for tagged assets across a certian time period. I want to avoid changing the called procs at all costs as they work with formulas, calculations, cursors etc and took awhile to get bedded down.
Here is the code:
CREATE procedure IAM_CAC_CHART @costcentre varchar(20)
as
declare @tagno varchar(20)
declare @desc varchar(200)
declare @assetno varchar(20)
declare @serialno varchar(50)
declare @type varchar(20)
create table #piechart
(tag_no varchar(20),
description varchar(200),
asset_no varchar(20),
serial_no varchar(50),
type varchar(20),
cac int)
declare chart_cursor cursor for
select a.tag_no, a.description, a.asset_no, a.serial_no, 'PRIMARY'
from asset a, tag_record tr
where a.cost_centre = @costcentre
and a.tag_no is not null
and a.asset_id = tr.asset_id
and tr.rel_indic is null
order by a.tag_no
open chart_cursor
fetch chart_cursor into @tagno, @desc, @assetno, @serialno, @type
while @@fetch_status = 0
begin
create table #piecac
(cac money)
declare @value money
declare @sql varchar(1000)
set @sql = 'exec iam_cac '+'"' +@tagno+'"'
insert into #piecac
exec(@sql)
set @value = (select * from #piecac)
insert into #piechart (tag_no, description, asset_no, serial_no, type, cac)
select @tagno, @desc, @assetno, @serialno, @type, @value
fetch chart_cursor into @tagno, @desc, @assetno, @serialno, @type
end
close chart_cursor
deallocate chart_cursor
select * from #piechart
GO
I'm also getting errors about the temp table already exists. I've been microsoft support and read the articles there but I didn't find a solution.
Is there away to change IAM_CAC_CHART to do what I need without changing the called procs or only changing the called procs slightly?
Any help will be appreciated greatly!
John
I understand why I'm getting this error but do I overcome it?
I have a stored procedure called IAM_CAC_CHART(will supply code below) which will be used to pass data to the developers to generate a pie report. The proc uses a cursor as well as 2 temp tables, 1 outside the cursor and another within. Within the cursor i use dynamic sql to execute another proc called IAM_CAC. This is used in an INSERT INTO EXEC(@sql) to store the result returned from IAM_CAC into the 1 temp table.
The catch is that IAM_CAC also executes dynamic sql to exec another proc and store the result in a temp table. IAM_CAC calls a proc called IAM_TOTAL_BASE_VALUE as it has to do calculations using the result returned.
Since SQL Server doesn't support nested INSERT EXEC statements I've run into a wall.
The called procs are mainly used for a very important line graph which displays TCO figures, base price, condition assessment code for tagged assets across a certian time period. I want to avoid changing the called procs at all costs as they work with formulas, calculations, cursors etc and took awhile to get bedded down.
Here is the code:
CREATE procedure IAM_CAC_CHART @costcentre varchar(20)
as
declare @tagno varchar(20)
declare @desc varchar(200)
declare @assetno varchar(20)
declare @serialno varchar(50)
declare @type varchar(20)
create table #piechart
(tag_no varchar(20),
description varchar(200),
asset_no varchar(20),
serial_no varchar(50),
type varchar(20),
cac int)
declare chart_cursor cursor for
select a.tag_no, a.description, a.asset_no, a.serial_no, 'PRIMARY'
from asset a, tag_record tr
where a.cost_centre = @costcentre
and a.tag_no is not null
and a.asset_id = tr.asset_id
and tr.rel_indic is null
order by a.tag_no
open chart_cursor
fetch chart_cursor into @tagno, @desc, @assetno, @serialno, @type
while @@fetch_status = 0
begin
create table #piecac
(cac money)
declare @value money
declare @sql varchar(1000)
set @sql = 'exec iam_cac '+'"' +@tagno+'"'
insert into #piecac
exec(@sql)
set @value = (select * from #piecac)
insert into #piechart (tag_no, description, asset_no, serial_no, type, cac)
select @tagno, @desc, @assetno, @serialno, @type, @value
fetch chart_cursor into @tagno, @desc, @assetno, @serialno, @type
end
close chart_cursor
deallocate chart_cursor
select * from #piechart
GO
I'm also getting errors about the temp table already exists. I've been microsoft support and read the articles there but I didn't find a solution.
Is there away to change IAM_CAC_CHART to do what I need without changing the called procs or only changing the called procs slightly?
Any help will be appreciated greatly!
John