Solved

extracting to a report for upload into staging tables

Posted on 2013-05-20
8
179 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
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 12

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 12

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
 

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
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.

 
LVL 12

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 12

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

If you have heard of RFC822 date formats, they can be quite a challenge in SQL Server. RFC822 is an Internet standard format for email message headers, including all dates within those headers. The RFC822 protocols are available in detail at:   ht…
The Delta outage: 650 cancelled flights, more than 1200 delayed flights, thousands of frustrated customers, tens of millions of dollars in damages – plus untold reputational damage to one of the world’s most trusted airlines. All due to a catastroph…
Viewers will learn how the fundamental information of how to create a table.
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

920 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

18 Experts available now in Live!

Get 1:1 Help Now