Solved

extracting to a report for upload into staging tables

Posted on 2013-05-20
8
193 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 13

Expert Comment

by:Koen Van Wielink
ID: 39183595
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 13

Expert Comment

by:Koen Van Wielink
ID: 39183596
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 13

Expert Comment

by:Koen Van Wielink
ID: 39183597
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
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 

Author Comment

by:Amanda Walshaw
ID: 39183674
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
 
LVL 13

Expert Comment

by:Koen Van Wielink
ID: 39183690
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 13

Accepted Solution

by:
Koen Van Wielink earned 500 total points
ID: 39183696
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
ID: 39186462
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
ID: 39186463
great support given
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
MS SQL Update query with connected table data 3 41
Database ERD 4 32
TSQL convert date to string 4 37
SSRS Deployment problem 5 67
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…
In this article I will describe the Copy Database Wizard method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
Using examples as well as descriptions, and references to Books Online, show the documentation available for date manipulation functions and by using a select few of these functions, show how date based data can be manipulated with these functions.
Via a live example, show how to shrink a transaction log file down to a reasonable size.

820 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