Subquery cannot appear in an Insert Values statement - sql

I have the following query in SQL Server CE which gives me an error during the execution time:
INSERT INTO trans_rel(trans, sale_purch_id, inc_exp_id)
VALUES('p', 104, (select MAX(expence_id) from c_expence))
The error is this:
Subquery cannot appear in an Insert Values statement.
What is wrong with this query?

Try this one:
INSERT INTO trans_rel
SELECT 'p', '102', MAX(expence_id)
FROM c_expence

This is exactly what your are looking for:
INSERT INTO trans_rel(trans, sale_purch_id, inc_exp_id)
SELECT 'p' as 'trans', '104' as 'sale_purch_id', MAX(expence_id) AS inc_exp_id
FROM c_expence;

Related

"failed to evaluate expression" when fiddling with INSERT INTO on DataBricks SQL

I receive the following error when I try to insert into a casted date in my table
Error in SQL statement: AnalysisException: failed to evaluate expression to_date('01.01.2016', 'dd.mm.yyyy'): Cannot evaluate expression: to_date(01.01.2016, Some(dd.mm.yyyy)); line 2 pos 1
insert into E_Par_Holidays
values
('BE', to_date('01.01.2016', 'dd.mm.yyyy'));
The table is defined as:
create table E_Par_Holidays (
Country varchar(255),
Holiday date
);
And oddly the following SQL statement works like a charm:
SELECT to_date('01.01.2016', 'dd.mm.yyyy') as DateExample;
Thank you for your help!
I fixed it by using insert into select logic
insert into E_Par_Holidays
SELECT 'BE', to_date('01.01.2016', 'dd.mm.yyyy');
you shouldn't use insert statement in spark sql.
instead use dataframe writer api
dataframe.write.mode(SaveMode.append).jdbc(...)
You can use following: insert into alfiya.timestamptbl values(cast('0001-01-01 17:00:00.000 +0300' as timestamp ));

Access INSERT with nested SELECT

Why does the following SQL statement not work?
INSERT INTO dialog (speaker, dialog_text) VALUES (
(
SELECT FIRST(id)
FROM FIGURE
WHERE char_name="Doe" AND forename="John"
),
"Some text"
);
It produces this error:
Query input must contain at least one table or query.
The single SELECT statement works.
An Access SQL INSERT ... VALUES statement will not let you use a subquery for one of the VALUES
Switching to an INSERT ... SELECT statement, as Piotr suggested will work.
Or you could use an Access Domain Aggregate function, instead of a subquery, in your INSERT ... VALUES statement:
INSERT INTO dialog (speaker, dialog_text)
VALUES (
DMin("id", "FIGURE", "char_name='Doe' AND forename='John'"),
'Some text'
);
Following works:
INSERT INTO dialog (speaker, dialog_text)
SELECT FIRST(id), "Some text"
FROM FIGURE
WHERE char_name="Doe" AND forename="John"

Insert statement in SQL Server 2005

I have strange problem related to SQL Server 2005
When I try to insert into table
insert into IDName
VALUES (101 , 'AA'),
(301 , 'BB')
I receive this error
Msg 102, Level 15, State 1, Line 3
Incorrect syntax near ','.
There is no problem, if I insert records one by one.
EDIT:
Thanks for reply guys.... but this script works in other installation of sql server 2005... I think it is some setting issue but i do not where... if you could help
This syntax was introduced in SQL Server 2008. So upgrade, or use the more verbose:
INSERT dbo.IDName(column1, column2)
SELECT 101 , 'AA'
UNION ALL SELECT 301 , 'BB';
Some additional changes:
Always use the schema prefix when referencing objects
Always specify the column list for an INSERT
Always use semi-colons to terminate statements
SQL Server 2005 does not support that insert syntax; you would either need
insert into IDName
SELECT 101 , 'AA'
UNION ALL SELECT 301 , 'BB'
or
insert into IDName VALUES (101 , 'AA');
insert into IDName VALUES (301 , 'BB');

SQL Server – inserting multiple rows with single (ANSI style) statement

I am using following method for inserting multiple rows using a single INSERT statement, that is the ANSI style of inserting rows. It is available in SQL Server 2008 and 2012. I am not sure of SQL Server 2005/ 2000.
Create test table:
create table TestInsert (ID INT, Name NVARCHAR(50))
Single INSERT statement to insert 5 rows
INSERT INTO TestInsert
VALUES (1,'a'),
(2,'b'),
(3,'c'),
(4,'d'),
(5,'e')
Please let me know if there is any other best way to achieve this
Try this instead:
INSERT TestInsert
SELECT 1, 'a'
UNION ALL
SELECT 2, 'b'
UNION ALL
SELECT 3, 'c'
UNION ALL
SELECT 4, 'd'
UNION ALL
SELECT 5, 'e'
SQL Server - inserting multiple rows with single (ANSI style) statement
For SQL Server 2000+
According to SQL The Complete Reference, Third Edition (August 12, 2009):
1) The syntax for multirow INSERTs is
INSERT INTO table-name (columns not mandatory)
query
(page 236, Figure 10-3).
2) The SELECT statement has the FROM clause mandatory (page 87, Figure 6-1).
So, in this case, to insert multiple rows using just one INSERT statement we need an auxiliary table with just one row:
CREATE TABLE dual(value INT PRIMARY KEY CHECK(value = 1))
INSERT dual(value) VALUES(1)
and then
INSERT INTO table-name (columns) -- the columns are not mandatory
SELECT values FROM dual
UNION ALL
SELECT another-values FROM dual
UNION ALL
SELECT another-values FROM dual
Edit 2: For SQL Server 2008+
Starting with SQL Server 2008 we can use row constructors: (values for row 1), (values for row 2), (values for row 3), etc. (page 218).
So,
INSERT INTO TestInsert
VALUES (1,'a'), --The string delimiter is ' not ‘...’
(2,'b'),
(3,'c'),
(4,'d'),
(5,'e')
will work on SQL Server 2008+.

Counting Null records not working

create table scoda(product varchar(25),price int,sale varchar(23))
insert into scoda values('watch',10,6)
insert into scoda values('socks',8,'NULL')
SELECT COUNT(*) AS GK FROM scoda WHERE sale IS NULL
Expected output:1
Actual output:0
help needed to continue i am beginner in sql
USE
insert into scoda values('socks',8,NULL)
(without quotes).
Problem is in insert statement:
insert into scoda values('socks',8,NULL)