• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 204
  • Last Modified:

extracting to a report for upload into staging tables

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
Amanda Walshaw
Asked:
Amanda Walshaw
  • 5
  • 3
1 Solution
 
Koen Van WielinkIT ConsultantCommented:
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
 
Koen Van WielinkIT ConsultantCommented:
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
 
Koen Van WielinkIT ConsultantCommented:
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
NFR key for Veeam Backup for Microsoft Office 365

Veeam is happy to provide a free NFR license (for 1 year, up to 10 users). This license allows for the non‑production use of Veeam Backup for Microsoft Office 365 in your home lab without any feature limitations.

 
Amanda WalshawBusiness Solutions AnalsystAuthor Commented:
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
 
Koen Van WielinkIT ConsultantCommented:
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
 
Koen Van WielinkIT ConsultantCommented:
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
 
Amanda WalshawBusiness Solutions AnalsystAuthor Commented:
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
 
Amanda WalshawBusiness Solutions AnalsystAuthor Commented:
great support given
0

Featured Post

Prepare for your VMware VCP6-DCV exam.

Josh Coen and Jason Langer have prepared the latest edition of VCP study guide. Both authors have been working in the IT field for more than a decade, and both hold VMware certifications. This 163-page guide covers all 10 of the exam blueprint sections.

  • 5
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now