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
 General SQL Server Forums
 New to SQL Server Programming
 insert from another table

Author  Topic 

a.ashabi
Posting Yak Master

117 Posts

Posted - 2008-08-06 : 18:32:31
Hi.I've wrote this Query:
SELECT DISTINCT cat_level2_id
FROM dbo.tbl_cat_relation
WHERE (cat_level3_id NOT IN
(SELECT tbl_configurator.cat_level2_id
FROM tbl_configurator))

the result shows me 127 rows in tbl_configurator which I dont have any cat_level2_id on tbl_relation.
now I want a query to insert those 127 rows to tbl_relation.
thank u so much 4 yr help.

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2008-08-07 : 02:29:53
quote:
Originally posted by a.ashabi

Hi.I've wrote this Query:
SELECT DISTINCT cat_level2_id
FROM dbo.tbl_cat_relation
WHERE (cat_level3_id NOT IN
(SELECT tbl_configurator.cat_level2_id
FROM tbl_configurator))

the result shows me 127 rows in tbl_configurator which I dont have any cat_level2_id on tbl_relation.
now I want a query to insert those 127 rows to tbl_relation.
thank u so much 4 yr help.



what you're checking above is just oppoiste. The query is looking for cat_level2_id values in tbl_relation which are not in tbl_configurator.

What you're asking for is this
INSERT INTO tbl_relation (cat_level2_id)
SELECT c.cat_level2_id
FROM tbl_configurator c
LEFT JOIN tbl_relation r
ON r.cat_level2_id=c.cat_level2_id
WHERE r.cat_level2_id IS NULL
Go to Top of Page

a.ashabi
Posting Yak Master

117 Posts

Posted - 2008-08-07 : 05:09:51
thank u again.worked :)
Go to Top of Page
   

- Advertisement -