How can I insert 100000 rows in SQL Server? - sql

INSERT INTO pantscolor_t (procode, color, pic)
VALUES
('74251', 'Black', '511black.jpg'),
('74251', 'OD Green', '511odgreen.jpg'),
('74251', 'Black', '511black.jpg'),
('74251', 'OD Green', '511odgreen.jpg'),
('74251', 'Black', '511black.jpg'),
('74251', 'OD Green', '511odgreen.jpg'),
..........
..........
..........
INSERT INTO pantscolor_t (procode,color,pic)
VALUES
('74251', 'Charcoal', '511charcoal.jpg'),
('74251', 'Charcoal', '511charcoal.jpg'),
('74251', 'Charcoal', '511charcoal.jpg'),
('74251', 'Charcoal', '511charcoal.jpg'),
.............
.............
.............
INSERT INTO........................
INSERT INTO........................
INSERT INTO........................
INSERT INTO........................
I have 100000 rows like this but my insert statements bigger than 1000 rows. When I run the SQL statement in SSMS, I get an error:
The number of row value expressions in the INSERT statement exceeds the maximum allowed number of 1000 row values.

Another solution is to use a select query with unions.
INSERT INTO pantscolor_t (procode,color,pic)
SELECT '74251', 'Black', '511black.jpg'
UNION ALL SELECT '74251', 'OD Green', '511odgreen.jpg'
UNION ALL SELECT '74251', 'Black', '511black.jpg'
UNION ALL SELECT '74251', 'OD Green', '511odgreen.jpg'
UNION ALL SELECT '74251', 'Black', '511black.jpg'
UNION ALL SELECT '74251', 'OD Green', '511odgreen.jpg'
--etc....
UNION ALL is used instead of UNION in order to speed up the query when dealing with thousands of records. UNION ALL allows for duplicate rows whereas UNION will ensure that duplicates do not exist in the result set. For this scenario we don't want to remove any possible duplicates, so UNION ALL is used.

Create csv file (or some file with defined field delimiter and row delimiter) and use "BULK INSERT" option to load file to database. File can have 100000 rows; there won't be any problem of loading huge file using bulk upload.
http://msdn.microsoft.com/en-us/library/ms188365.aspx

INSERT mytable (col1, col2, col3, col4, col5, col6)
SELECT * FROM (VALUES
('1502577', '0', '114', 'chodba', 'Praha', 'Praha 1'),
('1503483', '0', 'TVP', 'chodba', 'Praha', 'Praha 2'),
/* ... more than 1000 rows ... */
('1608107', '0', '8', 'sklad', 'Tlumačov', 'Tlumačov'),
('1608107', '0', '9', 'sklad', 'Tlumačov', 'Tlumačov')
) AS temp (col1, col2, col3, col4, col5, col6);

By applying the following you should not have any error :
INSERT INTO pantscolor_t (procode,color,pic) VALUES ('74251','Black','511black.jpg')
INSERT INTO pantscolor_t (procode,color,pic) VALUES ('74251', 'OD Green', '511odgreen.jpg')
INSERT INTO pantscolor_t (procode,color,pic) VALUES ('74251', 'Black', '511black.jpg')
INSERT INTO pantscolor_t (procode,color,pic) VALUES ('74251', 'OD Green', '511odgreen.jpg')
INSERT INTO pantscolor_t (procode,color,pic) VALUES ('74251', 'Black', '511black.jpg')
...........
I tried it and it worked, of course you can use the excel to concatenate the values easily.

Create a csv out.csv etc
Then use:
BULK INSERT
FROM 'C:\out.csv'
WITH
(
FIELDTERMINATOR = ',',
ROWTERMINATOR = '\n'
)
GO

Related

Selecting from Stored Procedures and Storing SP Results into a Table

