P
Peter McAuley
I am using table variables created in SQL Query Anayliser & try to run them
in query so that I can export the data to Excel & refresh the query at random.
I can get the result set returned in query but the data does not export back
the results to Excel, I can go from Excel to the Query via editing & the
result set is still there.
I am at a loss as to why this is happening below is the SQL statement
if exists (select * from dbo.sysobjects where id =
object_id(N'[#SessionsByRegion
]') and OBJECTPROPERTY(id, N'IsUserTable') = 1)
drop table [#SessionsByRegion
]
create table #SessionsByRegion
( Region varchar(20),
PractitionerType varchar(20),
Session int,
Exams int
)
Insert into #SessionsByRegion
select [Region] = Case
When substring(MSEC.Postcode,1,patindex('%[0-9]%',MSEC.Postcode)-1) in
('PE','NR','IP','CB','CO','CM','SS','RM','IG') then 'East'
When substring(MSEC.Postcode,1,patindex('%[0-9]%',MSEC.Postcode)-1) in
('EC','WC','N','SE','SW','W','WD','EN','AL','MK','LU','SG','HP','E') then
'London'
When substring(MSEC.Postcode,1,patindex('%[0-9]%',MSEC.Postcode)-1) in
('CR','CT','BR','SM','RH','BN','TW','GU','KT','HA','UB','SL','NW','PO','SO','SP','TN','ME','DA') then 'South'
When substring(MSEC.Postcode,1,patindex('%[0-9]%',MSEC.Postcode)-1) in
('DE','NG','LE','CV','NN','B','HR','WR','ST','WS','DY','SY','LD','TF','WV')
then 'Midlands'
When substring(MSEC.Postcode,1,patindex('%[0-9]%',MSEC.Postcode)-1) in
('CH','LL','L','CW','WA','WN','SK','OL','M','LA','BB','PR','FY','WN','CA','BL') then 'North West'
When substring(MSEC.Postcode,1,patindex('%[0-9]%',MSEC.Postcode)-1) in
('EX','TQ','TR','PL','BH','DT','BA','BS','TA','CF','SA','NP','OX','GL','SN','RG') then 'South West'
When substring(MSEC.Postcode,1,patindex('%[0-9]%',MSEC.Postcode)-1) in
('BD','HG','LS','WF','HX','HD','YO','S','DN','LN','HU','NE','SR','DH','TS','DL') then 'North'
When substring(MSEC.Postcode,1,patindex('%[0-9]%',MSEC.Postcode)-1) in
('EH','TD','KY','FK','PH','DD','AB','IV','HS','KW','ZE','G','ML','KA','PA','DG','BT') then 'Scotland'
When MSEC.Postcode is NULL then 'No Postcode'
End,
[PractitionerType] = Case
When Ex.Exam_Dr_Resource_ID in
('7884','9363','7856','9933','9928','10012','7960','9391','9366','9955','9233','9364','9898','9893','7892','2146','6143','2296','2163','2209') Then 'OP'
When Ex.Exam_Dr_Resource_ID in
('9387','9369','9500','9367','9479','10060','9986','10010','9372','9374','9367','9959','9401','9941','9497','9377','9746','9444','10090','9788','9808','9371','9899','9489','9763','9931','9921','9917','9895','9916','9850','9759','9742','9756','9990','9495','9747','10075','9482','9484','9487','9915','9960','9498','9769','9370','9468','9469','9924','8031','10058','10098','10106') then 'OHA'
When Ex.Exam_Dr_Resource_ID in
('9400','9408','9409','9465','9404','9373','9777','9787','9393','9380','9481','9491','9499','9445','9464','9798','9375','9796','9891','9470','9492','9450','9461','9477','9814','9493','9386','9486','9415','9382','9378','9745','9853','9384','9752') then 'WPC'
When Ex.Exam_Dr_Resource_ID in ('9396','9761','9466') then 'OT'
When Ex.Exam_Dr_Resource_ID not in
('7884','9363','7856','9933','9928','10012','7960','9391','9366','9955','9233','9364','9898','9893','7892','2146','6143','2296','2163','2209','9387','9369','9500','9367','9479','10060','9986','10010','9372','9374','9367','9959','9401','9941','9497','9377','9746','9444','10090','9788','9808','9371','9899','9489','9763','9931','9921','9917','9895','9916','9850','9759','9742','9756','9990','9495','9747','10075','9482','9484','9487','9915','9960','9498','9769','9370','9468','9469','9924','8031','9400','9408','9409','9465','9404','9373','9777','9787','9393','9380','9481','9491','9499','9445','9464','9798','9375','9796','9891','9470','9492','9450','9461','9477','9814','9493','9386','9486','9415','9382','9378','9745','9853','9384','9752','9396','9761','9466','10058','10098','10106') then 'ASSOC'
end,
Session = ex.session_id,
Exams = count(ex.activity_id)
from dat_ctReferral R
inner join ref_ctProcessLookup PL on
R.ReferralType_ID = PL.ReferralType_ID and
R.ReferralSubType_ID = PL.ReferralSubType_ID and
R.Customer_ID = PL.Customer_ID
inner join ref_ctReferralSubType ST on
ST.ReferralType_id = R.ReferralType_id and
ST.ReferralSubType_id = R.ReferralSubType_id
inner join ref_ctCustomer Cus on
Cus.Customer_id = R.Customer_id
inner join ref_ctContract Con on
Cus.Contract_id = Con.Contract_id
inner join dat_ctClientReferral ClR on
ClR.Referral_id = R.Referral_id
inner join dat_ctClient Cl on
ClR.Client_id = Cl.Client_id
inner join dat_ctActivity Aty on
Aty.Referral_ID = R.Referral_ID
inner join dat_ctExam Ex on
Ex.Activity_id = Aty.Activity_id
inner join ref_ctMSC MSC on
Ex.MSC_id = MSC.MSC_id
inner join ref_ctMSEC MSEC on
Ex.MSEC_id = MSEC.MSEC_id
cross join drv_finInt_InvoicePeriod P
where Ex.ExmCancel_id is null and
Coalesce(Ex.Exam_Date_Time, Ex.Exam_Date) >= convert(datetime,
'01/05/2005', 103) and
Coalesce(Ex.Exam_Date_Time, Ex.Exam_Date) <= convert(datetime,
'01/06/2005', 103) and
Aty.StageType_ID = 'Exam' and
Con.Contract_ID <> 'COPD' and
(patindex('%[0-9]%',MSEC.Postcode) > 0 or MSEC.Postcode is Null)
group by MSEC.Postcode,
Exam_Dr_Resource_ID,
ex.session_id
select Region,
PractitionerType,
[Avergage Exams] = AVG(convert(numeric(9,3), exams))
from #SessionsByRegion
group by Region,
PractitionerType
order by region,
PractitionerType
in query so that I can export the data to Excel & refresh the query at random.
I can get the result set returned in query but the data does not export back
the results to Excel, I can go from Excel to the Query via editing & the
result set is still there.
I am at a loss as to why this is happening below is the SQL statement
if exists (select * from dbo.sysobjects where id =
object_id(N'[#SessionsByRegion
]') and OBJECTPROPERTY(id, N'IsUserTable') = 1)
drop table [#SessionsByRegion
]
create table #SessionsByRegion
( Region varchar(20),
PractitionerType varchar(20),
Session int,
Exams int
)
Insert into #SessionsByRegion
select [Region] = Case
When substring(MSEC.Postcode,1,patindex('%[0-9]%',MSEC.Postcode)-1) in
('PE','NR','IP','CB','CO','CM','SS','RM','IG') then 'East'
When substring(MSEC.Postcode,1,patindex('%[0-9]%',MSEC.Postcode)-1) in
('EC','WC','N','SE','SW','W','WD','EN','AL','MK','LU','SG','HP','E') then
'London'
When substring(MSEC.Postcode,1,patindex('%[0-9]%',MSEC.Postcode)-1) in
('CR','CT','BR','SM','RH','BN','TW','GU','KT','HA','UB','SL','NW','PO','SO','SP','TN','ME','DA') then 'South'
When substring(MSEC.Postcode,1,patindex('%[0-9]%',MSEC.Postcode)-1) in
('DE','NG','LE','CV','NN','B','HR','WR','ST','WS','DY','SY','LD','TF','WV')
then 'Midlands'
When substring(MSEC.Postcode,1,patindex('%[0-9]%',MSEC.Postcode)-1) in
('CH','LL','L','CW','WA','WN','SK','OL','M','LA','BB','PR','FY','WN','CA','BL') then 'North West'
When substring(MSEC.Postcode,1,patindex('%[0-9]%',MSEC.Postcode)-1) in
('EX','TQ','TR','PL','BH','DT','BA','BS','TA','CF','SA','NP','OX','GL','SN','RG') then 'South West'
When substring(MSEC.Postcode,1,patindex('%[0-9]%',MSEC.Postcode)-1) in
('BD','HG','LS','WF','HX','HD','YO','S','DN','LN','HU','NE','SR','DH','TS','DL') then 'North'
When substring(MSEC.Postcode,1,patindex('%[0-9]%',MSEC.Postcode)-1) in
('EH','TD','KY','FK','PH','DD','AB','IV','HS','KW','ZE','G','ML','KA','PA','DG','BT') then 'Scotland'
When MSEC.Postcode is NULL then 'No Postcode'
End,
[PractitionerType] = Case
When Ex.Exam_Dr_Resource_ID in
('7884','9363','7856','9933','9928','10012','7960','9391','9366','9955','9233','9364','9898','9893','7892','2146','6143','2296','2163','2209') Then 'OP'
When Ex.Exam_Dr_Resource_ID in
('9387','9369','9500','9367','9479','10060','9986','10010','9372','9374','9367','9959','9401','9941','9497','9377','9746','9444','10090','9788','9808','9371','9899','9489','9763','9931','9921','9917','9895','9916','9850','9759','9742','9756','9990','9495','9747','10075','9482','9484','9487','9915','9960','9498','9769','9370','9468','9469','9924','8031','10058','10098','10106') then 'OHA'
When Ex.Exam_Dr_Resource_ID in
('9400','9408','9409','9465','9404','9373','9777','9787','9393','9380','9481','9491','9499','9445','9464','9798','9375','9796','9891','9470','9492','9450','9461','9477','9814','9493','9386','9486','9415','9382','9378','9745','9853','9384','9752') then 'WPC'
When Ex.Exam_Dr_Resource_ID in ('9396','9761','9466') then 'OT'
When Ex.Exam_Dr_Resource_ID not in
('7884','9363','7856','9933','9928','10012','7960','9391','9366','9955','9233','9364','9898','9893','7892','2146','6143','2296','2163','2209','9387','9369','9500','9367','9479','10060','9986','10010','9372','9374','9367','9959','9401','9941','9497','9377','9746','9444','10090','9788','9808','9371','9899','9489','9763','9931','9921','9917','9895','9916','9850','9759','9742','9756','9990','9495','9747','10075','9482','9484','9487','9915','9960','9498','9769','9370','9468','9469','9924','8031','9400','9408','9409','9465','9404','9373','9777','9787','9393','9380','9481','9491','9499','9445','9464','9798','9375','9796','9891','9470','9492','9450','9461','9477','9814','9493','9386','9486','9415','9382','9378','9745','9853','9384','9752','9396','9761','9466','10058','10098','10106') then 'ASSOC'
end,
Session = ex.session_id,
Exams = count(ex.activity_id)
from dat_ctReferral R
inner join ref_ctProcessLookup PL on
R.ReferralType_ID = PL.ReferralType_ID and
R.ReferralSubType_ID = PL.ReferralSubType_ID and
R.Customer_ID = PL.Customer_ID
inner join ref_ctReferralSubType ST on
ST.ReferralType_id = R.ReferralType_id and
ST.ReferralSubType_id = R.ReferralSubType_id
inner join ref_ctCustomer Cus on
Cus.Customer_id = R.Customer_id
inner join ref_ctContract Con on
Cus.Contract_id = Con.Contract_id
inner join dat_ctClientReferral ClR on
ClR.Referral_id = R.Referral_id
inner join dat_ctClient Cl on
ClR.Client_id = Cl.Client_id
inner join dat_ctActivity Aty on
Aty.Referral_ID = R.Referral_ID
inner join dat_ctExam Ex on
Ex.Activity_id = Aty.Activity_id
inner join ref_ctMSC MSC on
Ex.MSC_id = MSC.MSC_id
inner join ref_ctMSEC MSEC on
Ex.MSEC_id = MSEC.MSEC_id
cross join drv_finInt_InvoicePeriod P
where Ex.ExmCancel_id is null and
Coalesce(Ex.Exam_Date_Time, Ex.Exam_Date) >= convert(datetime,
'01/05/2005', 103) and
Coalesce(Ex.Exam_Date_Time, Ex.Exam_Date) <= convert(datetime,
'01/06/2005', 103) and
Aty.StageType_ID = 'Exam' and
Con.Contract_ID <> 'COPD' and
(patindex('%[0-9]%',MSEC.Postcode) > 0 or MSEC.Postcode is Null)
group by MSEC.Postcode,
Exam_Dr_Resource_ID,
ex.session_id
select Region,
PractitionerType,
[Avergage Exams] = AVG(convert(numeric(9,3), exams))
from #SessionsByRegion
group by Region,
PractitionerType
order by region,
PractitionerType