Wednesday, 21 August 2013

Unable to fix steps in Stored Procedure after queries?

Unable to fix steps in Stored Procedure after queries?

I Have Tables TABLE_TEXAS_2013
TABLE_DALLAS_2013,
Mapping_Table,
Summary_Table,
Area_Factor..
TABLE_TEXAS_2013 Looks Like
market_id texas_id year month value realvalue
4 1211 2013 1 36 36
6 1231 2013 1 43 99
TABLE_TEXAS_2013 Looks Like
market_id texas_id year month value realvalue
9 1218 2013 1 36 78
10 1238 2013 1 43 99
Mapping_Table
Texas_id role
1211 jr
1231 dr
1245 sr
1346 hr
Summary_Table
area year month total_value
texas 2013 1 777777
dallas 2013 1 989889
Factor_Table
area_id area year month factor
1 TEXAS 2000 1 2.38
480 DALLAS 2020 12 1
These is My code :
Begin Try Drop Table #TEMP1 End Try
Begin Catch End catch
SELECT sum(VALUE) as Strategy,a.Year,a.Month
INTO #TEMP1
FROM TABLE_TEXAS_2013 a
INNER JOIN Mapping_table B
ON a.Carline_id=b.CARLINE_ID
WHERE Role IN ('hr','sr')
GROUP BY A.Year,A.Month
ALTER TABLE #TEMP1 ADD AREA varchar(50)
UPDATE #TEMP1 SET AREA ='TEXAS'
Begin Try Drop Table #TEMP3 End Try
Begin Catch End catch
SELECT O.Total_Value,C.Year,C.Month,C.Strategy,C.AREA
INTO #TEMP3
FROM Factor_Table C
INNER JOIN #TEMP2 O
on C.Year=O.Year
AND C.Month=O.Month
and C.AREA=AREA.Media
UPDATE A
SET
A.Factor=B.Total_Value/B.Strategy,A.Total_Value=B.Total_Value,A.LastModified=sysDatetime(),A.LastModifiedBy=suser_name()
FROM AREA_fACTOR A
INNER JOIN #TEMP3 B ON
A.Year=B.Year and
A.Month=B.Month and
A.AREA=AREA.Media
UPDATE R
SET Real_Value=Value*S.Factor
FROM TABLE_TEXAS_2013 R
INNER JOIN AREA_fACTOR S
ON R.Year=S.Year
and R.Month=S.Month
The problem is i have succesfully created temp tables and generated the
steps but iam unable to fix all steps in one stored procedure.
Note: if i gives input to stores proc TABLE_TEXAS_2013 or
TABLE_DALLAS_2013 it should must and should go through every step because
FACTOR_Table consists of 2013 data or else if i give TABLE_TEXAS_2012 or
2011 it want to skip all steps and should goes to last step and want to
find update real value by using the factor in AREA_fACTOR table.. -->if
iam using TEXAS iam adding area TEXAS to one TEMP TABLE same fallows if i
use DALLAS.
-->iam mapping for hr,sr for dallas and texas..
Can anyone help me on this problem?

No comments:

Post a Comment