I have a query to insert like
insert into test (city,State,City,number,Sales,Quantity)
select 'AAA','Texas ','SSS',115121,79,1
UNION ALL
select 'BBB','Texas ','WWW',144921,338,2 UNION ALL
and 45000 record more.
What is the fastest way I can insert this query. I want to insert it with batch or bulk upload. Please help.
I'm new to Sql but what is the best way to insert more than 1000 rows from an excel document into my database(Sql server 2008.)
For example I'm using the below query:
INSERT INTO mytable(companyid, category, sub, catalogueref)
VALUES
('10197', 'cat', 'sub', '123'),
('10197', 'cat2', 'sub2', '124')
This is working fine but there is a limit of inserting 1000 records and I have 19000 records and I don't really want to do 19 separate insert statements and another question, is that the company id is always the same is there a better way then writing it 19000 times?
Just edit the data in Excel or another program to create N amount of insert statements with a single insert for each statement, you'll have an unlimited number of inserts. For example...
INSERT INTO table1 VALUES (6696480,'McMurdo Station',-77.846,166.676,'Antarctica','McMurdo')
INSERT INTO table1 VALUES (3833367,'Ushuaia',-54.8,-68.3,'America','Argentina')
...19,000 later
INSERT INTO table1 VALUES (3838854,'Rio Grande',-53.78769,-67.70946,'America','Argentina')
Microsoft provides an import wizard with SQL Server. I've used it to migrate data from other databases and from spreadsheets. It is pretty robust and easy to use.
There are several options, the Import Wizard which Erik suggests, or SSIS is another good one.
Read here:
Import Excel spreadsheet columns into SQL Server database
Ok, it's a late answer but I ran into this very same problem and found a solution that worked twice as fast as #Nur.B's solution for a 7K-row insertion.
Note: whenever possible prefer to use TRANSACTIONS when dealing with large amounts of data.
INSERT INTO mytable(companyid, category, sub, catalogueref)
SELECT '10197', 'cat', 'sub', '123' UNION ALL
SELECT '10197', 'cat2', 'sub2', '124' UNION ALL
-- ... other N-thousand rows
SELECT '10197', 'catN-1', 'subN-1', '12312' UNION ALL
SELECT '10197', 'catN', 'subN', '12313'; -- don't add "UNION ALL" statement on the last line
You should be able to insert using multiple transactions -
BEGIN TRANSACTION
Insert into mytable(companyid,category,sub,catalogueref)
values
('10197','cat', sub','123'),
('10197','cat2', sub2','124')
...998 more rows...
COMMIT TRANSACTION
go
BEGIN TRANSACTION
Insert into mytable(companyid,category,sub,catalogueref)
values
('10197','cat', sub','123'),
('10197','cat2', sub2','124')
...998 more rows...
COMMIT TRANSACTION
INSERT INTO mytable(companyid, category, sub, catalogueref)
SELECT companyid, category, sub, catalogueref from (VALUES
('10197', 'cat', 'sub', '123'),
('10197', 'cat2', 'sub2', '124')
//more 1000 rows...
as sub (companyid, category, sub, catalogueref)
While I was inserting some records in table i found that..
INSERT INTO T_CANDYBAR_DATA
SELECT CONSUMER_ID,CANDYBAR_NAME,SURVEY_YEAR,GENDER,1 AS STAT_TYPE,OVERALL_RATING
FROM CANDYBAR_CONSUMPTION_DATA
UNION
SELECT CONSUMER_ID,CANDYBAR_NAME,SURVEY_YEAR,GENDER,2 AS STAT_TYPE,NUMBER_BARS_CONSUMED
FROM CANDYBAR_CONSUMPTION_DATA;
79 rows inserted.
INSERT ALL
INTO t_candybar_data VALUES (consumer_id,candybar_name,survey_year,gender,1,overall_rating)
INTO t_candybar_data VALUES (consumer_id,candybar_name,survey_year,gender,2,number_bars_consumed)
SELECT * FROM candybar_consumption_data
86 rows inserted.
I have read somewhere that INSERT ALL INTO automatically unions then why those difference is showing.
The problem is your queries are different—your first is with UNION and your second is without—so they are naturally inserting different numbers of values. As far as what INSERT ALL is versus a straight INSERT:
INSERT can be used for inserting new records to a single table.
INSERT ALL can be used for inserting new records to multiple tables based on the query condition.
So your assumption as stated here:
I have read somewhere that INSERT ALL INTO automatically unions then
why those difference is showing.
Is incorrect. INSERT ALL doesn’t have anything to do with UNION in any way. But that said, you might be mixing up UNION ALL as explained here.
The SQL UNION ALL operator is used to combine the result sets of 2 or
more SELECT statements. It returns all rows from the query (even if
the row exists in more than one of the SELECT statements).
Each SELECT statement within the UNION ALL must have the same number
of fields in the result sets with similar data types.
So I have this temp table that has structure like:
col1 col2 col3 col3
intID1 intID2 intID3 bitAdd
I am doing a union of the values of this temp table with a select query and storing
it into the same temp table.The thing is col3 is not part of the union query I will
need it later on to update the table.
So I am doing like so:
Insert into #temptable
(
intID1,
intID2,
intID3
)
select intID1,intID2,intID3
From
#temptable
UNION
select intID1,intID2,intID3
From
Table A
Issue is that I want only the rows that are not already existing in the temp table to be added.Doing it this way will add a duplicate of the already existing row(since union will return one row)How do I insert only those rows not existing in the current temp table in my union query?
Use MERGE:
MERGE INTO #temptable tmp
USING (select intID1,intID2,intID3 From Table A) t
ON (tmp.intID1 = t.intID1 and tmp.intID2 = t.intID2 and tmp.intID3 = t.intID3)
WHEN NOT MATCHED THEN
INSERT (intID1,intID2,intID3)
VALUES (t.intID1,t.intID2,t.intID3)
Nice and simple with EXCEPT
INSERT INTO #temptable (intID1, intID2, intID3)
SELECT intID1,intID2,intID3 FROM TableA
EXCEPT
SELECT intID1,intID2,intID3 FROM #temptable
I see where you are coming from. In most programming languages #temptable would be a variable (a relation variable or relvar for short) to which you would assign a value (a relation value) thus:
#temptable := #temptable UNION A
In the relational model, this would achieve the desired result because a relation has no duplicate rows by definition.
However, SQL is not truly relational and does not support assignment. Instead, you are required to add rows to a table using SQL DML INSERT statements (which is not so bad: the users of a truly relational database language, if we had one, would no doubt demand a similar shorthand for relational assignment!) but you are also required to do the test for duplicates yourself.
The answers from Daniel Hilgarth and Joachim Isaksson both look good. It's good practice to have two good, logically sound candidate answers then look for criteria (usually performance under typical load) to eliminate one (but retaining it commented out for future re-testing!)
I know that within a trigger - at least for SQL Server - one should never assume that the inserted table has just one row, which means SQL like this in a trigger is usually bad:
select #UserID = ID from inserted
But out of curiosity, can a set of INSERT statements ever result in an inserted table of more than one row? I know it's easy enough with an UPDATE, but from my tests I can't simulate a similar result for INSERTs. I've tried sending across sets of inserts before sending the batch terminator, e.g:
insert into TriggerTest (col2) select 'a'
insert into TriggerTest (col2) select 'b'
insert into TriggerTest (col2) select 'c'
go
And also wrapping them in transactions:
begin tran
insert into TriggerTest (col2) select 'a'
insert into TriggerTest (col2) select 'b'
insert into TriggerTest (col2) select 'c'
commit
But it will always result in the trigger fired 3 times with an inserted table of 1 row, and never one time with an inserted table of 3 rows.
That completely makes sense to me (they are 3 separate statements after all), and I don't need to actually do it, I'm just wondering if INSERTS alone can ever behave differently to this.
Edit: this is a dumb question: of course it can, when inserting a result set!
insert into TriggerTest (col2) select 'a' union select 'b'
... or any other sort of set.
Forgive me, it is almost 3AM here. I'll leave this question here for people who should know better anyway.
try
insert into TriggerTest (col2)
select 'a'
union
select 'b'
union
select 'c'