Script to import text files to SQL Server - sql-server

I have around 1000 text files that need to be imported as tables to MS SQL Server. Usually I use Import and Export Data Tool, but doing that a 1000 times would be insufficient.
Is there a way to automate the process and import the 1000 text files and create the tables in SQL without doing that manually? Can that be achieved using a script?

Use SSIS. Start with your export package saved to the file system. In SSDT create a new SSIS project. Delete the Package.dtsx file created with the project. Right click the Packages "folder" and select Add Existing Package, navigate to the package you saved and select it. Now you can start automating the loads.

Related

Automatically Save Query Results to Excel

I have a simple query that I want to run and save the results in excel file on daily basis.
I am trying to create a SSIS(SQL Server Integration Services) package to automate this thing.
My query is
select * from Customers c , Orders o where c.Company = 'Company A'
I was able to create a package and run it using Integration Service, but the issue is that when I make changes in the database and re run the package, instead of overwriting the existing excel, it simply appends the new rows. Is there any way that I can overwrite the existing excel?
The second issue is, I can't see the option to schedule the package under integration services. When I right-click on the package name, I see the following
There is no option to schedule the package.
Is there another way, I can achieve the same purpose? I tried creating a job also -
I choose the target file as an excel file which was already not present in the system -
After the job executes successfully, it creates the excel but I can't open it, it says the file is corrupted.
EDIT : My SSISDB doesn't show any packages. When I created the package, it showed me two options, either store it on sql server or file system, so I chose file system -
Steps how I created the package -
I right clicked the database -> Export Data -> specified the data source-> specified the above query-> saved the ssis on file system .
Initially, my package was not automatically present under the file system.I had to import it manually.
I followed the steps from here -
https://www.mssqltips.com/sqlservertutorial/202/simple-way-to-export-data-from-sql-server/

How to generate Insert statement from PGAdmin4 Tool?

We are writing a new application, and while testing, we will need a bunch of dummy data. I've added that data by using MS Access to dump excel files into the relevant tables into the Postgres database.
What should I do now to generate an Insert statements from the PGAdmin4 Tool similar to what SQL Studio allow us to generate an Insert statements for SQL Server? There are no options available to me. I can't use the closest one, which is to export and import the data via CSV.
I understand that you cannot import the CSV file into the actual DB as this needs to be done through ASP.NET core EF. Perhaps, you can probably create a test schema and import the CSV file into the test schema. Once you have the data imported into the test schema, you can use that to generate SQL statements using the steps below:
Right click on target table and select "Backup".
Select a file path to store the backup. You can save the file name as data.backup
Choose "Plain" as Format.
Open the tab "Options" check "Use Column Inserts".
Click the Backup-button.
Once the file gets generated you can open with Notepad++ or VSCode to get the SQL insert statements
You can use the statements generated and delete the test schema created
Here is a resource that might help you in loading data from Excel file into PostgresSQL if you still need to take this path Transfer Data from Excel to PostgreSQL

Automation -File Upload-Microsoft SQL Server Management Studio

I have to weekly upload text files from a server location to Microsoft SQL Server Management Studio .I wish to automate the task so that files are automatically uploaded .Can somebody suggest me the way?
Methods I know of:-
Via SQL:
Use OPENROWSET to open the file and obtain the records to write into
a table.
Use BULK INSERT to open the file and insert directly into a table (you may need to pair with XP_CMDSHELL to get a directory listing to loop through)
VIa SSMS:
Create a DataFlow to import from file
SSMS makes it easier to do clever things with the import process. But it can be very finnicky.
With both of those you can set up an Agent job to run the script / package automatically.

DBeaver How to Save Export Data Script

DBeaver has excellent Import data/Export data tools, but is it possible to Save the export or import script rather than execute it immediately so that it can be executed at a later time?
I need to migrate a production database so I want to prepare all of the scripts beforehand and then execute them all when it's time to do the switch.
You can save your scripts from menu [SQL editor]->[Save SQL script].
Then in project panel you can create link on folder that contains your saved scripts or on script itself.
Also there is a Script Management guide on DBreaver Git wiki.

Importing a CSV file without headers into SQL 2008

I want to import a CSV with 4,8M records into a SQL 2008 table. I'm trying to do it with the Management Studio wizard but it keeps trying to recognize a header row which the CSV doesnt have. I don't find any option to skip this and although I specify the columns myself, the wizard still tries to find a header row and doesnt import anything without it.
The structure of the CSV is
"818180","25529","Dario","Pereyra","Rosario","SF","2010-09-02"
I've also tried alternatives like BULK INSERT but then I find out that with BULK INSERT I can't import files with a text qualifier.
The easiest way for a one time import would definitely be the "Import Data" function in SQL Server Management Studio. This will launch a wizard and will allow you to define where you want to import your data from - pick "Flat File Source". The next dialog allows you to browse for the file you want to import, and you can specify all sorts of things on that dialog (like the encoding of the file, what the text qualifier is - if any - and so on.
You can also select to skip any number of rows (e.g. "skip the first 5 rows"), or you can select that the first row has column names.
If your file does not have the column names in the first row, uncheck that option.
If you need to do this import over and over again, you can save all the information about the import as a Integration Services package in SQL Server (or in an external SSIS file), and you can then run that import again and again from the SQL Server Agent "Jobs" menu (enable SQL Server Agent, if you haven't already, and find the "Jobs" sub-item - you should see all your jobs under there and you can launch them again from that menu).
And if you want to, you can also launch these SSIS packages from your C# or VB.NET code - check out this CodeProject article or see Michael Entin's blog post on the topic.
Uncheck "first row has column names"
http://epicenter.geobytes.com/images/MsSqlI006.gif

Resources