Solved

extracting to a report for upload into staging tables

Posted on 2013-05-20
8
178 Views
Last Modified: 2013-05-21
I am using the following query to extract id numbers for inserting data, there is over 2000.
I need to extract to a populate to an excel report or export to exel the sql results.
how do you do it? I have tried saving it file but it is a sql file or crystal reports.

DECLARE @i int = 0
WHILE @i < 2497 BEGIN
    SET @i = @i + 1
    select replace(NEWID(),'-','')
END
0
Comment
Question by:Amanda Walshaw
  • 5
  • 3
8 Comments
 
LVL 12

Expert Comment

by:Koen Van Wielink
Comment Utility
Hi Flyfish,

You can create an ODBC connection in Excel. You can then add the SQL query to Excel itself. Safest way is to mold your query into a stored procedure first. See here how to create an ODBC:

http://www.depts.ttu.edu/ithelpcentral/solutions/odbcsetup.php

Or just google it, there's a million sites which describe how to. Once added, you can find the connection under the Data tab in Excel, selecting "From other sources", and the "Microsoft Query". Select your ODBC data source to get started. (For more details on how to create a query, see further below after the part which describes how to add an SQL driver connection instead of ODBC).

Alternatively you can use the SQL driver inside MS Excel so that you don't need the ODBC connection on the computer. To do this, in Excel go to the Data tab, then select "From other sources", then select "From Microsoft query".
Under Databases, select New data source". Give the data source a name (eg. the database you're connecting to). I believe this should be SQL Server, but it might be SQL native client or native client 10. Not entirely sure here.
After that, click the "connect" button. Select the server you wish to connect to. If you use Windows authentication you can select "use trusted connection". If not, untick and enter your SQL username and password. Go to "Options" before you click on OK to select the default database and, if required, language.
Under step 4 of the create new data source screen you can select a default table if you like, but this is usually not needed.
If you use SQL authentication you can choose to "save my user ID and password in the data source definition". This is however NOT recommended, as this stores your credentials unencripted in the connection string!
Once all is filled, press OK. Now you can select your new data source. Select OK. You can now use the query wizard to create your query or, my preferred way, click "cancel" and then say "yes" when asked if you want to continue to edit this query in Microsoft query.
Just close the  "add tables" window that pops up and click on the SQL button in the MS Query taskbar to write a query. You can copy/paste your query here, or if you changed it to a stored procedure type {Call "yourstoredprocedure"} including the opening and closing bracket.
If you set everything up correctly you should see the data appear in your screen. Once you click on the exit button (open door with arrow) Excel will ask you where to put the data in your Excel sheet.
Rigth clicking on the table should allow you to refresh the data again.

Hope this gets you started.
0
 
LVL 12

Expert Comment

by:Koen Van Wielink
Comment Utility
Hi Flyfish,

You can create an ODBC connection in Excel. You can then add the SQL query to Excel itself. Safest way is to mold your query into a stored procedure first. See here how to create an ODBC:

http://www.depts.ttu.edu/ithelpcentral/solutions/odbcsetup.php

Or just google it, there's a million sites which describe how to. Once added, you can find the connection under the Data tab in Excel, selecting "From other sources", and the "Microsoft Query". Select your ODBC data source to get started. (For more details on how to create a query, see further below after the part which describes how to add an SQL driver connection instead of ODBC).

Alternatively you can use the SQL driver inside MS Excel so that you don't need the ODBC connection on the computer. To do this, in Excel go to the Data tab, then select "From other sources", then select "From Microsoft query".
Under Databases, select New data source". Give the data source a name (eg. the database you're connecting to). I believe this should be SQL Server, but it might be SQL native client or native client 10. Not entirely sure here.
After that, click the "connect" button. Select the server you wish to connect to. If you use Windows authentication you can select "use trusted connection". If not, untick and enter your SQL username and password. Go to "Options" before you click on OK to select the default database and, if required, language.
Under step 4 of the create new data source screen you can select a default table if you like, but this is usually not needed.
If you use SQL authentication you can choose to "save my user ID and password in the data source definition". This is however NOT recommended, as this stores your credentials unencripted in the connection string!
Once all is filled, press OK. Now you can select your new data source. Select OK. You can now use the query wizard to create your query or, my preferred way, click "cancel" and then say "yes" when asked if you want to continue to edit this query in Microsoft query.
Just close the  "add tables" window that pops up and click on the SQL button in the MS Query taskbar to write a query. You can copy/paste your query here, or if you changed it to a stored procedure type {Call "yourstoredprocedure"} including the opening and closing bracket.
If you set everything up correctly you should see the data appear in your screen. Once you click on the exit button (open door with arrow) Excel will ask you where to put the data in your Excel sheet.
Rigth clicking on the table should allow you to refresh the data again.