I'm trying to build a couple of tables based upon the results of a few Stored procedures inside an Informix Database, that I cannot edit, and the parameter of the second SP requires values from another (will be called a few times). I have figured out what to do, but am having trouble executing it, predominantly because I cannot save my DB into a Temp table so I can then modify it.
If someone could give me some pointers, that would be great.
Some examples of things I've tried
call <stored procedure(...)> INTO TEMP db
and
create temp table name(...)
INSERT INTO namefrom storedprocedure()
and
create temp table name(...)
Select INTO name from storedprocedure()
Kind Regards,
Fox
Here's an example of a user-defined function (aka stored procedure) returning multiple values that are inserted into a temporary table. It uses the stores_demo database.
create function func1()
returning char(15), char(15);
define v_fname, v_lname char(15);
foreach select fname, lname into v_fname, v_lname from customer
return v_fname, v_lname with resume;
end foreach
end function;
create temp table tt1(fname char(15), lname char(15));
insert into tt1 execute function func1();
You can use:
SELECT * FROM TABLE(stored_procedure()) INTO TEMP p32;
This inserts the rows returned by the stored procedure into the temporary table p32.
There may be other ways to do it, but this seemed to work and is fairly straight-forward to understand.
(Tested: Informix 12.10.FC6 on a Mac running macOS 10.14.6 Mojave.)
Test schema
CREATE TABLE elements
(
atomic_number INTEGER NOT NULL PRIMARY KEY
CHECK (atomic_number > 0 AND atomic_number < 120),
symbol CHAR(3) NOT NULL UNIQUE,
name CHAR(20) NOT NULL UNIQUE,
atomic_weight DECIMAL(8, 4) NOT NULL,
pt_period SMALLINT NOT NULL
CHECK (pt_period BETWEEN 1 AND 7),
pt_group CHAR(2) NOT NULL
-- 'L' for Lanthanoids, 'A' for Actinoids
CHECK (pt_group IN ('1', '2', 'L', 'A', '3', '4', '5', '6',
'7', '8', '9', '10', '11', '12', '13',
'14', '15', '16', '17', '18')),
stable CHAR(1) DEFAULT 'Y' NOT NULL
CHECK (stable IN ('Y', 'N'))
);
INSERT INTO elements VALUES( 1, 'H', 'Hydrogen', 1.0079, 1, '1', 'Y');
INSERT INTO elements VALUES( 2, 'He', 'Helium', 4.0026, 1, '18', 'Y');
INSERT INTO elements VALUES( 3, 'Li', 'Lithium', 6.9410, 2, '1', 'Y');
INSERT INTO elements VALUES( 4, 'Be', 'Beryllium', 9.0122, 2, '2', 'Y');
INSERT INTO elements VALUES( 5, 'B', 'Boron', 10.8110, 2, '13', 'Y');
INSERT INTO elements VALUES( 6, 'C', 'Carbon', 12.0110, 2, '14', 'Y');
INSERT INTO elements VALUES( 7, 'N', 'Nitrogen', 14.0070, 2, '15', 'Y');
INSERT INTO elements VALUES( 8, 'O', 'Oxygen', 15.9990, 2, '16', 'Y');
INSERT INTO elements VALUES( 9, 'F', 'Fluorine', 18.9980, 2, '17', 'Y');
INSERT INTO elements VALUES( 10, 'Ne', 'Neon', 20.1800, 2, '18', 'Y');
INSERT INTO elements VALUES( 11, 'Na', 'Sodium', 22.9900, 3, '1', 'Y');
INSERT INTO elements VALUES( 12, 'Mg', 'Magnesium', 24.3050, 3, '2', 'Y');
INSERT INTO elements VALUES( 13, 'Al', 'Aluminium', 26.9820, 3, '13', 'Y');
INSERT INTO elements VALUES( 14, 'Si', 'Silicon', 28.0860, 3, '14', 'Y');
INSERT INTO elements VALUES( 15, 'P', 'Phosphorus', 30.9740, 3, '15', 'Y');
INSERT INTO elements VALUES( 16, 'S', 'Sulphur', 32.0650, 3, '16', 'Y');
INSERT INTO elements VALUES( 17, 'Cl', 'Chlorine', 35.4530, 3, '17', 'Y');
INSERT INTO elements VALUES( 18, 'Ar', 'Argon', 39.9480, 3, '18', 'Y');
The full table has 118 elements listed, of course.
Sample procedure
DROP PROCEDURE IF EXISTS lightweights;
CREATE PROCEDURE lightweights()
RETURNING INTEGER AS atomic_number, VARCHAR(3) AS symbol, VARCHAR(20) AS name;
DEFINE num INTEGER;
DEFINE sym VARCHAR(3);
DEFINE nam VARCHAR(20);
FOREACH SELECT e.atomic_number, e.symbol, e.name
INTO num, sym, nam
FROM elements AS e
WHERE atomic_number < 10
RETURN num, sym, nam WITH RESUME;
END FOREACH;
END PROCEDURE;
Example execution of the procedure to create a temp table
$ sqlcmd -d stores
SQL[3368]: select * from table(lightweights());
1|H |Hydrogen
2|He |Helium
3|Li |Lithium
4|Be |Beryllium
5|B |Boron
6|C |Carbon
7|N |Nitrogen
8|O |Oxygen
9|F |Fluorine
SQL[3368]: select * from table(lightweights()) into temp p32;;
SQL[3369]: select * from p32;
1|H |Hydrogen
2|He |Helium
3|Li |Lithium
4|Be |Beryllium
5|B |Boron
6|C |Carbon
7|N |Nitrogen
8|O |Oxygen
9|F |Fluorine
SQL[3370]: q;
$
Note that in sufficiently recent versions of Informix, you can replace TEMP with STANDARD or RAW to create a permanent table instead of a temporary table (and deleting TEMP is equivalent to replacing it with STANDARD). See SELECT statement > INTO table clauses.

