The table value constructor has limit of maximum number of lines; when this limit is exceeded, the error message 10738 is displayed. In the case in question, the information "INSERT instruction exceeds the maximum allowed number of 1000 line values". That is, each command with the INSERT statement can have up to 1000 lines; no more.
According to the above mentioned documentation, to insert more lines than the limit allows, use one of the following methods:
- Create multiple INSERT instructions;
- Use a derived table;
- Bulk import data using the utility BCP or the instruction
BULK INSERT.
To the method 1 - Create multiple INSERT instructions, each line to be included must be in a single command with the INSERT statement. Something like
-- código #1
set nocount on;
set IDENTITY_INSERT [dbo].[cidade] on;
INSERT INTO cidade (id_cidade,descricao,uf,codigo_ibge,ddd) VALUES (1, 'Abacate da Pedreira (Macapá)','AP',1600550,'96');
INSERT INTO cidade (id_cidade,descricao,uf,codigo_ibge,ddd) VALUES (2, 'Abadia (Jandaíra)','BA',2917904,'75');
INSERT INTO cidade (id_cidade,descricao,uf,codigo_ibge,ddd) VALUES (3, 'Abadia de Goiás','GO',5200050,'62');
For this it is necessary to open the file in text editor, and replace
),
for );
(1
for
INSERT INTO cidade (id_cidade,descricao,uf,codigo_ibge,ddd) VALUES (1
(2
for
INSERT INTO cidade (id_cidade,descricao,uf,codigo_ibge,ddd) VALUES (2
- ...
(9
for
INSERT INTO cidade (id_cidade,descricao,uf,codigo_ibge,ddd) VALUES (9
The text editor quickly replaces.
To the method 2 - Use a derived table, just replace the code snippet
INSERT INTO cidade (id_cidade,descricao,uf,codigo_ibge,ddd) VALUES
for
INSERT INTO cidade (id_cidade, descricao, uf, codigo_ibge, ddd)
SELECT id_cidade, descricao, uf, codigo_ibge, ddd
from (values
and add at the end the code snippet
) as T (id_cidade, descricao, uf, codigo_ibge, ddd);
The final code will look like this:
-- código #3
set IDENTITY_INSERT dbo.cidade on;
INSERT INTO cidade (id_cidade, descricao, uf, codigo_ibge, ddd)
SELECT id_cidade, descricao, uf, codigo_ibge, ddd
from (values
(1, 'Abacate da Pedreira (Macapá)','AP',1600550,'96'),
(2, 'Abadia (Jandaíra)','BA',2917904,'75'),
(3, 'Abadia de Goiás','GO',5200050,'62'),
...
) as T (id_cidade, descricao, uf, codigo_ibge, ddd);
Probably the simplest change to perform in the file containing the script.
To the method 3 - Import the data in bulk, and using BULK INSERT, one should transform the SQL script into a data file in CSV format. Something like
1, Abacate da Pedreira (Macapá),AP,1600550,96
2, Abadia (Jandaíra),BA,2917904,75
3, Abadia de Goiás,GO,5200050,62
For this it is necessary to open the file in text editor and:
- delete the first 4 lines
- replace
),
for nothing; i.e., eliminate ),
- replace
(1
for 1
- replace
(2
for 2
- ...
- replace
(9
for 9
- pull out
)
final
- replace
'
for nothing; i.e., withdraw apostrophes.
The text editor quickly replaces.
It should be attentive to the fact that there are localities in which there is elisão, as Santa Bárbara d'West. As in the script the string delimiter is apostrophe, I assume that in the name of the localities there is no use of elision. But it is good to check before.
The import command looks something like
-- código #2 v2
BULK INSERT Cidade
from 'cidade.csv'
with (fieldterminator=',');
Do you have access to the database table where the data used to generate the text file with INSERT is? If necessary, you have how to regenerate the text file?
– José Diz
I have only the script shown in the post.
– hard123