Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2000 Forums
 Transact-SQL (2000)
 insert into a table in one statment

Author  Topic 

tarz
Starting Member

31 Posts

Posted - 2007-06-19 : 13:16:14
Hi,

Is there away to insert into 2 tables in one statment.

for example:
insert into tbl_menu values ('google', 1)
insert into tbl_links values (1, 'http://google.ca')

but do it in one line..

Thank you,

rmiao
Master Smack Fu Yak Hacker

7266 Posts

Posted - 2007-06-19 : 13:17:29
No you can't.
Go to Top of Page

cvraghu
Posting Yak Master

187 Posts

Posted - 2007-06-19 : 13:24:10
There is a complicated way for this

1. Create a view combining both tables.
2. Write a InsteadOf trigger.


Go to Top of Page

tarz
Starting Member

31 Posts

Posted - 2007-06-19 : 13:28:05
Thank you I will check out how to write it in a view. I am new to SQL.
I also get this error when I do one insert.. Could someone tell me what does this mean..

The INSERT statement conflicted with the FOREIGN KEY constraint "FK_tbl_menu_tbl_links". The conflict occurred in database "prj666_072a03", table "dbo.tbl_links", column 'linkNum'. The statement has been terminated.

Thank you for your help,
Go to Top of Page

Michael Valentine Jones
Yak DBA Kernel (pronounced Colonel)

7020 Posts

Posted - 2007-06-19 : 13:45:54
quote:
Originally posted by tarz

Thank you I will check out how to write it in a view. I am new to SQL.
I also get this error when I do one insert.. Could someone tell me what does this mean..

The INSERT statement conflicted with the FOREIGN KEY constraint "FK_tbl_menu_tbl_links". The conflict occurred in database "prj666_072a03", table "dbo.tbl_links", column 'linkNum'. The statement has been terminated.

Thank you for your help,



Forget trying to do it in a view with a before trigger. That is not something for someone who is new to SQL and there is rarely a reason for some one who is experienced with SQL to do that.

Keep it simple, and just write two (or however many you need) insert statements.

For the error you are getting now, try reversing the order of the insert statements. Any value in a lookup (referenced) table must be in the lookup table before you can insert a row that references it.









CODO ERGO SUM
Go to Top of Page

tarz
Starting Member

31 Posts

Posted - 2007-06-19 : 14:14:47
Hi Michael,
It worked once I reversed the order of the insert statement.
I was wondering how did you know that I had to reverse the order of the insert statement and what does lookup reference table mean?

Thanks alot for your help,
Go to Top of Page
   

- Advertisement -