How to order by sum in each rollup data group?

I have my animal table with points for each animal, its color and name.
create table animals (
category text,
color text,
name text,
points numeric
)
insert into animals values ('CATS', 'WHITE', 'FLUFFY', 1234)
insert into animals values ('CATS', 'WHITE', 'FLUFFY', 2568)
insert into animals values ('CATS', 'WHITE', 'SALLY', 124)
insert into animals values ('CATS', 'WHITE', 'SALLY', 12401)
insert into animals values ('CATS', 'WHITE', 'MR KITTY', 101)
insert into animals values ('CATS', 'WHITE', 'MR KITTY', 1761)
insert into animals values ('CATS', 'BLACK', 'DARTH CATHER', 1761)
insert into animals values ('CATS', 'BLACK', 'DARTH CATHER', 2361)
insert into animals values ('CATS', 'BLACK', 'ITS A TRAP', 171)
insert into animals values ('CATS', 'BLACK', 'HAN SOLO', 1713)
insert into animals values ('DOGS', 'BROWN', 'PLUTO', 1761)
insert into animals values ('DOGS', 'BROWN', 'DUFFY', 23611)
insert into animals values ('DOGS', 'BROWN', 'PICACHOU', 1171)
insert into animals values ('DOGS', 'BROWN', 'DUFFY', 17123)
insert into animals values ('DOGS', 'BLACK', 'MICKEY', 17361)
insert into animals values ('DOGS', 'BLACK', 'JACKSON', 23361)
insert into animals values ('DOGS', 'BLACK', 'BA', 1751)
insert into animals values ('DOGS', 'BLACK', 'MACGUYVER', 17513)
insert into animals values ('FISH', 'YELLOW', 'LENON', 1571)
insert into animals values ('FISH', 'YELLOW', 'BEUSCONI', 17153)
I need to return sql output similar to output from Excel pivot table:
(with proper descending order by sum of points within data groups)
I'm really close with this query:
with data_grouped as
(
Select
COALESCE ( category, 'Total'::text) as category,
COALESCE ( color, 'Total'::text) as color,
COALESCE ( name,'Total'::text) as name,
sum(points) as points
FROM animals
GROUP BY category, color, name
),
data_evolutions as
(
select grouping(d.category, d.color, d.name) as GR_ALL,
d.category,
d.color,
d.name,
sum(d.points) as points
from data_grouped d
group by rollup(d.category, d.color, d.name)
order by category, color, name
)
select
COALESCE (d.category, 'Total'::text) as "CATEGORY",
COALESCE (d.color, 'Total'::text) as "COLOR",
COALESCE (d.name, 'Total'::text) as "NAME",
points
from data_evolutions d
order by
CASE WHEN category IS NULL THEN 1 ELSE 0 END, category,
CASE WHEN color IS NULL THEN 1 ELSE 0 END, color,
CASE WHEN name IS NULL THEN 1 ELSE 0 END, points desc
but still I get wrong order.
Animals are ordered by name, not by sum.
How should I change my query to get descending order by sum of points within each level?
I need correct sums, and order with totals and subtotals.

