I have two tables. Products(id_product, name) and Images(id_image, id_product, image).
How can i INSERT a product and it's categories in a single query, inserting the inserted id_product into the Images coresponding id_product.
Product(1, 'Toy')
Image(1, 1, 'image.jpg')
Image(2, 1, 'image-2.jpg')
Image(3, 1, 'image-3.jpg')
Something like this. I need it to be in a single query.
you can use a store procedure to handle the sql statements
Related
Looking to insert 500 rows into many-to-many table (sample_tag).
Aiming to have 500 new records created in sample_tag linking the first 500 rows from sample to associated tag_id in the tag table.
The following code seems to make sense, but triggers an error because the nested SELECT statement returns more than one row:
INSERT INTO sample_tag (sample_id, tag_id)
VALUES ((SELECT sample_id FROM sample WHERE sample_id <= 500), 1)
What could be the correct SQL to accomplish this insert for multiple rows?
You can have multiple inserts by not using VALUES keyword. You need to specify same number of columns on your source table.
INSERT INTO sample_tag (sample_id, tag_id)
SELECT sample_id, tag_id from sample where sample_id<=500
I created two tables, Employeeinfo and Employeerequests.
Table Employeeinfo can have one unique user with columns:
id (primary key, auto increment)
name
dep
address
and table Employeerequests can have multiple requests against one unique user ID with columns
id (primary key, auto increment)
CustomerID(foreign key to Employeeinfo(ID column))
category
requests.
Now I want to design a stored procedure in such a way so that I can insert values into both tables at the same time. Please help. I am very new to SQL. Thanks in advance.
This is a bit long for a comment.
SQL Server only allows you to insert into one table in a single query. You presumably want to provide both employee and request information. So that limitation on insert is a real problem.
You can get around the limitation by creating a view combining the two table and then defining an instead of insert trigger on the view. This is explained in the documentation.
That said, you seem to not have extensive SQL knowledge. So, I would recommend simply using two separate statements, one for each table. You can wrap them in a stored procedure, if you find that convenient.
In the stored procedure, you can use Output clause of Insert statement as:
DECLARE #MyTableVar TABLE (NewCustomerID INT);
-- The OUTPUT clause have access to all the columns in the table,
-- even those not part of Insert statement ex:EmployeeID.
INSERT INTO [dbo].[Employeeinfo] ([Name], [dep], [address])
OUTPUT INSERTED.Id INTO #MyTableVar
SELECT 'Test', 'TestDep', 'TestAddress'
-- then make insert in child table as
INSERT INTO [dbo].[Employeerequests] (CustomerID, category)
SELECT NewCustomerID, 'TestCat'
FROM #MyTableVar
Sample code here...
Hope that helps!
I need to insert data into a media table. The data must have the media id (which is a sequence), format (DVD, VHS) and the movie title id which is a sequence that exists another table named movies. How do I pull the data for the title_id from the movies table into the media table? I'm not quite sure where to start but I've listed the code I have so far for the first 2 columns.
INSERT INTO m_media
(media_id, format, title_id)
VALUES (media_id_seq.NEXTVAL, 'DVD', );
Instead of inserting separate values, use SELECT statement which will fetch data from the movies table. Something like this:
insert into m_media (media_id, format, title_id)
select media_id_seq.nextval,
'DVD',
m.title_id
from movies m
where ... --> condition, if you want to restrict rows returned by that SELECT
I have 3 tables in SQL server with following fields.
Table 1- id, name, age.
Table 2- id,email, Address.
Table 3- id, name, email.
I wish to use two triggers like, when I insert values on Table 1, id and name should insert in Table 3. When I insert values in Table 2, Email should insert in Table 3 and it should insert at id and name position means it should not show NULL values. Name,id and email should insert in one row.
You should make the View for inserting data from two tables to a single table
You should fix the data model so that it is normalized. You wouldn't need a trigger to replicate data into Table 3 if the name and email columns weren't in 2 tables.
Is there a way i can do multiple inserts into one table using a condition?
i have a list of subscribers in tbl_subscribers. i have an update on productX so i would like everyone who is subscribes to productX to get a notification. The user_notification table is id PK, user_id, notification_id. The two values i need is product_id (productX) which allows me to find a list of subscribers in tbl_subscribers and the notification_id to insert into the user_notification table.
How can i do this insert using one query? I see you can do a select statement in sqlite http://www.sqlite.org/lang_insert.html but i cannot wrap my head around how i may do this nor seen an example.
I believe you're looking from INSERT SELECT as outlined here:
http://www.1keydata.com/sql/sqlinsert.html
The second type of INSERT INTO allows
us to insert multiple rows into a
table. Unlike the previous example,
where we insert a single row by
specifying its values for all columns,
we now use a SELECT statement to
specify the data that we want to
insert into the table. If you are
thinking whether this means that you
are using information from another
table, you are correct. The syntax is
as follows:
INSERT INTO "table1" ("column1", "column2", ...)
SELECT "column3", "column4", ... FROM "table2"
insert into user_notification(user_id, notification_id)
select s.user_id, #notification_id
from tbl_subscriber s
where s.product_id = #productX