insert into statements,how to be fastly insert records - sql

If i have a 1 million insert into statement in text file then how to be inserted in a table with faster time.
INSERT INTO TABLE(a,b,c) VALUES (1,'shubham','engg');
INSERT INTO TABLE(a,b,c) VALUES (2,'swapnil','chemical');
INSERT INTO TABLE(a,b,c) VALUES (n,'n','n');
like in above we have 1 million rows. how to be fastly insert records in a table If any other options is there else simply run above all statement in sequency.

Avoid row by row inserts for dumping such huge quantities of data. They are pretty slow and there's no reason you should rely on the plain inserts, even if you're utilising SQL* Plus command line to run it as a file. Put the values to be inserted as comma(or any other delimiter) separated entries in a flat file and then use options such as
SQL Loader
External table
It is a common practice to extract data into flat files from tools like SQL Developer. Choose the "CSV" option instead of "Insert" that will generate the values in a flat file.

If it means that your text file contains literally those INSERT INTO statements, then run the file.
If you use GUI, load the file and run it as a script (so that it executes them all).
If you use SQL*Plus, call the file using #, e.g.
SQL> #insert_them_all.sql

You may use a batch insert on single query statement:
INSERT INTO TABLE(a,b,c) VALUES
(1,'shubham','engg'),
(2,'swapnil','chemical'),
(n,'n','n');

Related

Using SSMS 2014, how to generate efficient insert scripts?

When generating INSERT scripts for tables in SSMS 2014, the output of the generated script is in the format of:
INSERT [schema].[table] (<column_list) VALUES (column_values)
GO
INSERT [schema].[table] (<column_list) VALUES (column_values)
GO
While this gets the job done, it is horribly slow. We can manually re-tool the script to be in the format of:
INSERT [schema].[table] (<column_list>)
VALUES (column_values)
,(column_values) -- up to 1000 rows
GO
INSERT [schema].[table] (<column_list>)
VALUES (column_values)
,(column_values) -- up to 1000 rows
GO
We've noted an increase in speed of more than 10x by changing the script in this manner, which is very beneficial if it is a script that needs to be re-run occasionally (not just a one-time insert.)
The question is, is there a way to do this from within the SSMS script generation, or alternately, is there a process that can convert the script that is in the first format into a script in the second format?
I develop SSMSBoost add-in. We have feature named Results Grid Scripter, that can produce virtually any script that you want based on the data from Results Grid:
https://www.ssmsboost.com/Features/ssms-add-in-results-grid-script-results
There are several pre-defined templates and you can change them to get exactly that you need.

Oracle Insert Performance

I insert data from external table to my table that is range partitioned and have two local indexes.
My case,
I have to insert records under 60 seconds for each flat file because new one comes.
A flat file consists +5 M records and 2 GB.(volume : Totally 5 billion records daily ) Additionally I do some sort operations before insert on external table select.
My environment is on Oracle ExaData X-5 12.2 version.
There are many process doing insert to same table simultaneously so I can not use append hint. I can use parallel and nologging hints.
I have .exe that manages all this process. It gets flat files from source then combines them if there is one more flat files then moves combined file to true directory for external table and calls a procedure to insert data from external table to my table. Lastly changes flat file with next one.
There is one .exe for each different flat file.
All select operation takes 35-40 seconds from external table but insert takes too much times 50-60 seconds.
Can you give me some useful advices?

Bulk insert from CSV file - skip duplicates

