I am trying to insert a value in a middle table, which is created by having two other tables relationship many to many. So this middle table has 2 foreign keys(idProduct and idWarehouse), one for each table and an additional row(storage). So I have trouble inserting in this third row.
It gives this error:
Result: near "SELECT": syntax error
At line 1:
INSERT INTO ProductsInWarehouse (idProduct, idWarehouse, storage)
VALUES
(SELECT
This is my code:
INSERT INTO ProductsInWarehouse (idProduct, idWarehouse, storage)
VALUES
(SELECT id FROM Products
WHERE nameProduct = 'cheese',
SELECT id FROM Warehouse
WHERE nameWarehouse = 'Vegas',
'10')
Each of the queries must be enclosed inside parantheses:
INSERT INTO ProductsInWarehouse (idProduct, idWarehouse, storage) VALUES
(
(SELECT id FROM Products WHERE nameProduct = 'cheese'),
(SELECT id FROM Warehouse WHERE nameWarehouse = 'Vegas'),
'10'
);
This will work if theses queries return always only 1 row, so both columns nameProduct and nameWarehouse must be unique.
The statement can also be written without the VALUES clause, with SELECT:
INSERT INTO ProductsInWarehouse (idProduct, idWarehouse, storage)
SELECT
(SELECT id FROM Products WHERE nameProduct = 'cheese'),
(SELECT id FROM Warehouse WHERE nameWarehouse = 'Vegas'),
'10';
Related
I am working on java with JDBC connections and trying to perform DDL commands.
Here i had a doubt about one particular flow, can that be possible? if yes, can you explain me how and what to do with example.
I am trying to select data from item table containing item_id, sale_price, description, barcode columns and want to update barcode data for item_id = 9 and insert into item_duplicate table. With out updating the item table. But item_dupliacte table should contain the updated value for barcode.
my item_duplicate table
item table
MERGE item_duplicate AS D
USING item AS I
ON (D.item_id = I.item_id )
WHEN MATCHED
THEN UPDATE set D.part_no='new part'
WHEN NOT MATCHED BY D
THEN
INSERT (item_id,part_no,sale_price,description,barcode)
SELECT i.ITEM_ID,i.PART_NO,i.SALE_PRICE,i.DESCRIPTION,b.BARCODE
FROM item i
JOIN item_barcode b
ON b.ITEM_ID = i.ITEM_ID
WHERE i.ITEM_ID = ?
WHEN NOT MATCHED BY I
THEN DELETE;
A simple insert into select from.
CREATE TABLE dbo.TEST
(
item_id INT NOT NULL
, barcode VARCHAR (20) NULL
, sale_price DECIMAL (14, 2) NULL
, description VARCHAR (100) NULL
, PRIMARY KEY (item_id)
)
CREATE TABLE dbo.TEST_COPY
(
item_id INT NOT NULL
, barcode VARCHAR (20) NULL
, sale_price DECIMAL (14, 2) NULL
, description VARCHAR (100) NULL
, PRIMARY KEY (item_id)
)
INSERT INTO TEST_COPY (item_id, barcode, sale_price, description) SELECT item_id, '9999' as barcode, sale_price, description FROM TEST WHERE item_id = 9
I am trying to delete the duplicates that I am getting in my database using DELETE function of my SQL with LIMIT 1 but it is showing me the "LIMIT" syntax error .
myDatabase.execSQL("CREATE TABLE IF NOT EXISTS users (name VARCHAR , age INT(3))");
myDatabase.execSQL("INSERT INTO users (name, age) VALUES ('Vaishant', 21)");
myDatabase.execSQL("INSERT INTO users (name, age) VALUES ('Tommy',4)");
myDatabase.execSQL("DELETE FROM users WHERE name = 'Vaishant' LIMIT 1");
Can someone tell me why I am getting this error and how to correct it ?
SQLite does not support LIMIT in a DELETE statement.
Use a subquery that returns the rowid of a row that contains the name that you search for:
String sql = "DELETE FROM users WHERE rowid = (SELECT rowid FROM users WHERE name = 'Vaishant' LIMIT 1)";
myDatabase.execSQL(sql);
If you want to delete the duplicate names and keep only 1, then you can do this:
DELETE FROM users
WHERE NOT EXISTS (SELECT 1 FROM users u WHERE u.name = users.name AND u.rowid < users.rowid)
or:
DELETE FROM users
WHERE rowid NOT IN (SELECT MIN(rowid) FROM users GROUP BY name)
In your delete statement you are using limit with Delete query. You should use it as below:
First add id column as primary key in your table
myDatabase.execSQL("CREATE TABLE IF NOT EXISTS users (ID INTEGER PRIMARY KEY AUTOINCREMENT, name VARCHAR , age INT(3))");
change the query of delete as below
myDatabase.execSQL("DELETE FROM users
WHERE id IN
(SELECT id FROM
(SELECT id,
ROW_NUMBER() OVER (PARTITION BY name ORDER BY name) AS row_num FROM users )t
WHERE row_num > 1)");
I'm trying to get some data from multiple tables using join query. I have a reservation table where I store the "pick-up location id" and "drop location id" and another table "location" where the location names were stored.
I'm trying to get the location name (pick-up and drop location) in 2 different variables from the "location" table with these 2 ids from the reservation table.
I have the tables with the following parameters.
Location table: "locations_deatils" has location_name location_id.
Reservation table: reservation_cars has pickup_location id, drop_location id, pick_up date, drop_date
SELECT l.location_name as pick-up_loc,
l.location_name as drop_loc,
c.pickup_date,
c.return_date,
FROM locations_deatils l inner join
reservation_cars c on l.locations_id = c.pickup_location and l.locations_id
= c.return_location
WHERE c.pickup_date >= :pickupTime and c.return_date <=:returnTime;
You need to do a separate join for the pick-up and for the drop location.
SELECT l1.location_name as 'pick-up location', l2.location_name as 'drop location', pickup_date, return_date
FROM reservation_cars r
JOIN locations_deatils l1 ON l1.locations_id = r.pickup_location
JOIN locations_deatils l2 ON l2.locations_id = r.return_location
WHERE r.pickup_date >= :pickupTime
AND r.drop_date <= :returnTime
Maybe you meant something like this:
SELECT p_loc.location_name AS pickup_location,
cars.pickup_date,
d_loc.location_name AS drop_location,
cars.return_date
FROM reservation_cars cars
JOIN location_details p_loc
ON cars.pickup_location = p_loc.location_id
JOIN location_details d_loc
ON cars.return_location = d_loc.location_id
WHERE .....
You might need to use an outer join in case the pickup and/or return locations are not set (yet).
I have two tables:
TABLE1 has columns(id,name,password),
TABLE2 has columns(id,salary,dept).
id in table1 is auto-generated sequence number. Data is added using java form. I wanted to show table1 id in table2 along with other details.
create or replace PROCEDURE PRO5(y in varchar,z in varchar,x in varchar,
c in varchar,d in varchar,b in number, j in number)
as BEGIN
insert into emp_general(username,email,password) values(y,z,x);
insert into SALARY_DET(username ,salary,company,dept) values(y,c,d,b);
set j := INSERT INTO salary_det(ID) SELECT ID FROM emp_general;
END;
Try this,
select #Id = convert(int,scope_identity());
For Oracle
SET LID = LAST_INSERT_ID();
Hi I have been trying to select more than one rows by calling the procedure through CallableStatement. While I am trying to populate the result set to the combo box the code returns the error as follows.
Java Error:
java.sql.SQLException: Subquery returns more than 1 row
Stored Procedure :
CREATE DEFINER=`user_name`#`%` PROCEDURE `GET_USER_PROFILE`(
IN p_user_id VARCHAR(150),
IN p_role VARCHAR(150),
OUT p_user_data VARCHAR(200),
OUT p_city VARCHAR(150),
OUT p_state VARCHAR(150),
OUT p_country VARCHAR(150),
OUT q_Msg VARCHAR(150))
BEGIN
DECLARE available INT DEFAULT 0;
SET p_city = (SELECT CITY FROM countries GROUP BY CITY);
SET p_state = (SELECT STATE FROM countries GROUP BY STATE);
SET p_country = (SELECT COUNTRY FROM countries GROUP BY COUNTRY);
SELECT COUNT(EMAIL) INTO available FROM STAFF_PROFILE WHERE EMAIL = p_user_id AND ROLE = p_role;
IF(available=1) THEN
SET p_user_data = (SELECT * FROM STAFF_PROFILE WHERE EMAIL = p_user_id AND ROLE = p_role );
else
SET q_Msg = 'USER_LOGGED_FIRST';
END IF;
END
#DaveHowes and #Ilya are correct, the issue is with your SQL statement.
Lets say in your Countries table consists of the following:
city state country
'New York' 'New York' 'USA'
'Los Angeles' 'California' 'USA'
'Chicago' 'Illinois' 'USA'
'Ottawa' '' 'Canada'
Now, if we take your sub queries from your example:
SELECT city FROM countries GROUP BY city
would return:
city
'New York'
'Los Angeles'
'Chicago'
'Ottawa'
You're trying to assign a multiple results to a varchar hence you get the exception "Subquery returns more than 1 row".