I am trying to debug some classic ASP code and the app keeps breaking on an insert statement. The backend is in SQL
The statement looks something like this:
insert into tableX (id, fo, ao) values (12, '', 'ab')
in tableX both fo and ao are set to allow null values.
fo is a foreign key pointing to the foTable (look up table)
I guess it's not liking the single quotes? How else do I go about this in classic asp?
The error I'm getting is:
The INSERT statement conflicted with the FOREIGN KEY constraint
"FK_tableX_FO". The conflict occurred in database "tableX", table
"dbo.FIELD_OFFICE", column 'FO'.
I also want to add that this is a process of migrating from Oracle. Does anyone know if Oracle treats '' as nulls, which is why I'm now getting an error in SQL?
An empty string is not a null value. You need to call it like this
insert into tableX (id, fo, ao) values (12, null, 'ab')
Alternatively
insert into tableX (id, ao) values (12, 'ab')
Either of these statements will work.
insert into tableX (id, ao) values (12, 'ab')
or
insert into tableX (id, fo, ao) values (12, NULL, 'ab')
If fo is set to allow nulls and you don't want to violate the FK constraint, then you should provide NULL instead of an empty string for the fo column.
Related
I have a basic upsert query that works ok:
insert into table (id, data) values (1, '<data_string>')
on conflict (id) do update set data='<data_string>';
The only problem is I need to fly lots of these queries over the network so I wonder if there is a way to cut the traffic in half by not having <data_string> listed twice in the query?
Yes, there is the special table EXCLUDED for the purpose:
INSERT INTO tbl (id, data)
VALUES (1, '<data_string>')
ON CONFLICT (id) DO UPDATE SET data = EXCLUDED.data;
Like the manual explains:
Note that the special excluded table is used to reference values originally proposed for insertion:
Works for multi-row INSERTs as well:
INSERT INTO tbl (id, data)
VALUES (1, '<data_string1>')
, (2, '<data_string2>')
, (3, '<data_string3>')
ON CONFLICT (id) DO UPDATE
SET data = EXCLUDED.data;
I have a table with two columns: id, age where id is the primary key
I know how to insert ONE new row for new primary key else update the age with the new value if the age value is not null using the following sql:
insert into obj (id, age)
values (2, 42)
on conflict (id) do
update set age = coalesce(42, obj.age)
but how do I do it with multiple rows? For example:
insert into obj (id, age)
values (2, 42), (3, 43), (5, 60)
on conflict (id) do
update set age = coalesce(???, obj.age)
the question is what should I put in the '???' in COALESCE?
I thought some one suggested using COALESCE(values(age), obj.age, I tried but it didn't work (syntax error).
insert into obj (id, age)
values (2, 42), (3, 43), (5, 60)
on conflict (id) do
update set age = coalesce(excluded.age, obj.age)
Note the use of the special record excluded. Per the documentation:
The SET and WHERE clauses in ON CONFLICT DO UPDATE have access to the existing row using the table's name (or an alias), and to rows proposed for insertion using the special excluded table.
I'm receiving a "Syntax error in INSERT INTO statement" when trying to submit this query.
INSERT INTO
Modified (ModifiedID, Username, Table, RecordID)
VALUES
('12','null','Accident','1')
All datatypes match in the database. If I run the query without inserting into "Table", it works. I don't know why, when I add the table string value to the query I receive the error.
Try this
INSERT INTO Modified (ModifiedID, Username, [Table], RecordID)
VALUES ('12','null','Accident','1')
In most SQL dialects, TABLE is a RESERVED WORD. The [] is Microsoft's delimiter and I believe backticks ` are used in MySQL
Also, do you really want 'null' as opposed to NULL, they have different meanings
In HSQLDB:
INSERT INTO Modified (ModifiedID, Username, "Table", RecordID)
VALUES (12, NULL, 'Accident', 1);
I am just going to assume that "key-ish" sounding fields, eg. ModifiedID and RecordID, are supposed to be INTEGER or BIGINT rather than, say, VARCHAR. DDL would be nice.
In MySQL:
INSERT INTO Modified (ModifiedID, Username, `Table`, RecordID)
VALUES ('12', NULL, 'Accident', '1')
I switch between Oracle and SQL Server occasionally, and often forget how to do some of the most trivial tasks in SQL Server. I want to manually insert a row of data into a SQL Server database table using SQL. What is the easiest way to do that?
For example, if I have a USERS table, with the columns of ID (number), FIRST_NAME, and LAST_NAME, what query do I use to insert a row into that table?
Also, what syntax do I use if I want to insert multiple rows at a time?
To insert a single row of data:
INSERT INTO USERS
VALUES (1, 'Mike', 'Jones');
To do an insert on specific columns (as opposed to all of them) you must specify the columns you want to update.
INSERT INTO USERS (FIRST_NAME, LAST_NAME)
VALUES ('Stephen', 'Jiang');
To insert multiple rows of data in SQL Server 2008 or later:
INSERT INTO USERS VALUES
(2, 'Michael', 'Blythe'),
(3, 'Linda', 'Mitchell'),
(4, 'Jillian', 'Carson'),
(5, 'Garrett', 'Vargas');
To insert multiple rows of data in earlier versions of SQL Server, use "UNION ALL" like so:
INSERT INTO USERS (FIRST_NAME, LAST_NAME)
SELECT 'James', 'Bond' UNION ALL
SELECT 'Miss', 'Moneypenny' UNION ALL
SELECT 'Raoul', 'Silva'
Note, the "INTO" keyword is optional in INSERT queries. Source and more advanced querying can be found here.
Here are 4 ways to insert data into a table.
Simple insertion when the table column sequence is known.
INSERT INTO Table1 VALUES (1,2,...)
Simple insertion into specified columns of the table.
INSERT INTO Table1(col2,col4) VALUES (1,2)
Bulk insertion when...
You wish to insert every column of Table2 into Table1
You know the column sequence of Table2
You are certain that the column sequence of Table2 won't change while this statement is being used (perhaps you the statement will only be used once).
INSERT INTO Table1 {Column sequence} SELECT * FROM Table2
Bulk insertion of selected data into specified columns of Table2.
.
INSERT INTO Table1 (Column1,Column2 ....)
SELECT Column1,Column2...
FROM Table2
I hope this will help you
Create table :
create table users (id int,first_name varchar(10),last_name varchar(10));
Insert values into the table :
insert into users (id,first_name,last_name) values(1,'Abhishek','Anand');
For example, "person" table has "id" IDENTITY column as shown below:
CREATE TABLE person (
id INT IDENTITY, -- Here
name NVARCHAR(50),
age INT,
PRIMARY KEY(id)
)
Then, we don't need to manually put a value to "id" IDENTITY column when inserting a row:
INSERT INTO person VALUES ('John', 27) -- The value for "id" is not needed
And, we can also insert multiple rows without the values for "id" IDENTITY column:
INSERT INTO person VALUES ('John', 27), ('Tom', 18)
I'm trying to simply adding some data in a table but I receive an error:
Msg 110, Level 15, State 1, Line 1
There are fewer columns in the INSERT statement than values specified in the VALUES clause. The number of values in the VALUES clause must match the number of columns specified in the INSERT statement.
Here what I'm using
INSERT INTO dbo.ReModalities
(ModalityId, Name, Description)
VALUES
(
1,'A','A.',
2,'B','B.'
);
This should give you an idea of the Table Column
INSERT INTO [XXX].[dbo].[ReModalities]
([ModalityId]
,[Name]
,[Description])
VALUES
(<ModalityId, int,>
,<Name, nvarchar(64),>
,<Description, nvarchar(256),>)
GO
Also I would like to know if there is way I can avoid typing the IDs (the table has PK so they should be created automatically) many thanks!
Each row of the values statement should be enclosed in parenthesis. Try:
VALUES
(1,'A','A.'),
(2,'B','B.');
If the ID has a default value or is an identity, you can omit it:
insert dbo.ReModalities
(Name, Description)
values ('A','A.'),
('B','B.');