I have this file.sql
:
INSERT INTO "Parcelas" ("PlanEstudio", "LineaEstudio", "NIF", "Plan", "Cosecha", "Linea", "Modulo", "CombCoberturas", "Referencia", "Hoja", "Parcela", "Cultivo", "Variedad", "GrupoVarietal", "Comarca", "ProvSigPac", "TermSigPac", "AgreSigPac", "ZonaSigPac", "PoliSigPac", "ParcSigPac", "ReciSigPac", "Superficie", "Arboles", "Edad", "ProdPrinc", "ProdComp", "Precio", "ValorProcPrinc", "ValorProdComp", "PRE", "PRF", "RendAsig2021")
VALUES (2022, 300, '43729050R', 2020, 2021, 300, '2', 2, 'K826975', 2, 11, 5, 26, '31-08', 9, 25, 900, 0, 0, 26, 371, 1, 0.86, 1016, 18, 35000, 0, 0.25, 8750, 0, 35000, 22229, 0);
And I get this error with sqlcmd -i
:
$ /opt/mssql-tools/bin/sqlcmd -S "$SERVER" -d "$DATABASE" -U "$USER" -P "$PASSWORD" -i a.sql
Msg 102, Level 15, State 1, Server BBDD-DES, Line 1
Incorrect syntax near 'Parcelas'.
But if I don't use -i
and execute the same insert, I don't have problems:
$ /opt/mssql-tools/bin/sqlcmd -S "$SERVER" -d "$DATABASE" -U "$USER" -P "$PASSWORD"
1> INSERT INTO "Parcelas" ("PlanEstudio", "LineaEstudio", "NIF", "Plan", "Cosecha", "Linea", "Modulo", "CombCoberturas", "Referencia", "Hoja", "Parcela", "Cultivo", "Variedad", "GrupoVarietal", "Comarca", "ProvSigPac", "TermSigPac", "AgreSigPac", "ZonaSigPac", "PoliSigPac", "ParcSigPac", "ReciSigPac", "Superficie", "Arboles", "Edad", "ProdPrinc", "ProdComp", "Precio", "ValorProcPrinc", "ValorProdComp", "PRE", "PRF", "RendAsig2021") VALUES (2022,300,'43729050R',2020,2021,300,'2',2,'K826975',2,11,5,26,'31-08',9,25,900,0,0,26,371,1,0.86,1016,18,35000,0,0.25,8750,0,35000,22229,0);
2>
Why I can't run this insert using a .sql
file?
1条答案
按热度按时间7vux5j2d1#
The problem is that, by default,
sqlcmd
does not enable quoted identifiers. As a result"Parcelas"
is read as a a literal string, not as a delimited object name.Considering that none of your objects appear to need delimit identified, you could just get rid of them, or alternatively use the T-SQL delimit identifier, brackets (
[]
). Though, in truth, it's best to pass the-I
(Enable Quoted Identifiers) when usingsqlcmd
:Note this proof of concept, for example:
This returns the following table:
Notice many rows with the value
name
. If I pass the-I
switch, I get the actual database names: