christine1
MIS
Hi everyone,
I am trying to improve one of my sproc's instead up having 3 sproc's that do the same thing, just call FROM 3 different tables. I want to use 1 sproc that utilizes Dynamic SQL to to do the same thing using the variable. Here's my code can anyone tel me where I'm going wrong and please help me with the sytax forthis thing...
--First I create a temp table...
CREATE TABLE #tmp_et
(site_id_piece varchar(12),
site_code varchar(12),
inspection_code varchar(3) NULL,
description varchar(60),
site_hrs float NULL,
site_cnt int NULL,
site_avg_hrs float NULL,
xfleet_hrs float NULL,
xfleet_cnt int NULL,
xfleet_avg_hrs float NULL)
CREATE TABLE #tmp_et_site
(site_id_piece varchar(12),
site_code varchar(12),
inspection_code varchar(3) NULL,
description varchar(60),
site_hrs float NULL,
site_cnt int NULL,
site_avg_hrs float NULL)
TRUNCATE TABLE #tmp_et
TRUNCATE TABLE #tmp_et_site
***/SELECT @command_str = 'cube_unit_sched_events_scp'
SELECT @report_type = 'sched'
EXEC (@command_str)/***
INSERT INTO #tmp_et (site_id_piece, site_code, inspection_code, description,
xfleet_hrs, xfleet_cnt)
SELECT @id_piece,
site_code,
inspection_code,
description,
SUM(c.outage_hours),
SUM(c.event_count)
--FROM cube_unit_'sched'_events_scp c, */This is the table name that will change depending on the required report/*
cube_header_info hi
WHERE hi.design = @design
AND c.id_piece = hi.unit_id_piece
AND c.begin_date <= @begin_date
AND DATENAME(year, c.begin_date) = DATENAME(year,@begin_date)
GROUP BY c.description, c.site_code, c.INSPECTION_CODE
ORDER by c.site_code
***/SELECT @command_str = 'cube_unit_sched_events_scp'
EXEC sp_cube_et_sched_maintenance/***
INSERT INTO #tmp_et_site (site_phys_piece, ebs_code, inspection_code, description,site_hrs, site_cnt, site_avg_hrs)
SELECT @id_piece,
site_code,
inspection_code,
description,
SUM(c.outage_hours),
SUM(c.event_count),
SUM(c.outage_hours) / SUM(c.event_count)
--FROM cube_unit_sched_events_scp c,
cube_header_info hi
WHERE hi.site_phys_piece = @phys_piece
AND hi.design = @design
AND c.id_piece = hi.id_piece
AND c.begin_date <= @begin_date
AND DATENAME(year, c.begin_date) = DATENAME(year,@begin_date)
GROUP BY c.description, c.site_code, c.INSPECTION_CODE
ORDER by c.site_code
Please help, what am doing wrong?
[sig][/sig]
I am trying to improve one of my sproc's instead up having 3 sproc's that do the same thing, just call FROM 3 different tables. I want to use 1 sproc that utilizes Dynamic SQL to to do the same thing using the variable. Here's my code can anyone tel me where I'm going wrong and please help me with the sytax forthis thing...
--First I create a temp table...
CREATE TABLE #tmp_et
(site_id_piece varchar(12),
site_code varchar(12),
inspection_code varchar(3) NULL,
description varchar(60),
site_hrs float NULL,
site_cnt int NULL,
site_avg_hrs float NULL,
xfleet_hrs float NULL,
xfleet_cnt int NULL,
xfleet_avg_hrs float NULL)
CREATE TABLE #tmp_et_site
(site_id_piece varchar(12),
site_code varchar(12),
inspection_code varchar(3) NULL,
description varchar(60),
site_hrs float NULL,
site_cnt int NULL,
site_avg_hrs float NULL)
TRUNCATE TABLE #tmp_et
TRUNCATE TABLE #tmp_et_site
***/SELECT @command_str = 'cube_unit_sched_events_scp'
SELECT @report_type = 'sched'
EXEC (@command_str)/***
INSERT INTO #tmp_et (site_id_piece, site_code, inspection_code, description,
xfleet_hrs, xfleet_cnt)
SELECT @id_piece,
site_code,
inspection_code,
description,
SUM(c.outage_hours),
SUM(c.event_count)
--FROM cube_unit_'sched'_events_scp c, */This is the table name that will change depending on the required report/*
cube_header_info hi
WHERE hi.design = @design
AND c.id_piece = hi.unit_id_piece
AND c.begin_date <= @begin_date
AND DATENAME(year, c.begin_date) = DATENAME(year,@begin_date)
GROUP BY c.description, c.site_code, c.INSPECTION_CODE
ORDER by c.site_code
***/SELECT @command_str = 'cube_unit_sched_events_scp'
EXEC sp_cube_et_sched_maintenance/***
INSERT INTO #tmp_et_site (site_phys_piece, ebs_code, inspection_code, description,site_hrs, site_cnt, site_avg_hrs)
SELECT @id_piece,
site_code,
inspection_code,
description,
SUM(c.outage_hours),
SUM(c.event_count),
SUM(c.outage_hours) / SUM(c.event_count)
--FROM cube_unit_sched_events_scp c,
cube_header_info hi
WHERE hi.site_phys_piece = @phys_piece
AND hi.design = @design
AND c.id_piece = hi.id_piece
AND c.begin_date <= @begin_date
AND DATENAME(year, c.begin_date) = DATENAME(year,@begin_date)
GROUP BY c.description, c.site_code, c.INSPECTION_CODE
ORDER by c.site_code
Please help, what am doing wrong?
[sig][/sig]