How can I select this in Microsoft Query?

364 Views Asked by At

I have a stored procedure and a simple table.

I need to join this two object then allow user to see the result using Microsoft Query in Excel.

This is what I have. The Exec SP_Budget create global temp table and fill ##tmpBudget

exec SP_Budget;
Select g.name, g.address,g.Amount,b.BudgetAmt from gTable g
Left join ##tmpBudget b on b.NameID=g.NameID

Microsoft query can only do a simple

select * from tTable 

how can I do this?

I have to have the stored procedure because it does UNpivot inside the SP_Budget

EDIT: The more I think about this. I think I need to post the original SP_Budget so here it is. Because maybe a better approach is to create a function rather than SP_Budget. Here is my SP_Budget. Is it possible to convert this SP to a function?

--this link show me how to build column list for the PIVOT/UNPIVOT http://stackoverflow.com/questions/9585094/sql-server-pivots-displaying-row-values-to-column-headers
--this link show me how to build column list of a specific table http://stackoverflow.com/questions/18775409/unpivot-with-dynamic-columns-plus-column-names
--here we build the dynamic query for the column list for the PIVOT/UNPIVOT 
declare @sql  AS NVARCHAR(MAX);
declare @cols nvarchar(max);
select @cols = coalesce(@cols+N',', N'') + quotename(c.name) from syscolumns c
inner join sysobjects o on c.id = o.id and o.xtype = 'u'
where o.name = 'AcctHist' and c.name not in ('PID', 'UID') and c.name like 'ptdbal%' and  c.name <> 'PtdBal12' order by c.colid

--Construct the full T-SQL statement
--and execute dynamically
SET @sql = N'select 
DB,Acct,Sub,cpnyid
,Fiscyr+
CASE WHEN len(Convert(varchar(2),CONVERT(int,right(Period,2))+1))=1
THEN ''0''+Convert(varchar(2),CONVERT(int,right(Period,2))+1) 
ELSE Convert(varchar(2),CONVERT(int,right(Period,2))+1)
END "Period"
,Amounts
from (
SELECT  A.DB,A.Sub,A.acct,A.FiscYr,A.CpnyID
, sum(A.PtdBal00) "PtdBal00" 
,sum(A.PtdBal01) "PtdBal01"
,sum(A.PtdBal02) "PtdBal02"
,sum(A.PtdBal03) "PtdBal03"
,sum(A.PtdBal04) "PtdBal04"
,sum(A.PtdBal05) "PtdBal05"
,sum(A.PtdBal06) "PtdBal06"
,sum(A.PtdBal07) "PtdBal07"
,sum(A.PtdBal08) "PtdBal08"
,sum(A.PtdBal09) "PtdBal09"
,sum(A.PtdBal10) "PtdBal10"
,sum(A.PtdBal11) "PtdBal11"
FROM vz_Finance_Budget A
Group by   A.DB,A.Sub,A.acct,A.FiscYr,A.CpnyID
)xx
UNPIVOT (Amounts for Period in ('+@cols+')) unpiv;';
EXEC sp_executesql @sql;

Edit2: Thank you for all the suggestions. I decided that the limiting factor is microsoft query itself. Plus microsoft query should be replaced with something newer (I think). Instead of configuring my server to allow openrowset or OPENQuery. I will look into microsoft query replacement. not sure if this is the right way to go. but I will update here. thanks.

Edit3: Trying this blog now: http://blogs.office.com/2010/06/07/running-a-sql-stored-procedure-from-excel-no-vba/ I will update when I complete it

2

There are 2 best solutions below

0
On BEST ANSWER

I solved this using this blog below http://blogs.office.com/2010/06/07/running-a-sql-stored-procedure-from-excel-no-vba/

basically in excel, when you click data tab - From other sources - instead of "from microsoft query". I select "from SQL server". With "from SQL Server". I have the option in the "Connection properties - definition" to select the command type to SQL (instead of table)

the blog explains this with screenshot... Thanks

1
On

As far as I know you can have explicit JOIN in Microsoft Query

Description of the usage of joins in Microsoft Query

SELECT ....
FROM tbl1, tbl2
WHERE tbl1.Id = tbl2.Id

Update

If you want to have the result set in Excel, You can use OPENROWSET to export the data from SQL Server to Excel.

INSERT INTO OPENROWSET('Microsoft.Jet.OLEDB.4.0', 
'Excel 8.0;Database=C:\testing.xls;', 
'SELECT name, address, Amount, BudgetAmt FROM [Sheet1$]') 
SELECT g.name, g.address, g.Amount, b.BudgetAmt FROM gTable g
LEFT JOIN #tmpBudget b ON b.NameID = g.NameID

As you just mentioned earlier, you need to save your proc result into a temp table first

sp_configure 'Show Advanced Options', 1
GO
RECONFIGURE
GO
sp_configure 'Ad Hoc Distributed Queries', 1
GO
RECONFIGURE
GO

SELECT * INTO #tmpBudget
FROM OPENROWSET('SQLNCLI'
                ,'Server=(local)\SQL2008;Trusted_Connection=yes;'
                ,'EXEC SP_Budget')