Insert Into Multiple Values (Oracle SQL Developer)

I'm trying to write a simple insert into statement using multiple values. The solutions I've seen say to separate each set of values with a comma, however, I am still being met with an error. Here is my SQL statement.
INSERT INTO DIVISION (DIVISION_ID, DIVISION_NAME)
VALUES ('20', 'Quebec'), ('30', 'Ontario');
You can also use this one:
INSERT ALL
INTO DIVISION (DIVISION_ID, DIVISION_NAME) VALUES (20, 'Quebec')
INTO DIVISION (DIVISION_ID, DIVISION_NAME) VALUES (30, 'Ontario')
SELECT * FROM dual;
If DIVISION_ID is a numeric data type:
INSERT INTO DIVISION (DIVISION_ID, DIVISION_NAME)
SELECT 20, 'Quebec' FROM DUAL
UNION ALL
SELECT 30, 'Ontario' FROM DUAL;
Otherwise:
INSERT INTO DIVISION (DIVISION_ID, DIVISION_NAME)
SELECT '20', 'Quebec' FROM DUAL
UNION ALL
SELECT '30', 'Ontario' FROM DUAL;

How to make a join with the stored procedure result?

I want to make a join between a query and the result of stored procedure (for some reason).
my query is:
SELECT COUNT(a.values_key) AS docscount,b.main_code,b.year
FROM SP_name_should_be_here a
INNER JOIN dmr23req b
ON a.values_key = b.req_year || ',' || b.req_ser
AND a.newstateserial = 17
AND a.taskcode = 35
GROUP BY b.main_code,b.year
My stored procedure dmr_get_full_in takes two param
The answer is that you should use an 'iterator function' in the FROM clause:
FROM TABLE(FUNCTION stored_procedure(arg1, arg2)) AS alias(col1, col2, ....)
The 'alias' is not always necessary, but look hard at the column names in the output of executing the procedure directly:
EXECUTE stored_procedure(arg1, arg2)
If that contains unique column names, you'll be OK; if not, you won't and you'll need the AS alias(col1, col2, ...) notation.
Here's an analogue (more or less) to your query.
Trivial function that could be written differently, but works:
CREATE FUNCTION SimpleElements(lo INTEGER, hi INTEGER) RETURNING INTEGER AS Atomic_Number;
DEFINE an INTEGER;
FOREACH SELECT Atomic_Number
INTO an
FROM Elements
WHERE Atomic_Number BETWEEN lo AND hi
ORDER BY Atomic_Number
RETURN an WITH RESUME;
END FOREACH;
END FUNCTION;
Query using TABLE(FUNCTION stored_procedure(arg1, arg2)):
The MOD function is used as simple way of generating some groups to count, but it illustrates the notation which is the main point of the exercise.
SELECT COUNT(*) AS group_count, MOD(b.atomic_number, 3) AS mod_num
FROM TABLE(FUNCTION SimpleElements(1, 10)) AS a(Atomic_Number)
JOIN Elements AS b
ON a.atomic_number = b.atomic_number
GROUP BY mod_num
ORDER BY mod_num;
Output:
3 0
4 1
3 2
Table of Elements
CREATE TABLE elements
(
atomic_number INTEGER NOT NULL UNIQUE CONSTRAINT c1_elements
CHECK (atomic_number > 0 AND atomic_number < 120),
symbol CHAR(3) NOT NULL UNIQUE CONSTRAINT c2_elements,
name CHAR(20) NOT NULL UNIQUE CONSTRAINT c3_elements,
atomic_weight DECIMAL(8,4) NOT NULL,
stable CHAR(1) DEFAULT 'Y' NOT NULL
CHECK (stable IN ('Y', 'N'))
);
INSERT INTO elements VALUES( 1, 'H', 'Hydrogen', 1.0079, 'Y');
INSERT INTO elements VALUES( 2, 'He', 'Helium', 4.0026, 'Y');
INSERT INTO elements VALUES( 3, 'Li', 'Lithium', 6.9410, 'Y');
INSERT INTO elements VALUES( 4, 'Be', 'Beryllium', 9.0122, 'Y');
INSERT INTO elements VALUES( 5, 'B', 'Boron', 10.8110, 'Y');
INSERT INTO elements VALUES( 6, 'C', 'Carbon', 12.0110, 'Y');
INSERT INTO elements VALUES( 7, 'N', 'Nitrogen', 14.0070, 'Y');
INSERT INTO elements VALUES( 8, 'O', 'Oxygen', 15.9990, 'Y');
INSERT INTO elements VALUES( 9, 'F', 'Fluorine', 18.9980, 'Y');
INSERT INTO elements VALUES( 10, 'Ne', 'Neon', 20.1800, 'Y');
INSERT INTO elements VALUES( 11, 'Na', 'Sodium', 22.9900, 'Y');
INSERT INTO elements VALUES( 12, 'Mg', 'Magnesium', 24.3050, 'Y');
INSERT INTO elements VALUES( 13, 'Al', 'Aluminium', 26.9820, 'Y');
INSERT INTO elements VALUES( 14, 'Si', 'Silicon', 28.0860, 'Y');
INSERT INTO elements VALUES( 15, 'P', 'Phosphorus', 30.9740, 'Y');
INSERT INTO elements VALUES( 16, 'S', 'Sulphur', 32.0650, 'Y');
INSERT INTO elements VALUES( 17, 'Cl', 'Chlorine', 35.4530, 'Y');
INSERT INTO elements VALUES( 18, 'Ar', 'Argon', 39.9480, 'Y');
…

