dbTalk Databases Forums  

how insert data in two tables with single query in mysql

comp.databases.mysql comp.databases.mysql


Discuss how insert data in two tables with single query in mysql in the comp.databases.mysql forum.



Reply
 
Thread Tools Display Modes
  #1  
Old   
Pab
 
Posts: n/a

Default how insert data in two tables with single query in mysql - 11-23-2011 , 11:20 PM






Hi,


i am trying to insert data two table with single query with
foreign key reference but its not working
my query is


BEGIN TRANSACTION
DECLARE @salid int
Insert into sal_products (upc,shelf_number,aisle_no) values
('123asd','N/A','N/A','N/A','N/A',100)
select @salid = scope_identity()
insert into sal_product_locations (sal_product_id,side,position)
values (@salid,'N/A','N/A')
commit


could any one provide me solution for this

thanks,
-pab

Reply With Quote
  #2  
Old   
Kees Nuyt
 
Posts: n/a

Default Re: how insert data in two tables with single query in mysql - 11-24-2011 , 03:32 AM






On Wed, 23 Nov 2011 21:20:11 -0800 (PST), Pab
<prabu.net88 (AT) gmail (DOT) com> wrote:

Quote:
Hi,


i am trying to insert data two table with single query with
foreign key reference but its not working
"Is not working" does not tell us much. Please be more
specific in your problem description.


Quote:
my query is


BEGIN TRANSACTION
DECLARE @salid int
Insert into sal_products (upc,shelf_number,aisle_no) values
('123asd','N/A','N/A','N/A','N/A',100)
select @salid = scope_identity()
insert into sal_product_locations (sal_product_id,side,position)
values (@salid,'N/A','N/A')
commit
This code has several errors. I cannot imagine this is
the actual code you used. The error messages for these
errors are pretty clear.


Quote:
could any one provide me solution for this
We might once you tell us what the problem is.
Best regards,
--
( Kees Nuyt
)
c[_]

Reply With Quote
  #3  
Old   
Álvaro G. Vicario
 
Posts: n/a

Default Re: how insert data in two tables with single query in mysql - 11-24-2011 , 03:51 AM



El 24/11/2011 6:20, Pab escribió/wrote:
Quote:
i am trying to insert data two table with single query with
foreign key reference but its not working
As a general rule, when your code is not working the solution is to fix
it so it works

Quote:
my query is


BEGIN TRANSACTION
DECLARE @salid int
Insert into sal_products (upc,shelf_number,aisle_no) values
('123asd','N/A','N/A','N/A','N/A',100)
select @salid = scope_identity()
insert into sal_product_locations (sal_product_id,side,position)
values (@salid,'N/A','N/A')
commit


could any one provide me solution for this
Where's the single query here? I can see three queries. You can't just
omit the query separator (";" by default) and expect that MySQL will
magically compose a single query.

The answer to your question (insert data in two tables with single
query) can be found in the FAQ section of the manual:

http://dev.mysql.com/doc/refman/5.5/...daitem-B-1-1-4

However, I can't really see how your code attempt matches the question :-?



--
-- http://alvaro.es - Álvaro G. Vicario - Burgos, Spain
-- Mi sitio sobre programación web: http://borrame.com
-- Mi web de humor satinado: http://www.demogracia.com
--

Reply With Quote
  #4  
Old   
Tony Mountifield
 
Posts: n/a

Default Re: how insert data in two tables with single query in mysql - 11-24-2011 , 04:40 AM



In article <f1305dde-265c-4979-b184-6c9e9f7975a2 (AT) z22g2000prd (DOT) googlegroups.com>,
Pab <prabu.net88 (AT) gmail (DOT) com> wrote:
Quote:
Hi,


i am trying to insert data two table with single query with
foreign key reference but its not working
my query is


BEGIN TRANSACTION
DECLARE @salid int
Insert into sal_products (upc,shelf_number,aisle_no) values
('123asd','N/A','N/A','N/A','N/A',100)
select @salid = scope_identity()
insert into sal_product_locations (sal_product_id,side,position)
values (@salid,'N/A','N/A')
commit
This is a MySQL newsgroup. Your code above looks like it is for MS
SQL Server. If it is, then you should try comp.databases.ms-sqlserver
instead.

