to ask a question on join using insert statement

hello suppose my table name is xyz in which three columns are there and another table abc also consist of three column, having id column common n both of them.normally we have seen that joins are used using select statement but i dont wana select column using join. i wana insert column using joins.i want to make use of insert statement in join not select. plz anyone answer ma question.

more ▼

asked Feb 03, 2010 at 03:42 PM in Default

java gravatar image

9 4 4 4

(comments are locked)
10|1200 characters needed characters left

1 answer: sort voted first

I'm not 100% sure what you are trying to do, but I hope this helps.

    SELECT abc.id
    FROM abc
    	INNER JOIN xyz
    		ON xyz.id = abc.id

It will insert into table jki any id values that are found in both abc and xyz.

Another method that might work better for you in some situations is to use WHERE EXISTS:

INSERT INTO jki  (id)  SELECT abc.id  FROM abc  WHERE EXISTS  (SELECT * FROM xyz WHERE xyz.id = abc.id) 
more ▼

answered Feb 03, 2010 at 03:58 PM

Tom Staab gravatar image

Tom Staab
5.8k 6 8 10

(comments are locked)
10|1200 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

New code box

There's a new way to format code on the site - the red speech bubble logo will automatically format T-SQL for you. The original code box is still there for XML, etc. More details here.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here



Answers and Comments

SQL Server Central

Need long-form SQL discussion? SQLserverCentral.com is the place.



asked: Feb 03, 2010 at 03:42 PM

Seen: 947 times

Last Updated: Feb 03, 2010 at 04:00 PM