Hope this gets you started.
0
 
LVL 12

Expert Comment

by:Koen Van Wielink
Comment Utility
Hi Flyfish,

You can create an ODBC connection in Excel. You can then add the SQL query to Excel itself. Safest way is to mold your query into a stored procedure first. See here how to create an ODBC:

http://www.depts.ttu.edu/ithelpcentral/solutions/odbcsetup.php

Or just google it, there's a million sites which describe how to. Once added, you can find the connection under the Data tab in Excel, selecting "From other sources", and the "Microsoft Query". Select your ODBC data source to get started. (For more details on how to create a query, see further below after the part which describes how to add an SQL driver connection instead of ODBC).

Alternatively you can use the SQL driver inside MS Excel so that you don't need the ODBC connection on the computer. To do this, in Excel go to the Data tab, then select "From other sources", then select "From Microsoft query".
Under Databases, select New data source". Give the data source a name (eg. the database you're connecting to). I believe this should be SQL Server, but it might be SQL native client or native client 10. Not entirely sure here.
After that, click the "connect" button. Select the server you wish to connect to. If you use Windows authentication you can select "use trusted connection". If not, untick and enter your SQL username and password. Go to "Options" before you click on OK to select the default database and, if required, language.
Under step 4 of the create new data source screen you can select a default table if you like, but this is usually not needed.
If you use SQL authentication you can choose to "save my user ID and password in the data source definition". This is however NOT recommended, as this stores your credentials unencripted in the connection string!
Once all is filled, press OK. Now you can select your new data source. Select OK. You can now use the query wizard to create your query or, my preferred way, click "cancel" and then say "yes" when asked if you want to continue to edit this query in Microsoft query.
Just close the  "add tables" window that pops up and click on the SQL button in the MS Query taskbar to write a query. You can copy/paste your query here, or if you changed it to a stored procedure type {Call "yourstoredprocedure"} including the opening and closing bracket.
If you set everything up correctly you should see the data appear in your screen. Once you click on the exit button (open door with arrow) Excel will ask you where to put the data in your Excel sheet.
Rigth clicking on the table should allow you to refresh the data again.

Hope this gets you started.
0
 

Author Comment

by:Amanda Walshaw
Comment Utility
thank you Kvwielink for taking the time out,  but I am not getting the 100 qty I require the ms query via excel is only generating one result, please see attached document.
Where am I going wrong?  Much appreciated on your advice so far.
Kind Regards, FlyFish
Using-exel-2010-got-connected-to.docx
0
What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

 
LVL 12

Expert Comment

by:Koen Van Wielink
Comment Utility
Hi Flyfish,

Can you try and change the query to a stored procedure, then call the stored procedure in Excel instead? MS Query is limited in what it can handle, and I suspect this command might already be too complex. If you call the procedure ABCD for instance, the syntax would be:

{Call ABCD}.

Rgds,

Kvwielnk
0
 
LVL 12

Accepted Solution

by:
Koen Van Wielink earned 500 total points
Comment Utility
Looking at your query again, if using a procedure directly doesn't work, you might also want to consider inserting your results into a temp table, then calling a select from that temp table after the while loop has completed.

Rgds,

Kvwielink
0
 

Author Comment

by:Amanda Walshaw
Comment Utility
yep got the #temp table created and ran the query

create table #temp (myg varchar(60))
 
-- run your loop inserting the values into the temp table
DECLARE @i int = 0
WHILE @i < 2497 BEGIN
    SET @i = @i + 1
    insert into #temp(myg) values (replace(NEWID(),'-',''))
END
 
that works kind regards most valuable your time on this issue.
0
 

Author Closing Comment

by:Amanda Walshaw
Comment Utility
great support given
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Occasionally there is a need to clean table columns, especially if you have inherited legacy data. There are obviously many ways to accomplish that, including elaborate UPDATE queries with anywhere from one to numerous REPLACE functions (even within…
For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
Via a live example, show how to setup several different housekeeping processes for a SQL Server.

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now