使用Excel中的SQL查询从Excel单元格中获取数据

时间:2017-08-08 13:32:11

标签: sql excel

我一直在寻找一种在EXCEL中使用SQL查询的方法,用户可以在excel-cell中输入数据作为查询的一部分。

该查询在MS SQL Server Managment Studio中正常工作

但是,我希望“set @orgnr =”指向工作表中的单元格。

查询如下所示:

enter code here
declare @orgnr as varchar(9)

set @orgnr = 123123123

select rating, COUNT(distinct f.dunsnr) as 'Revisjonsklienter', COUNT(distinct db_saksnr) as 'Antall saker', case when SUM(belop) IS null then 0 else SUM(belop) end as 'Sum beløp'
from t_foretak f join t_foretakverv fv on f.dunsnr = fv.dunsnr and kode in (30) and fv.offnr = @orgnr and fv.aktiv_til = 111111111
left join t_foretaknavn fn on f.dunsnr = fn.dunsnr and fn.aktiv_til=111111111
left join t_rating_data r on r.dunsnr = f.dunsnr and r.aktiv_til = 111111111
left join t_beta b on b.dunsnr = f.dunsnr 
and b.type in ('AB','AO','AR','AV','DO','FG','GF','HE','HF','ID','IN','IS','KR','LT','MF','MI','NR','RS','SO','TG','TK','TL','TP','UA','UB','UL','UP') 
and b.aktiv_til = 111111111
where f.aktiv_til = 111111111 and fn.navn_type = 1 
group by rating
order by 
case rating when 'AAA' then 0
              when 'AA' then 1                         
              when 'A' then 2
              when 'AN' then 3
              when 'B' then 4
              when 'C' then 5
              when 'IKKE RATET' then 6
              when 'INGEN-RAT' then 7
              when 'KONKURS' then 8
              when 'TVANGSOPPL' then 9
              when 'LIKVIDERT' then 10
End

0 个答案:

没有答案