July 31, 2006 at 2:31 pm
Hi,
I have two SQLs
Select convert(varchar,DateAdd(mm, DateDiff(mm, 0, dateadd(mm, -2, Getdate())),0),112) as begindate
Select convert(varchar,DateAdd(mm, DateDiff(mm, 0, dateadd(mm, -1, Getdate())),0),112) as enddate
The output of these queries are 20060501 and 20060601. I am storing that values in begindate and enddate. I want to use these values down the stream (in sub-subqueries). I have to use these values for a very complecated query. Every month these value changes. Right now we are manually changing the dates and running the query. I got this query from this website but trying to implement in our query. I tried and it is giving me
"Server: Msg 8180, Level 16, State 1, Line 1
Statement(s) could not be prepared.
Server: Msg 137, Level 15, State 1, Line 1
Must declare the variable '@Begindate'.
[OLE/DB provider returned message: Deferred prepare could not be completed.]"
I even tried to declare those values at the top, but it is sill giving me the same error.
Could somebody please provide me some solution in passing these values down the subquries.
July 31, 2006 at 2:36 pm
Try like this:
Declare @BeginDate varchar(8)
Declare @EndDate varchar(8)
Select @BeginDate = convert(varchar,DateAdd(mm, DateDiff(mm, 0, dateadd(mm, -2, Getdate())),0),112)
Select @EndDate = convert(varchar,DateAdd(mm, DateDiff(mm, 0, dateadd(mm, -1, Getdate())),0),112)
Then you can use @BeginDate and @EndDate wherever you want.
July 31, 2006 at 2:52 pm
I tried but still same error.
"Server: Msg 8180, Level 16, State 1, Line 1
Statement(s) could not be prepared.
Server: Msg 137, Level 15, State 1, Line 1
Must declare the variable '@Begindate'.
[OLE/DB provider returned message: Deferred prepare could not be completed.]"
July 31, 2006 at 3:23 pm
try like this...
use adventureworks
SELECT h.* FROM sales.SalesOrderHeader h join
(Select
convert(varchar,DateAdd(mm, DateDiff(mm, 0, dateadd(mm, -2, Getdate())),0),112) begindate,
convert(varchar,DateAdd(mm, DateDiff(mm, 0, dateadd(mm, -1, Getdate())),0),112) enddate
) as Dt
on
h.Orderdate between dt.begindate and dt.enddate
July 31, 2006 at 4:13 pm
You will get more effective help if you submit your SQL code. It is hard to determine exactly what is causing your error if we do not know what your code looks like.
August 1, 2006 at 7:14 am
Here is the actual query. The variables we pass are used in openquery select statement where he it will be executed in another server.
Drop table My_main_table;
CREATE Table My_Main_Table
( Request_Type VARCHAR(20),
Issue_ID INT,
Submit_Date char(25),
Close_Date char (25),
Priority INT,
Days INT,
Weekend_holiday_count INT,
Act_num_of_working_days INT
)
go
Insert into My_Main_Table (Request_Type,
Issue_ID,
Submit_Date,
Close_Date,
Priority,
Days,
Weekend_holiday_count,
Act_num_of_working_days)
(select Result.Requet_Type
,Result.Issue_ID
,Result.Submit_Date
,Result.Close_Date
,Result.Priority
,Result.Days
,Result.weekend_holiday_count
,Result.Days - Result.weekend_holiday_count as Act_Num_of_working_days
from
(select Requet_Type
,init_result.Issue_ID
,Submit_Date
,Close_Date
,Priority
,CASE
WHEN DATEDIFF(d, Submit_Date,Close_Date)< 0 THEN 0
ELSE DATEDIFF(d, Submit_Date,Close_Date)
END AS DAYS
,Case count_result.weekend_holiday_count
when count_result.weekend_holiday_count then count_result.weekend_holiday_count
else 0
end as weekend_holiday_count
from openquery (team_track_server,'
SELECT CASE COD.TS_TECHARCH_REQUEST_TYPE
WHEN 3777 THEN ''Service Request''
WHEN 3778 THEN ''Issue''
WHEN 5153 THEN ''DCR Request''
END AS Requet_Type
, COD.TS_ISSUEID AS Issue_ID
, CASE
WHEN DATEPART(hour,DATEADD(s, COD.TS_SUBMITDATE, ''19700101'')) >= 15
THEN DATEADD(s, COD.TS_SUBMITDATE, ''19700102'')
ELSE DATEADD(s, COD.TS_SUBMITDATE, ''19700101'')
END AS Submit_Date
, DATEADD(s, COD.TS_SUBMITDATE, ''19700102'') AS Submit_Date
, CASE COD.TS_CLOSEDATE
WHEN COD.TS_CLOSEDATE THEN DATEADD(s, COD.TS_CLOSEDATE, ''19700101'')
ELSE getdate()
END AS Close_Date
, CASE
WHEN COD.TS_SEVERITY = 1996 AND COD.TS_TECHARCH_REQUEST_TYPE = 3778 THEN 1
WHEN COD.TS_SEVERITY = 1997 AND COD.TS_TECHARCH_REQUEST_TYPE = 3778 THEN 2
WHEN COD.TS_SEVERITY = 1998 AND COD.TS_TECHARCH_REQUEST_TYPE = 3778 THEN 2
WHEN COD.TS_SEVERITY = 1999 AND COD.TS_TECHARCH_REQUEST_TYPE = 3778 THEN 3
WHEN COD.TS_SEVERITY = 1996 AND COD.TS_TECHARCH_REQUEST_TYPE = 3777 THEN 1
WHEN COD.TS_SEVERITY = 1997 AND COD.TS_TECHARCH_REQUEST_TYPE = 3777 THEN 2
WHEN COD.TS_SEVERITY = 1998 AND COD.TS_TECHARCH_REQUEST_TYPE = 3777 THEN 3
WHEN COD.TS_SEVERITY = 1999 AND COD.TS_TECHARCH_REQUEST_TYPE = 3777 THEN 4
WHEN COD.TS_SEVERITY = 1996 AND COD.TS_TECHARCH_REQUEST_TYPE = 5153 THEN 1
WHEN COD.TS_SEVERITY = 1997 AND COD.TS_TECHARCH_REQUEST_TYPE = 5153 THEN 2
WHEN COD.TS_SEVERITY = 1998 AND COD.TS_TECHARCH_REQUEST_TYPE = 5153 THEN 3
WHEN COD.TS_SEVERITY = 1999 AND COD.TS_TECHARCH_REQUEST_TYPE = 5153 THEN 4
END as Priority
FROM teamtrack.UCO_COD_SRM COD
INNER JOIN
(SELECT DATEDIFF(s, ''19700101'', @Begindate) AS Min_Date
, DATEDIFF(s, ''19700101'', @Enddate) AS Max_Date) Date_Range
ON
COD.TS_SUBMITDATE >= Date_Range.Min_Date AND COD.TS_SUBMITDATE < Date_Range.Max_Date OR
COD.TS_CLOSEDATE >= Date_Range.Min_Date AND COD.TS_CLOSEDATE < Date_Range.Max_Date
WHERE (COD.TS_ISSUEID LIKE ''50%'')
and COD.TS_PROJECTID = 36
ORDER BY COD.TS_TECHARCH_REQUEST_TYPE , COD.TS_SEVERITY') as init_result left join
(
select count(Date_Col) as weekend_holiday_count
,Issue_ID
from openquery (team_track_server,'
SELECT CASE COD.TS_TECHARCH_REQUEST_TYPE
WHEN 3777 THEN ''Service Request''
WHEN 3778 THEN ''Issue''
WHEN 5153 THEN ''DCR Request''
END AS Requet_Type
, COD.TS_ISSUEID AS Issue_ID
, CASE
WHEN DATEPART(hour,DATEADD(s, COD.TS_SUBMITDATE, ''19700101'')) >= 15
THEN DATEADD(s, COD.TS_SUBMITDATE, ''19700102'')
ELSE DATEADD(s, COD.TS_SUBMITDATE, ''19700101'')
END AS Submit_Date
, DATEADD(s, COD.TS_SUBMITDATE, ''19700102'') AS Submit_Date
, CASE COD.TS_CLOSEDATE
WHEN COD.TS_CLOSEDATE THEN DATEADD(s, COD.TS_CLOSEDATE, ''19700101'')
ELSE getdate()
END AS Close_Date
, CASE
WHEN COD.TS_SEVERITY = 1996 AND COD.TS_TECHARCH_REQUEST_TYPE = 3778 THEN 1
WHEN COD.TS_SEVERITY = 1997 AND COD.TS_TECHARCH_REQUEST_TYPE = 3778 THEN 2
WHEN COD.TS_SEVERITY = 1998 AND COD.TS_TECHARCH_REQUEST_TYPE = 3778 THEN 2
WHEN COD.TS_SEVERITY = 1999 AND COD.TS_TECHARCH_REQUEST_TYPE = 3778 THEN 3
WHEN COD.TS_SEVERITY = 1996 AND COD.TS_TECHARCH_REQUEST_TYPE = 3777 THEN 1
WHEN COD.TS_SEVERITY = 1997 AND COD.TS_TECHARCH_REQUEST_TYPE = 3777 THEN 2
WHEN COD.TS_SEVERITY = 1998 AND COD.TS_TECHARCH_REQUEST_TYPE = 3777 THEN 3
WHEN COD.TS_SEVERITY = 1999 AND COD.TS_TECHARCH_REQUEST_TYPE = 3777 THEN 4
WHEN COD.TS_SEVERITY = 1996 AND COD.TS_TECHARCH_REQUEST_TYPE = 5153 THEN 1
WHEN COD.TS_SEVERITY = 1997 AND COD.TS_TECHARCH_REQUEST_TYPE = 5153 THEN 2
WHEN COD.TS_SEVERITY = 1998 AND COD.TS_TECHARCH_REQUEST_TYPE = 5153 THEN 3
WHEN COD.TS_SEVERITY = 1999 AND COD.TS_TECHARCH_REQUEST_TYPE = 5153 THEN 4
END as Priority
FROM teamtrack.UCO_COD_SRM COD
INNER JOIN
(SELECT DATEDIFF(s, ''19700101'', @Begindate) AS Min_Date
, DATEDIFF(s, ''19700101'', @Enddate) AS Max_Date) Date_Range
ON
COD.TS_SUBMITDATE >= Date_Range.Min_Date AND COD.TS_SUBMITDATE < Date_Range.Max_Date OR
COD.TS_CLOSEDATE >= Date_Range.Min_Date AND COD.TS_CLOSEDATE < Date_Range.Max_Date
WHERE (COD.TS_ISSUEID LIKE ''50%'')
and COD.TS_PROJECTID = 36
ORDER BY COD.TS_TECHARCH_REQUEST_TYPE , COD.TS_SEVERITY'),
performance.dbo.weekend_holiday_schedule
where Date_Col between Submit_Date and Close_Date
group by Issue_ID
) as count_result
on count_result.Issue_ID = init_result.Issue_ID) as Result)
select * from My_main_table
August 1, 2006 at 11:14 am
It's just like Julie said:
Declare @BeginDate varchar(8)
Declare @EndDate varchar(8)
Select @BeginDate = convert(varchar,DateAdd(mm, DateDiff(mm, 0, dateadd(mm, -2, Getdate())),0),112)
Select @EndDate = convert(varchar,DateAdd(mm, DateDiff(mm, 0, dateadd(mm, -1, Getdate())),0),112)
You've gotta declare and set those variables first.
August 1, 2006 at 12:39 pm
I did that. Here is my query with declaring begindate and enddate. I am getting same error.
Declare @BeginDate varchar(8)
Declare @EndDate varchar(8)
Select @BeginDate = convert(varchar,DateAdd(mm, DateDiff(mm, 0, dateadd(mm, -2, Getdate())),0),112)
Select @EndDate = convert(varchar,DateAdd(mm, DateDiff(mm, 0, dateadd(mm, -1, Getdate())),0),112)
Drop table My_main_table;
CREATE Table My_Main_Table
( Request_Type VARCHAR(20),
Issue_ID INT,
Submit_Date char(25),
Close_Date char (25),
Priority INT,
Days INT,
Weekend_holiday_count INT,
Act_num_of_working_days INT
)
go
Insert into My_Main_Table (Request_Type,
Issue_ID,
Submit_Date,
Close_Date,
Priority,
Days,
Weekend_holiday_count,
Act_num_of_working_days)
(select Result.Requet_Type
,Result.Issue_ID
,Result.Submit_Date
,Result.Close_Date
,Result.Priority
,Result.Days
,Result.weekend_holiday_count
,Result.Days - Result.weekend_holiday_count as Act_Num_of_working_days
from
(select Requet_Type
,init_result.Issue_ID
,Submit_Date
,Close_Date
,Priority
,CASE
WHEN DATEDIFF(d, Submit_Date,Close_Date)< 0 THEN 0
ELSE DATEDIFF(d, Submit_Date,Close_Date)
END AS DAYS
,Case count_result.weekend_holiday_count
when count_result.weekend_holiday_count then count_result.weekend_holiday_count
else 0
end as weekend_holiday_count
from openquery (team_track_server,'
SELECT CASE COD.TS_TECHARCH_REQUEST_TYPE
WHEN 3777 THEN ''Service Request''
WHEN 3778 THEN ''Issue''
WHEN 5153 THEN ''DCR Request''
END AS Requet_Type
, COD.TS_ISSUEID AS Issue_ID
, CASE
WHEN DATEPART(hour,DATEADD(s, COD.TS_SUBMITDATE, ''19700101'')) >= 15
THEN DATEADD(s, COD.TS_SUBMITDATE, ''19700102'')
ELSE DATEADD(s, COD.TS_SUBMITDATE, ''19700101'')
END AS Submit_Date
, DATEADD(s, COD.TS_SUBMITDATE, ''19700102'') AS Submit_Date
, CASE COD.TS_CLOSEDATE
WHEN COD.TS_CLOSEDATE THEN DATEADD(s, COD.TS_CLOSEDATE, ''19700101'')
ELSE getdate()
END AS Close_Date
, CASE
WHEN COD.TS_SEVERITY = 1996 AND COD.TS_TECHARCH_REQUEST_TYPE = 3778 THEN 1
WHEN COD.TS_SEVERITY = 1997 AND COD.TS_TECHARCH_REQUEST_TYPE = 3778 THEN 2
WHEN COD.TS_SEVERITY = 1998 AND COD.TS_TECHARCH_REQUEST_TYPE = 3778 THEN 2
WHEN COD.TS_SEVERITY = 1999 AND COD.TS_TECHARCH_REQUEST_TYPE = 3778 THEN 3
WHEN COD.TS_SEVERITY = 1996 AND COD.TS_TECHARCH_REQUEST_TYPE = 3777 THEN 1
WHEN COD.TS_SEVERITY = 1997 AND COD.TS_TECHARCH_REQUEST_TYPE = 3777 THEN 2
WHEN COD.TS_SEVERITY = 1998 AND COD.TS_TECHARCH_REQUEST_TYPE = 3777 THEN 3
WHEN COD.TS_SEVERITY = 1999 AND COD.TS_TECHARCH_REQUEST_TYPE = 3777 THEN 4
WHEN COD.TS_SEVERITY = 1996 AND COD.TS_TECHARCH_REQUEST_TYPE = 5153 THEN 1
WHEN COD.TS_SEVERITY = 1997 AND COD.TS_TECHARCH_REQUEST_TYPE = 5153 THEN 2
WHEN COD.TS_SEVERITY = 1998 AND COD.TS_TECHARCH_REQUEST_TYPE = 5153 THEN 3
WHEN COD.TS_SEVERITY = 1999 AND COD.TS_TECHARCH_REQUEST_TYPE = 5153 THEN 4
END as Priority
FROM teamtrack.UCO_COD_SRM COD
INNER JOIN
(SELECT DATEDIFF(s, ''19700101'', @@Begindate) AS Min_Date
, DATEDIFF(s, ''19700101'', @@Enddate) AS Max_Date) Date_Range
ON
COD.TS_SUBMITDATE >= Date_Range.Min_Date AND COD.TS_SUBMITDATE < Date_Range.Max_Date OR
COD.TS_CLOSEDATE >= Date_Range.Min_Date AND COD.TS_CLOSEDATE < Date_Range.Max_Date
WHERE (COD.TS_ISSUEID LIKE ''50%'')
and COD.TS_PROJECTID = 36
ORDER BY COD.TS_TECHARCH_REQUEST_TYPE , COD.TS_SEVERITY') as init_result left join
(
select count(Date_Col) as weekend_holiday_count
,Issue_ID
from openquery (team_track_server,'
SELECT CASE COD.TS_TECHARCH_REQUEST_TYPE
WHEN 3777 THEN ''Service Request''
WHEN 3778 THEN ''Issue''
WHEN 5153 THEN ''DCR Request''
END AS Requet_Type
, COD.TS_ISSUEID AS Issue_ID
, CASE
WHEN DATEPART(hour,DATEADD(s, COD.TS_SUBMITDATE, ''19700101'')) >= 15
THEN DATEADD(s, COD.TS_SUBMITDATE, ''19700102'')
ELSE DATEADD(s, COD.TS_SUBMITDATE, ''19700101'')
END AS Submit_Date
, DATEADD(s, COD.TS_SUBMITDATE, ''19700102'') AS Submit_Date
, CASE COD.TS_CLOSEDATE
WHEN COD.TS_CLOSEDATE THEN DATEADD(s, COD.TS_CLOSEDATE, ''19700101'')
ELSE getdate()
END AS Close_Date
, CASE
WHEN COD.TS_SEVERITY = 1996 AND COD.TS_TECHARCH_REQUEST_TYPE = 3778 THEN 1
WHEN COD.TS_SEVERITY = 1997 AND COD.TS_TECHARCH_REQUEST_TYPE = 3778 THEN 2
WHEN COD.TS_SEVERITY = 1998 AND COD.TS_TECHARCH_REQUEST_TYPE = 3778 THEN 2
WHEN COD.TS_SEVERITY = 1999 AND COD.TS_TECHARCH_REQUEST_TYPE = 3778 THEN 3
WHEN COD.TS_SEVERITY = 1996 AND COD.TS_TECHARCH_REQUEST_TYPE = 3777 THEN 1
WHEN COD.TS_SEVERITY = 1997 AND COD.TS_TECHARCH_REQUEST_TYPE = 3777 THEN 2
WHEN COD.TS_SEVERITY = 1998 AND COD.TS_TECHARCH_REQUEST_TYPE = 3777 THEN 3
WHEN COD.TS_SEVERITY = 1999 AND COD.TS_TECHARCH_REQUEST_TYPE = 3777 THEN 4
WHEN COD.TS_SEVERITY = 1996 AND COD.TS_TECHARCH_REQUEST_TYPE = 5153 THEN 1
WHEN COD.TS_SEVERITY = 1997 AND COD.TS_TECHARCH_REQUEST_TYPE = 5153 THEN 2
WHEN COD.TS_SEVERITY = 1998 AND COD.TS_TECHARCH_REQUEST_TYPE = 5153 THEN 3
WHEN COD.TS_SEVERITY = 1999 AND COD.TS_TECHARCH_REQUEST_TYPE = 5153 THEN 4
END as Priority
FROM teamtrack.UCO_COD_SRM COD
INNER JOIN
(SELECT DATEDIFF(s, ''19700101'', @Begindate) AS Min_Date
, DATEDIFF(s, ''19700101'', @Enddate) AS Max_Date) Date_Range
ON
COD.TS_SUBMITDATE >= Date_Range.Min_Date AND COD.TS_SUBMITDATE < Date_Range.Max_Date OR
COD.TS_CLOSEDATE >= Date_Range.Min_Date AND COD.TS_CLOSEDATE < Date_Range.Max_Date
WHERE (COD.TS_ISSUEID LIKE ''50%'')
and COD.TS_PROJECTID = 36
ORDER BY COD.TS_TECHARCH_REQUEST_TYPE , COD.TS_SEVERITY'),
performance.dbo.weekend_holiday_schedule
where Date_Col between Submit_Date and Close_Date
group by Issue_ID
) as count_result
on count_result.Issue_ID = init_result.Issue_ID) as Result)
select * from My_main_table
August 1, 2006 at 12:57 pm
Try it this way with only one declare and commas separating your two variables
Declare @BeginDate varchar(8),
@EndDate varchar(8)
Select @BeginDate = convert(varchar,DateAdd(mm, DateDiff(mm, 0, dateadd(mm, -2, Getdate())),0),112)
Select @EndDate = convert(varchar,DateAdd(mm, DateDiff(mm, 0, dateadd(mm, -1, Getdate())),0),112)
August 1, 2006 at 1:00 pm
Does the query parse out okay (you know, if you hit the check box icon)?
August 1, 2006 at 1:13 pm
Query works fine if I hardcode the values Begindate and enddate with "20060501" and "20060601".
By the way I even tried the last query posted here by PAM and it didn't work. I mean same error.
I think for the openquery (where u execute the query in another server) might have to pass variables in different way,,,,, I don't know.... Just guess....
August 1, 2006 at 1:19 pm
Suresh,
You need to declare the variables after the 'go' statement. The 'go' statement signals the end of a sql batch and closes the scope for those variables. If you move the declare statements within the scope that you are using the variables (i.e. after the 'go' statement), you should stop getting that error.
Regards,
Scott
August 1, 2006 at 2:20 pm
Hi Scott,
i tried that too. It didn't work and same error encountered
August 1, 2006 at 2:53 pm
Hi Guys,
Thanks much for all ur effort. Your ideas brought me to think in different ways to work it out and it worked. I executed that sql statements in the SUB-query itself and it worked. Below is the complete query.
Drop table My_main_table;
CREATE Table My_Main_Table
( Request_Type VARCHAR(20),
Issue_ID INT,
Submit_Date char(25),
Close_Date char (25),
Priority INT,
Days INT,
Weekend_holiday_count INT,
Act_num_of_working_days INT
)
go
Insert into My_Main_Table (Request_Type,
Issue_ID,
Submit_Date,
Close_Date,
Priority,
Days,
Weekend_holiday_count,
Act_num_of_working_days)
(select Result.Requet_Type
,Result.Issue_ID
,Result.Submit_Date
,Result.Close_Date
,Result.Priority
,Result.Days
,Result.weekend_holiday_count
,Result.Days - Result.weekend_holiday_count as Act_Num_of_working_days
from
(select Requet_Type
,init_result.Issue_ID
,Submit_Date
,Close_Date
,Priority
,CASE
WHEN DATEDIFF(d, Submit_Date,Close_Date)< 0 THEN 0
ELSE DATEDIFF(d, Submit_Date,Close_Date)
END AS DAYS
,Case count_result.weekend_holiday_count
when count_result.weekend_holiday_count then count_result.weekend_holiday_count
else 0
end as weekend_holiday_count
from openquery (team_track_server,'
SELECT CASE COD.TS_TECHARCH_REQUEST_TYPE
WHEN 3777 THEN ''Service Request''
WHEN 3778 THEN ''Issue''
WHEN 5153 THEN ''DCR Request''
END AS Requet_Type
, COD.TS_ISSUEID AS Issue_ID
, CASE
WHEN DATEPART(hour,DATEADD(s, COD.TS_SUBMITDATE, ''19700101'')) >= 15
THEN DATEADD(s, COD.TS_SUBMITDATE, ''19700102'')
ELSE DATEADD(s, COD.TS_SUBMITDATE, ''19700101'')
END AS Submit_Date
, DATEADD(s, COD.TS_SUBMITDATE, ''19700102'') AS Submit_Date
, CASE COD.TS_CLOSEDATE
WHEN COD.TS_CLOSEDATE THEN DATEADD(s, COD.TS_CLOSEDATE, ''19700101'')
ELSE getdate()
END AS Close_Date
, CASE
WHEN COD.TS_SEVERITY = 1996 AND COD.TS_TECHARCH_REQUEST_TYPE = 3778 THEN 1
WHEN COD.TS_SEVERITY = 1997 AND COD.TS_TECHARCH_REQUEST_TYPE = 3778 THEN 2
WHEN COD.TS_SEVERITY = 1998 AND COD.TS_TECHARCH_REQUEST_TYPE = 3778 THEN 2
WHEN COD.TS_SEVERITY = 1999 AND COD.TS_TECHARCH_REQUEST_TYPE = 3778 THEN 3
WHEN COD.TS_SEVERITY = 1996 AND COD.TS_TECHARCH_REQUEST_TYPE = 3777 THEN 1
WHEN COD.TS_SEVERITY = 1997 AND COD.TS_TECHARCH_REQUEST_TYPE = 3777 THEN 2
WHEN COD.TS_SEVERITY = 1998 AND COD.TS_TECHARCH_REQUEST_TYPE = 3777 THEN 3
WHEN COD.TS_SEVERITY = 1999 AND COD.TS_TECHARCH_REQUEST_TYPE = 3777 THEN 4
WHEN COD.TS_SEVERITY = 1996 AND COD.TS_TECHARCH_REQUEST_TYPE = 5153 THEN 1
WHEN COD.TS_SEVERITY = 1997 AND COD.TS_TECHARCH_REQUEST_TYPE = 5153 THEN 2
WHEN COD.TS_SEVERITY = 1998 AND COD.TS_TECHARCH_REQUEST_TYPE = 5153 THEN 3
WHEN COD.TS_SEVERITY = 1999 AND COD.TS_TECHARCH_REQUEST_TYPE = 5153 THEN 4
END as Priority
FROM teamtrack.UCO_COD_SRM COD
INNER JOIN
(SELECT DATEDIFF(s, ''19700101'', convert(varchar,DateAdd(mm, DateDiff(mm, 0, dateadd(mm, -2, Getdate())),0),112)) AS Min_Date
, DATEDIFF(s, ''19700101'', Drop table My_main_table;
CREATE Table My_Main_Table
( Request_Type VARCHAR(20),
Issue_ID INT,
Submit_Date char(25),
Close_Date char (25),
Priority INT,
Days INT,
Weekend_holiday_count INT,
Act_num_of_working_days INT
)
go
Insert into My_Main_Table (Request_Type,
Issue_ID,
Submit_Date,
Close_Date,
Priority,
Days,
Weekend_holiday_count,
Act_num_of_working_days)
(select Result.Requet_Type
,Result.Issue_ID
,Result.Submit_Date
,Result.Close_Date
,Result.Priority
,Result.Days
,Result.weekend_holiday_count
,Result.Days - Result.weekend_holiday_count as Act_Num_of_working_days
from
(select Requet_Type
,init_result.Issue_ID
,Submit_Date
,Close_Date
,Priority
,CASE
WHEN DATEDIFF(d, Submit_Date,Close_Date)< 0 THEN 0
ELSE DATEDIFF(d, Submit_Date,Close_Date)
END AS DAYS
,Case count_result.weekend_holiday_count
when count_result.weekend_holiday_count then count_result.weekend_holiday_count
else 0
end as weekend_holiday_count
from openquery (team_track_server,'
SELECT CASE COD.TS_TECHARCH_REQUEST_TYPE
WHEN 3777 THEN ''Service Request''
WHEN 3778 THEN ''Issue''
WHEN 5153 THEN ''DCR Request''
END AS Requet_Type
, COD.TS_ISSUEID AS Issue_ID
, CASE
WHEN DATEPART(hour,DATEADD(s, COD.TS_SUBMITDATE, ''19700101'')) >= 15
THEN DATEADD(s, COD.TS_SUBMITDATE, ''19700102'')
ELSE DATEADD(s, COD.TS_SUBMITDATE, ''19700101'')
END AS Submit_Date
, DATEADD(s, COD.TS_SUBMITDATE, ''19700102'') AS Submit_Date
, CASE COD.TS_CLOSEDATE
WHEN COD.TS_CLOSEDATE THEN DATEADD(s, COD.TS_CLOSEDATE, ''19700101'')
ELSE getdate()
END AS Close_Date
, CASE
WHEN COD.TS_SEVERITY = 1996 AND COD.TS_TECHARCH_REQUEST_TYPE = 3778 THEN 1
WHEN COD.TS_SEVERITY = 1997 AND COD.TS_TECHARCH_REQUEST_TYPE = 3778 THEN 2
WHEN COD.TS_SEVERITY = 1998 AND COD.TS_TECHARCH_REQUEST_TYPE = 3778 THEN 2
WHEN COD.TS_SEVERITY = 1999 AND COD.TS_TECHARCH_REQUEST_TYPE = 3778 THEN 3
WHEN COD.TS_SEVERITY = 1996 AND COD.TS_TECHARCH_REQUEST_TYPE = 3777 THEN 1
WHEN COD.TS_SEVERITY = 1997 AND COD.TS_TECHARCH_REQUEST_TYPE = 3777 THEN 2
WHEN COD.TS_SEVERITY = 1998 AND COD.TS_TECHARCH_REQUEST_TYPE = 3777 THEN 3
WHEN COD.TS_SEVERITY = 1999 AND COD.TS_TECHARCH_REQUEST_TYPE = 3777 THEN 4
WHEN COD.TS_SEVERITY = 1996 AND COD.TS_TECHARCH_REQUEST_TYPE = 5153 THEN 1
WHEN COD.TS_SEVERITY = 1997 AND COD.TS_TECHARCH_REQUEST_TYPE = 5153 THEN 2
WHEN COD.TS_SEVERITY = 1998 AND COD.TS_TECHARCH_REQUEST_TYPE = 5153 THEN 3
WHEN COD.TS_SEVERITY = 1999 AND COD.TS_TECHARCH_REQUEST_TYPE = 5153 THEN 4
END as Priority
FROM teamtrack.UCO_COD_SRM COD
INNER JOIN
(SELECT DATEDIFF(s, ''19700101'', convert(varchar,DateAdd(mm, DateDiff(mm, 0, dateadd(mm, -2, Getdate())),0),112)) AS Min_Date
, DATEDIFF(s, ''19700101'', convert(varchar,DateAdd(mm, DateDiff(mm, 0, dateadd(mm, -1, Getdate())),0),112)) AS Max_Date) Date_Range
ON
COD.TS_SUBMITDATE >= Date_Range.Min_Date AND COD.TS_SUBMITDATE < Date_Range.Max_Date OR
COD.TS_CLOSEDATE >= Date_Range.Min_Date AND COD.TS_CLOSEDATE < Date_Range.Max_Date
WHERE (COD.TS_ISSUEID LIKE ''50%'')
and COD.TS_PROJECTID = 36
ORDER BY COD.TS_TECHARCH_REQUEST_TYPE , COD.TS_SEVERITY') as init_result left join
(
select count(Date_Col) as weekend_holiday_count
,Issue_ID
from openquery (team_track_server,'
SELECT CASE COD.TS_TECHARCH_REQUEST_TYPE
WHEN 3777 THEN ''Service Request''
WHEN 3778 THEN ''Issue''
WHEN 5153 THEN ''DCR Request''
END AS Requet_Type
, COD.TS_ISSUEID AS Issue_ID
, CASE
WHEN DATEPART(hour,DATEADD(s, COD.TS_SUBMITDATE, ''19700101'')) >= 15
THEN DATEADD(s, COD.TS_SUBMITDATE, ''19700102'')
ELSE DATEADD(s, COD.TS_SUBMITDATE, ''19700101'')
END AS Submit_Date
, DATEADD(s, COD.TS_SUBMITDATE, ''19700102'') AS Submit_Date
, CASE COD.TS_CLOSEDATE
WHEN COD.TS_CLOSEDATE THEN DATEADD(s, COD.TS_CLOSEDATE, ''19700101'')
ELSE getdate()
END AS Close_Date
, CASE
WHEN COD.TS_SEVERITY = 1996 AND COD.TS_TECHARCH_REQUEST_TYPE = 3778 THEN 1
WHEN COD.TS_SEVERITY = 1997 AND COD.TS_TECHARCH_REQUEST_TYPE = 3778 THEN 2
WHEN COD.TS_SEVERITY = 1998 AND COD.TS_TECHARCH_REQUEST_TYPE = 3778 THEN 2
WHEN COD.TS_SEVERITY = 1999 AND COD.TS_TECHARCH_REQUEST_TYPE = 3778 THEN 3
WHEN COD.TS_SEVERITY = 1996 AND COD.TS_TECHARCH_REQUEST_TYPE = 3777 THEN 1
WHEN COD.TS_SEVERITY = 1997 AND COD.TS_TECHARCH_REQUEST_TYPE = 3777 THEN 2
WHEN COD.TS_SEVERITY = 1998 AND COD.TS_TECHARCH_REQUEST_TYPE = 3777 THEN 3
WHEN COD.TS_SEVERITY = 1999 AND COD.TS_TECHARCH_REQUEST_TYPE = 3777 THEN 4
WHEN COD.TS_SEVERITY = 1996 AND COD.TS_TECHARCH_REQUEST_TYPE = 5153 THEN 1
WHEN COD.TS_SEVERITY = 1997 AND COD.TS_TECHARCH_REQUEST_TYPE = 5153 THEN 2
WHEN COD.TS_SEVERITY = 1998 AND COD.TS_TECHARCH_REQUEST_TYPE = 5153 THEN 3
WHEN COD.TS_SEVERITY = 1999 AND COD.TS_TECHARCH_REQUEST_TYPE = 5153 THEN 4
END as Priority
FROM teamtrack.UCO_COD_SRM COD
INNER JOIN
(SELECT DATEDIFF(s, ''19700101'', convert(varchar,DateAdd(mm, DateDiff(mm, 0, dateadd(mm, -2, Getdate())),0),112)) AS Min_Date
, DATEDIFF(s, ''19700101'', convert(varchar,DateAdd(mm, DateDiff(mm, 0, dateadd(mm, -1, Getdate())),0),112)) AS Max_Date) Date_Range
ON
COD.TS_SUBMITDATE >= Date_Range.Min_Date AND COD.TS_SUBMITDATE < Date_Range.Max_Date OR
COD.TS_CLOSEDATE >= Date_Range.Min_Date AND COD.TS_CLOSEDATE < Date_Range.Max_Date
WHERE (COD.TS_ISSUEID LIKE ''50%'')
and COD.TS_PROJECTID = 36
ORDER BY COD.TS_TECHARCH_REQUEST_TYPE , COD.TS_SEVERITY'),
performance.dbo.weekend_holiday_schedule
where Date_Col between Submit_Date and Close_Date
group by Issue_ID
) as count_result
on count_result.Issue_ID = init_result.Issue_ID) as Result)
select * from My_main_table)) AS Max_Date) Date_Range
ON
COD.TS_SUBMITDATE >= Date_Range.Min_Date AND COD.TS_SUBMITDATE < Date_Range.Max_Date OR
COD.TS_CLOSEDATE >= Date_Range.Min_Date AND COD.TS_CLOSEDATE < Date_Range.Max_Date
WHERE (COD.TS_ISSUEID LIKE ''50%'')
and COD.TS_PROJECTID = 36
ORDER BY COD.TS_TECHARCH_REQUEST_TYPE , COD.TS_SEVERITY') as init_result left join
(
select count(Date_Col) as weekend_holiday_count
,Issue_ID
from openquery (team_track_server,'
SELECT CASE COD.TS_TECHARCH_REQUEST_TYPE
WHEN 3777 THEN ''Service Request''
WHEN 3778 THEN ''Issue''
WHEN 5153 THEN ''DCR Request''
END AS Requet_Type
, COD.TS_ISSUEID AS Issue_ID
, CASE
WHEN DATEPART(hour,DATEADD(s, COD.TS_SUBMITDATE, ''19700101'')) >= 15
THEN DATEADD(s, COD.TS_SUBMITDATE, ''19700102'')
ELSE DATEADD(s, COD.TS_SUBMITDATE, ''19700101'')
END AS Submit_Date
, DATEADD(s, COD.TS_SUBMITDATE, ''19700102'') AS Submit_Date
, CASE COD.TS_CLOSEDATE
WHEN COD.TS_CLOSEDATE THEN DATEADD(s, COD.TS_CLOSEDATE, ''19700101'')
ELSE getdate()
END AS Close_Date
, CASE
WHEN COD.TS_SEVERITY = 1996 AND COD.TS_TECHARCH_REQUEST_TYPE = 3778 THEN 1
WHEN COD.TS_SEVERITY = 1997 AND COD.TS_TECHARCH_REQUEST_TYPE = 3778 THEN 2
WHEN COD.TS_SEVERITY = 1998 AND COD.TS_TECHARCH_REQUEST_TYPE = 3778 THEN 2
WHEN COD.TS_SEVERITY = 1999 AND COD.TS_TECHARCH_REQUEST_TYPE = 3778 THEN 3
WHEN COD.TS_SEVERITY = 1996 AND COD.TS_TECHARCH_REQUEST_TYPE = 3777 THEN 1
WHEN COD.TS_SEVERITY = 1997 AND COD.TS_TECHARCH_REQUEST_TYPE = 3777 THEN 2
WHEN COD.TS_SEVERITY = 1998 AND COD.TS_TECHARCH_REQUEST_TYPE = 3777 THEN 3
WHEN COD.TS_SEVERITY = 1999 AND COD.TS_TECHARCH_REQUEST_TYPE = 3777 THEN 4
WHEN COD.TS_SEVERITY = 1996 AND COD.TS_TECHARCH_REQUEST_TYPE = 5153 THEN 1
WHEN COD.TS_SEVERITY = 1997 AND COD.TS_TECHARCH_REQUEST_TYPE = 5153 THEN 2
WHEN COD.TS_SEVERITY = 1998 AND COD.TS_TECHARCH_REQUEST_TYPE = 5153 THEN 3
WHEN COD.TS_SEVERITY = 1999 AND COD.TS_TECHARCH_REQUEST_TYPE = 5153 THEN 4
END as Priority
FROM teamtrack.UCO_COD_SRM COD
INNER JOIN
(SELECT DATEDIFF(s, ''19700101'', convert(varchar,DateAdd(mm, DateDiff(mm, 0, dateadd(mm, -2, Getdate())),0),112)) AS Min_Date
, DATEDIFF(s, ''19700101'', convert(varchar,DateAdd(mm, DateDiff(mm, 0, dateadd(mm, -1, Getdate())),0),112)) AS Max_Date) Date_Range
ON
COD.TS_SUBMITDATE >= Date_Range.Min_Date AND COD.TS_SUBMITDATE < Date_Range.Max_Date OR
COD.TS_CLOSEDATE >= Date_Range.Min_Date AND COD.TS_CLOSEDATE < Date_Range.Max_Date
WHERE (COD.TS_ISSUEID LIKE ''50%'')
and COD.TS_PROJECTID = 36
ORDER BY COD.TS_TECHARCH_REQUEST_TYPE , COD.TS_SEVERITY'),
performance.dbo.weekend_holiday_schedule
where Date_Col between Submit_Date and Close_Date
group by Issue_ID
) as count_result
on count_result.Issue_ID = init_result.Issue_ID) as Result)
select * from My_main_table
August 2, 2006 at 3:21 pm
The issue is that you have a GO statement partway down your code. This effectively ends the batch and all variables are then forgotton. I would have to ask why your are using permanent tables for this process rather than temporary tables and if you need to use permanent tables then you should simply be truncating them rather than dropping and creating, which are expensive operations anyway.
Chhers,
Phippsey
Viewing 15 posts - 1 through 14 (of 14 total)
You must be logged in to reply to this topic. Login to reply