UPDATE: Ended up using this method created by Johnny Bubriski and then modified it a bit to skip duplicates. Works like a charm and is apparently quite fast.
Link: http://johnnycode.com/2013/08/19/using-c-sharp-sqlbulkcopy-to-import-csv-data-sql-server/
I have been searching for an answer to this but cannot seem to find it. I am doing a T-SQL bulk insert to load data into a table in a local database from a csv file. My statement looks like this:
BULK INSERT Orders
FROM 'csvfile.csv'
WITH(FIELDTERMINATOR = ';', ROWTERMINATOR = '0x0a', FORMATFILE = 'formatfile.fmt', ERRORFILE = 'C:\\ProgramData\\Tools_TextileMagazine\\AdditionalFiles\\BulkInsertErrors.txt')
GO
SELECT *
FROM Orders
GO
I get an exception when I try to insert duplicate rows (for example taking the same csv file twice) which causes the entire insert to stop and rollback. Understandable enough since I am violating the primary key constraint. Right now I just show a messagebox to let users know that duplicates are present in the csv file, but this is of course not a proper solution, actually not a solution at all. My question is, is there any way to ignore these duplicate rows and just skip them and only add the rows that are not duplicate? Perhaps in a try catch somehow?
If it is not possible, what would be the "correct" (for lack of a better word) way to import data from the csv file? The exception is causing me a bit of trouble. I did read somewhere that you can set up a temporary table, load the data into it and select distinct between the two tables before inserting. But is there really no easier way to do it with bulk insert?
You could set the MAXERRORS property to quite a high which will allow the valid records to be inserted and the duplicates to be ignored. Unfortunately, this will mean that any other errors in the dataset won't cause the load to fail.
Alternatively, you could set the BATCHSIZE property which will load the data in multiple transactions therefore if there are duplicates it will only roll back the batch.
A safer, but less efficient, way would be to load the CSV file in to a separate, empty, table and then merge them into your orders table as you mentioned. Personally, this is the way I'd do it.
None of these solutions are ideal but I can't think of a way of ignoring duplicates in the bulk insert syntax.
First of all there is no direct solution like BULK INSERT WHERE NOT EXISTS. You can use following solutions.
When using BULK INSERT there are two scenarios
You are BULK INSERTing in a empty table
You are BULK INSERTing in a already filled table
Solution for Case 1:
set the MAXERRORS = 0
set the BATCHSIZE = total number of rows in csv file
Using above statement with BULK INSERT would cause whole BULK INSERT operation to roll back even if there is a single error, this would prevent from rows being imported even when there are errors in few rows. You would need to solve all import errors to complete the import operation. This method would prevent situations when you import 50 rows, 30 gets imported and remaining not. Then you have to search CSV file for the failed ones and reimport them or delete all imported rows from SQL table and do BULK INSERT again.
Solution for Case 2:
1> You can run select query on the existing table, right click and export in CSV. If you have any spreadsheet program then paste the data below import data and use conditional formatting on primary key column to highlight duplicate rows and delete them. Then use BULK INSERT operation.
2> Set MAXERRORS = number of rows, and import csv file using BULK INSERT. This is not safe and recommended way, as there might be other kinds of errors apart from duplicate key errors
3> Set BATCHSIZE = 1 and MAXERRORS = high number, and import csv file using BULK INSERT. This would import all the rows without errors and any row with errors would be skipped. This is useful if the data set is small and you can visually identify the rows which are not imported by observing table columns like id number column which shows the missing numbers.
4> Right click on existing table, choose table as > Crete to > new query window. Simply rename the table name and change to staging name like table_staging. BULK Insert in the staging table and then run a second query to copy data from staging table to main table and using WHERE clause to check if row/pk exists. This is a much safer way but forces you to create a staging table.

How to populate look up tables in sql?

Hi i am working on look up tables first time and we have some distinct values in look up tables which are coming from excel sheets. So i have two databases say A and B. there are around 22 look up tables and 5 certified tables in database A. and one import table and few work table in database B. i am just wondering how i am going to load Look up tables. do i have to write stored procs for each look up table and use in SSIS package. i just have to get distinct values from the Excel sheet for respective look up tables. i am using SQL Server 2005. Thanks for reading this.
1) Save the excel sheet as csv (use "Save As" from file menu)
2) Set "IGNORE_DUP_KEY" option on the destination table, this will skip duplicate values in CSV files(excel sheet)
3) construct a BULK INSERT command and provide the csv file and the destination table.
Alternative to 2) You may also set MAXERRORS in the BULK INSERT command to a value higher than the number of lines in csv file, this options just ignores any errors(there fore all the duplicate key errors will not cause the INSERT to fail)

how to insert a specific data in text file(with .log extension) into database table?

I have managed to insert the whole text-file data in SQL-SERVER database table with this statement
BULK INSERT [dbo].[tablename] FROM 'c:\weblog.log' WITH (
FIELDTERMINATOR = ' ',
ROWTERMINATOR = '\n' )
But my text-file is not organized in any format and it contains some data i want to omit from the insertion process. So i am looking for a way to be able to only insert some of the data in the text-file into my database table?
There are two ways. One way is to write some code that will read the specific data, to be inserted into the database, from the file and then insert it to the database. Second, if you have some minimal data that you want to remove from the file, you might run a Regex query to find and replace them with none (deleting the unwanted portion) from the file and then doing a bulk insert.
For bulk insert to work, you need it to be a delimited text file. So if your log file is not a delimited log file, you might not be able to insert it using the bulk insert.