best practice for sql scripts syntax

I have sql file that contains long Inserst.
the problem is that when I want for example to edit column number12 I have to start counting the columns in the values area and its very confusing.
they looks something like :
witch is very confusing for updating since you have to find which value belongs to which column.
is there another way to write Insert script so it will be more easy to mach column with its values ? or maybe there's a tool that can help ?
maybe something like -
Insert tableName column1 = '10', column2 = '5' , column3 = 'asdsd' ....
P.S - to see the image right click on it and 'Open in new tab'
Use two lines:
INSERT INTO TABLE (COLUMN1, COLUMN2 , COLUMN3)
VALUES (Value1 , "Long Value 2", "..." );
You can use the new way of inserting records that was introduced in SQL Server 2008.
insert into #MyTable
(Column1, Column2, Column3, Column4, Column5, Column6) values
(1, 2, 3, 4, cast(6 as numeric(38,8))), 6),
(1, 2, 3, 4, cast(6 as numeric(38,8))), 6),
(1, 2, 3, 4, cast(6 as numeric(38,8))), 6),
(1, 2, 3, 4, cast(6 as numeric(38,8))), 6),
(1, 2, 3, 4, cast(6 as numeric(38,8))), 6)
You need to keep the column names aligned with the data and when you have more lines than fit on a page you can either restart the insert or add a column comment line at an appropriate place
insert into #MyTable
(Column1, Column2, Column3, Column4, Column5, Column6) values
(1, 2, 3, 4, cast(6 as numeric(38,8))), 6),
(1, 2, 3, 4, cast(6 as numeric(38,8))), 6),
(1, 2, 3, 4, cast(6 as numeric(38,8))), 6),
--(Column1, Column2, Column3, Column4, Column5, Column6) values
(1, 2, 3, 4, cast(6 as numeric(38,8))), 6),
(1, 2, 3, 4, cast(6 as numeric(38,8))), 6)
You could also keep your data in an actual table and update the table instead. Use some tool to script the inserts from that table. For example generate scripts in Server Management Studio.
You don't need multiple INSERT statements - one will do.
You can use a SELECT statement with UNION to insert multiple rows:
INSERT myTable (Col1, Col2)
SELECT val1, val2 UNION
SELECT val3, val4
If you are using SQL Server 2008, the VALUES statement has an extension:
INSERT myTable (Col1, Col2)
VALUES (val1, val2),
(val3, val4)
See INSERT for SQL Server on MSDN.
If you are talking about readability, you could use something like the following (though you will end up with a very long file), and coding this up will take lots of time:
INSERT myTable (Col1, Col2)
SELECT val1, -- Col1
val2 -- Col2
UNION
SELECT val3, -- Col1
val4 -- Col2
There are no standard convention for what you want, as such scripts are meant for running in SQL, not for reading.