I just started working with MS SQL Server Management Studio to run a repetitive task of:
Change the source table in the FROM section
Execute the query
Save the file as csv that can be opened in grid form in MS Excel
I know this is far-fetch, but is it possible for me to write a query in a form of:
list = {a,b,...,n}
FOR i = 1 to n
SELECT *
FROM [Server].[list{i}] as Table
WHERE *conditions*
SAVE AS list{i}.csv
NEXT i
Thanks!
Just make sure you get yourself a read-only user so you can't drop tables or delete data or anything.
There are tons of examples and tutorials for using Excel ODBC mssql like these:
https://www.youtube.com/watch?v=joi2HTh45YQ
Related
using the Pentaho ETL tool I am trying to get an SQL query result dumped into an excel file output. I have created an excel file and saved it but the transformation executes but does not update the excel file. I am trying to insert select * from table A into the excel file.
The connection to the database is fine as I have other transformations.
The two steps are execute SQL script which I have tested in isolation and works, the second is the Microsoft excel output which is not working.
Any suggestions?
Use Table input instead of Execute SQL script. Execute SQL script doesn't give output to next steps
I'm a beginner and in class we're learning how to make tables, insert records, make foreign keys all through query.
I've made a 3 tables through query but now I have to edit a previous table through it's own query but I closed it already. How do I open it again so I can add something to the table? All I see is the "New Query" button.
The tables are all in one file, under one database.To be more specific, I want to open query window that shows up when you click "New Query" but I want to see the codes I wrote to make the table because I need to edit something there.
You won't be able to re-open closed table script if it has not been saved in a .sql file. Check Kyle's comment above for single table. Re-generate the script.
For more than one table, right click on Database name, Select Tasks->Generate Scripts-->Click Next --> Select all tables you want. Save script on clipboard. Paste in New Query. Save that file as SQL.
You can use advanced options also to generate the same script.
If you have saved your query earlier then open the folder which contains the sql code. Double click on that file, it will automatically open in SQL Server Management Studio.
I am using OPENROWSET and Microsoft.ACE.OLEDB.12.0 to retrieve data in SSMS from an Excel 2007 (.xlsx) file. When I execute the query I only get the title of the first column (Excel cell A1) and F2, F3, etc. for the rest of the columns and no data within the table. The Excel file contains data; however, OPENROWSET is not retrieving anything other than cell A1.
I have tried both
SELECT *
FROM OPENROWSET('Microsoft.ACE.OLEDB.12.0','Excel 12.0;Database=C:\filepath.xlsx','SELECT * FROM [Sheet1$]')
and
SELECT *
FROM OPENROWSET('Microsoft.ACE.OLEDB.12.0','Excel 12.0;Database=C:\filepath.xlsx',[Sheet1$])
I get the same result for both queries.
I have also tried using the Import/Export Wizard (64-bit) and when I am on the 'Select Source Tables and Views' and use Edit Mappings... I receive the following error:
Column information for the source and destination data could not be retrieved
-Unexpected error from external database driver(1).
**Additional information**
->Unexpected error from external database driver (1).(Microsoft Access Database Engine)
I have a 64-bit version of SQL Server that is being used. Also, this is an Excel file that I receive from an outside source. The other Excel files I receive from outside sources do not give me any problems when retrieving data using the same method. Additionally, I am using SQL Server 2008 R2 and the Excel file is located on the same drive as the SQL Server.
Any help is greatly appreciated. It would be nice if I could get this working so I can handle moving the data from these files over to a SSIS package.
I have created a table in sql server 2008 and the table contains a lots of records.I need to get all the data from that table to a script file.
If you have SSMS;
Right click on your database
Generate scripts
Change radio button to Select specific database objects
Check the tables box
Select your table
Next
Set the filename etc
Click on Advanced button
Change the Types of data to script setting to Schema and data
Click next to finish
Blog post here with screenshots for clarification
I have SSIS Package which Exports Data from table to Excel file
Control Flow :-
Data Flow :-
This is My Step :-
Drop Excel Table
Create Excel Table with format as of my Select Query which i used to retrieve data from database
Insert Data from Database to Excel file
I Used Query Like Select * From Table Where --Some Condition
I retrieve 3000 rows out of 10000 rows and put that 3000 rows in my excel sheet.
But when open my excel sheet i saw scrollbar which goes till 10000th row and ends hence my excel sheet size also increses . how can i reduce my excel sheet size ? my excel sheet contains only 3000 rows then why blank cells which goes till 10000th row ?
SQL Server 2008 &
Visual Studio 2008 with BIDS
I believe your issue is around the method in which you are using to create the file. You have two alternatives and both should fix your issue:
Solution #1:
You can create an Excel file with those predefined columns, essentially your empty output file - this would act as your 'Template File'. Your flow would then be this:
File System Task - Copy template file to output or working directory (rename if necessary)
OLEDB Source Task - Query your source for the data (3000)
Data Conversion Task
Excel Destination Task - Put data into new Excel file
Note: You already have steps 2 thru 3 complete, you just need to make sure you are connecting to the new Excel file. Also, to clarify, step 1 is outside the Control Flow Task.
This way is helpful because you always have a blank and consistently formatted Excel file to copy and work with.
Solution #2:
The other option is to use a Script Task and create the Excel file - you could also load the data into the file in this task. This requires some basic understanding of VB.NET or C#. Basically you would need to get a XLS library (like NPOI). This is more complicated, but gives you the best functionality.
I recommend you try solution #1 and see how that works for you.
Drop table SheetName doesn't delete the sheet instead it just deletes the row . If for the 1st time you have loaded 10K rows and then again executed the package by restricting the number of rows to 3K ,the excel file will still contain those 10K empty rows as it retains the sheet along with the empty spaces .
You can use script task to delete the sheet using COM obects .But for that you need to place the Excel PIA(Primary Interop Assemply) to make it visible for VSA or else create a new excel file every time the package runs
Else as suggested by Nicarus use File System Task to delete the existing file and create a new Excel file on every execution .
Diagram :
File System Task :
Use the same components and the query for Create Table using Execute SQL task and your DFT