Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Import query into Excel

Author  Topic 

tantcu
Yak Posting Veteran

58 Posts

Posted - 2012-07-13 : 14:16:46
Hey guys,

After finishing write code for the query, now I want to import into excel so that every time I want to update the data I just need to hit "Refresh All" in the excel file. However I read something online that you can not import query to excel file if your query contains something like this :
DECLARE @current_month INT;
DECLARE @previous_month INT;
DECLARE @previous_2_months INT;
DECLARE @previous_3_months INT;

SET @current_month =CAST(DATEPART(m,getdate())as INT);
SET @previous_month =CAST( DATEPART(m, DATEADD(m, -1, getdate()))as INT);
SET @previous_2_months =CAST( DATEPART(m, DATEADD(m, -2, getdate())) as INT);
SET @previous_3_months =CAST( DATEPART(m, DATEADD(m, -3, getdate())) as INT);


Do you guys know how to fix it so that excel can read the query.

TG
Master Smack Fu Yak Hacker

6065 Posts

Posted - 2012-07-13 : 14:52:24
one option:
You could save your code as a stored procedure. Make those variables input parameters. You can set the parameter values from excel before you refresh.

Be One with the Optimizer
TG
Go to Top of Page

tantcu
Yak Posting Veteran

58 Posts

Posted - 2012-07-16 : 09:09:09
I tried your way but I still don't work, could you be more specific guidance how to import these data to Excel. I notice that every query code consist of "Declare" and "SET" the result returned negative.
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2012-07-16 : 09:56:29
if you want to export to excel data based on parameters createa procedure with above variables as parameters as TG suggested. Then inside procedure you can populate temporary table with fitered data based on passed parameters. then do an insert into excel from temporary table using OPENROWSET or any of below methods

http://www.sqlteam.com/forums/topic.asp?TOPIC_ID=49926

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page
   

- Advertisement -