I've seen this error mentioned on StackOverflow, but not in the context I am using. I am relatively new to Teradata and this behavior is throwing me for a loop. Here is code that works:
INSERT INTO test_table (this, that) VALUES (1, 2);
Here is code that throws the error:
INSERT INTO test_table (this, that) VALUES (1, 2), (3, 4);
This is super confusing to me because the Teradata docs have the following example:
INSERT INTO cities VALUES (2, 'San Jose'), (3, 'Oakland');
Could someone show me what am I missing here? Thanks!
Teradata only allows you to insert one record with a single values. You can see this in the syntax diagram for insert -- there is no "backloop".
Two inserts is a simple workaround:
INSERT INTO test_table (this, that)
VALUES (1, 2);
INSERT INTO test_table (this, that)
VALUES (3, 4);
Related
Im using MariaDb and Im trying to execute a multi-value insert query using Begin End and I get the following error:
You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'END' at line 1 0.000 sec
BEGIN;
INSERT INTO Client (Id, Name, Total)
VALUES
(1, 'John', 1000),
(2, 'Mary', 3000),
(3, 'Mike', 14000),
(4, 'George', 15000);
END;
Can I use Begin, End with a multi-value insert and if yes what is the correct syntax here?
I have millions of data to insert. the above is just a small sample of my dataset
I have a programming language that does not allow me to write queries on multiple lines. It has to be written all in a single line.
I am unable to send a GO command because it has to be in a new line..
So for example, this does not work:
insert into mytable (field1, filed2) values (1, 2), (3, 4); go
as it should be
insert into mytable (field1, filed2) values (1, 2), (3, 4);
go
I've tried multiple things but none worked:
insert into mytable (field1, filed2) values (1,2),(3,4); \r go
insert into mytable (field1, filed2) values (1,2),(3,4); \r\n go
insert into mytable (field1, filed2) values (1,2),(3,4); $r$n go
insert into mytable (field1, filed2) values (1,2),(3,4); char(10) go
insert into mytable (field1, filed2) values (1,2),(3,4); char(13) go
Is there a way to write it inline, and have SQL Server use it as 2 different lines?
GO is not part of the TSQL language. It is used and recognized only by SSMS and sqlcmd to cut your script into parts ("batches"), and then each part is compiled and run separately, one after the other.
The reason that SSMS and sqlcmd work this way this is that it makes it possible to have e.g. a CREATE TABLE statement, followed by INSERT statements for that table. The INSERT-part will only compile if the table already exists, and that will be the case only after the CREATE has been run.
It is OK to combine multiple INSERTs into one statement. When in doubt about where the next statement should start, you can add a semicolon (;) to mark the end of the previous statement.
I get an error when trying to run this SQL query in MS Access:
INSERT INTO Learner (learnerPersonIDPKFK, registrationDate)
VALUES (1, 21/09/2015);
INSERT INTO Learner (learnerPersonIDPKFK, registrationDate, recommendedByLearnerPersonIDFK)
VALUES (2, 05/03/2016, 1);
This is the error:
enter image description here
The Learner table looks like this:
CREATE TABLE Learner
(
learnerPersonIDPKFK INT NOT NULL PRIMARY KEY,
registrationDate DATETIME,
recommendedByLearnerPersonIDFK INT NOT NULL,
CONSTRAINT fk_recommendedByLearnerPersonIDFK
FOREIGN KEY(recommendedByLearnerPersonIDFK)
REFERENCES Learner (learnerPersonIDPKFK)
);
Try wrapping your dates in single quotes:
INSERT INTO Learner (learnerPersonIDPKFK, registrationDate)
VALUES (1, '21/09/2015');
INSERT INTO Learner (learnerPersonIDPKFK, registrationDate, recommendedByLearnerPersonIDFK)
VALUES (2, '05/03/2016', 1);
Depending on your localization settings for the DATETIME format, you may need to use the MM/DD/YYYY format. Remember, your passing in a varchar and letting SQL try to "guess" how to implicitly convert it:
INSERT INTO Learner (learnerPersonIDPKFK, registrationDate)
VALUES (1, '09/21/2015');
INSERT INTO Learner (learnerPersonIDPKFK, registrationDate, recommendedByLearnerPersonIDFK)
VALUES (2, '03/05/2016', 1);
Use a simple tester to verify if SQL can "guess" your date formating:
SELECT CAST('09/21/2015' AS DATETIME)
I have searched across the board and the internet trying to see what I'm doing wrong. I have endlessly gotten the "Declare: syntax" error no matter where in the code I have moved the declaration. I've tried it with and without # signs and semicolons thinking this was something different than C++. All the guides I've read say this should work so perhaps someone here can tell me what is wrong?
BEGIN TRANSACTION;
DECLARE #clID INT;
CREATE TABLE CLAIM(claimID, repDate, lossDate, claimNo, claimStat);
INSERT INTO CLAIM VALUES(1, '2016-10-1', '2016-10-1', 1, 'Open');
INSERT INTO CLAIM VALUES(2, '2016-10-1', '2016-10-1', 2, 'Open');
CREATE TABLE EXPOSURE(expID, claimID, coverage, claimEx, expStat);
INSERT INTO EXPOSURE VALUES(1, 2, 'BI', 'U152', 'Open');
INSERT INTO EXPOSURE VALUES(2, 2, 'PD', 'U152', 'Open');
CREATE TABLE RESERVELINE(resLineID, expID, claimID, covID, IP);
INSERT INTO RESERVELINE VALUES(1, 1, 2, 'BI', 02);
INSERT INTO RESERVELINE VALUES(1, 3, 3, 'CDL', 01);
CREATE TABLE RESULTS(claimID);
COMMIT;
INSERT INTO RESULTS
SELECT claimID FROM CLAIM WHERE repDate<'2016-10-3';
SELECT * FROM RESULTS;
--Must FIRST query first table with Date()
Thanks guys.
I want to insert a certain value into a certain field of five different rows altogether. But whenever I run this query it doesn't get executed. What's wrong with it, and how can I fix it?
INSERT INTO `employee`(`password`) VALUES ('abc') WHERE `id` IN (1,2,3,4,5);
An INSERT can't have a WHERE clause. It looks like you meant to do an update:
UPDATE `employee`
SET `password` = 'abc'
WHERE `id` IN (1,2,3,4,5);
Or perhaps a multi-row insert:
INSERT INTO `employee` (`id`, `password`)
VALUES (1, 'abc'), (2, 'abc'), (3, 'abc'), (4, 'abc'), (5, 'abc');
Also, FYI, you really shouldn't be storing passwords as plain text, which it looks like you may be doing.