Cheers
Tony
--
Tony Mountifield
Work: tony (AT) softins (DOT) co.uk - http://www.softins.co.uk
Play: tony (AT) mountifield (DOT) org - http://tony.mountifield.org

Reply With Quote
  #5  
Old   
Pab
 
Posts: n/a

Default Re: how insert data in two tables with single query in mysql - 11-24-2011 , 07:24 AM



in the above query i am trying to insert values to sal_products table
and then i am taking that id using scope_identity and then i am trying
to insert that id with some values in sal_product_locations table but
is show some error near aisle_' which is error after that under score
symbol


thanks,
-pab

Reply With Quote
  #6  
Old   
Jerry Stuckle
 
Posts: n/a

Default Re: how insert data in two tables with single query in mysql - 11-24-2011 , 09:17 AM



On 11/24/2011 8:24 AM, Pab wrote:
Quote:
in the above query i am trying to insert values to sal_products table
and then i am taking that id using scope_identity and then i am trying
to insert that id with some values in sal_product_locations table but
is show some error near aisle_' which is error after that under score
symbol


thanks,
-pab
As Tony said - this looks like Microsoft SQL Server. This newsgroup is
for MySQL, an entirely different RDBMS.

Please ask your question in comp.databases.ms-sqlserver.

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex (AT) attglobal (DOT) net
==================

Reply With Quote
  #7  
Old   
Luuk
 
Posts: n/a

Default Re: how insert data in two tables with single query in mysql - 11-24-2011 , 01:25 PM



On 24-11-2011 06:20, Pab wrote:
Quote:
Hi,


i am trying to insert data two table with single query with
foreign key reference but its not working
my query is


BEGIN TRANSACTION
DECLARE @salid int
Insert into sal_products (upc,shelf_number,aisle_no) values
('123asd','N/A','N/A','N/A','N/A',100)
After the tablename (sal_products) you specify 3 field names
(upc,shelf_number,ais_no), but after values you have 5 values
('123asd','N/A','N/A','N/A','N/A',100)

This cannot be correct.


Quote:
select @salid = scope_identity()
insert into sal_product_locations (sal_product_id,side,position)
values (@salid,'N/A','N/A')
commit


could any one provide me solution for this
As others said, please provide the exact error message!

--
Luuk

Reply With Quote
  #8  
Old   
Pab
 
Posts: n/a

Default Re: how insert data in two tables with single query in mysql - 11-25-2011 , 12:05 AM



actually i need mysql query only
how can i perform insert data in two table with single query and i
also need id from first table to insert that id to second table column
sal_product_id

Insert into sal_products
(upc,store_area,section_aisle,shelf_number,aisle_n o,location) values
('123asd','N/A','N/A','N/A','N/A',100)
insert into sal_product_locations (sal_product_id,side,position)

pls provide me a solution


thanks,
-pab

Reply With Quote
  #9  
Old   
Axel Schwenke
 
Posts: n/a

Default Re: how insert data in two tables with single query in mysql - 11-25-2011 , 03:11 AM



Pab <prabu.net88 (AT) gmail (DOT) com> wrote:

Quote:
actually i need mysql query only
how can i perform insert data in two table with single query
You cannot.
But you can use two queries as in the example you have shown.

Quote:
and i
also need id from first table to insert that id to second table column
sal_product_id
While I have not seen you using an auto-generated id in your
example; MySQL does have AUTO_INCREMENT columns and you can
refer to the last generated values with the LAST_INSERT_ID()
SQL function. Like so:

BEGIN;
INSERT INTO sal_products (...) VALUES (...);
INSERT INTO sal_product_locations (...) VALUES (LAST_INSERT_ID(), ...);
COMMIT;

This is one transaction with 2 queries.

Quote:
pls provide me a solution
You want to read the fine manual:

http://dev.mysql.com/doc/refman/5.1/en/insert.html
http://dev.mysql.com/doc/refman/5.1/...increment.html
http://dev.mysql.com/doc/refman/5.1/en/commit.html


XL

Reply With Quote
Reply




Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.3
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.