?
Solved

Insert into

Posted on 2013-01-07
8
Medium Priority
?
320 Views
Last Modified: 2013-01-25
create table dba.tbl_member (member_name varchar(20))
create table dba.tbl_donor  (donor_name varchar(20))

INSERT INTO dba.tbl_member Values ( 'USA')
INSERT INTO dba.tbl_member Values ( 'CANADA')
INSERT INTO dba.tbl_member Values ( 'MEXICO')

INSERT INTO dba.tbl_donor Values ( 'USA')
INSERT INTO dba.tbl_donor Values ( 'ASIA')
INSERT INTO dba.tbl_donor Values ( 'BRAZIL')

select * from dba.tbl_member
select * from dba.tbl_donor

-- Now I need to insert all donor_name values from dba.tbl_donor to member_name.tbl_member table.
-- BUT IF the same name already exists in dba.tbl_member then prefix it with 'DL-' and insert else insert the name from 2nd to first table.
-- So here USA exists in both tables so insert 'DL-USA' in dba.tbl_member and then the rest 2 rows.

INSERT INTO dba.tbl_member (member_name)
(SELECT CASE WHEN a.donor_name =b.member_name THEN 'DL-' + substring(a.donor_name, 1, LEN(a.donor_name))  
ELSE a.donor_name END AS member_name FROM dba.tbl_donor a,dba.tbl_member b) -- BUT I THINK MY THIS QUERY NEEDS YOUR HELP
FROM dba.tbl_donor

So for this scenario I should be able to see total 6 rows in dba.tbl_member after insert.
0
Comment
Question by:cottage125
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
  • 2
  • +1
8 Comments
 
LVL 25

Expert Comment

by:TempDBA
ID: 38752375
You can use outer join for this.

INSERT INTO dba.tbl_member (member_name)
SELECT
           CASE WHEN b.member_name is null
                       THEN 'DL-' + a.donor_name
                       ELSE a.donor_name
           END AS member_name
FROM dba.tbl_donor a
left outer join dba.tbl_member b
on a.donor_name = b.member_name
0
 

Author Comment

by:cottage125
ID: 38752431
ITs giving wrong results see below,  thats now what I want.
member_name
'DL-ASIA'
'DL-BRAZIL'
'USA'

But I want,

member_name
'ASIA'
'BRAZIL'
'DL-USA'
0
 
LVL 41

Expert Comment

by:ralmada
ID: 38752438
try

INSERT INTO dba.tbl_member (member_name)
select case when b.member_name is null then a.donor_name else 'DL -' + a.donor_name end
FROM dba.tbl_donor a
left outer join dba.tbl_member b on a.donor_name = b.member_name
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 25

Expert Comment

by:TempDBA
ID: 38752478
<<-- BUT IF the same name already exists in dba.tbl_member then prefix it with 'DL-' and insert else insert the name from 2nd to first table.
-- So here USA exists in both tables so insert 'DL-USA' in dba.tbl_member and then the rest 2 rows.>>

I thought that's what you wanted. Anyway, Just reverse the case statemetn

INSERT INTO dba.tbl_member (member_name)
SELECT
           CASE WHEN b.member_name is not null
                       THEN 'DL-' + a.donor_name
                       ELSE a.donor_name
           END AS member_name
FROM dba.tbl_donor a
left outer join dba.tbl_member b
on a.donor_name = b.member_name
0
 
LVL 41

Expert Comment

by:ralmada
ID: 38752507
or even simpler

INSERT INTO dba.tbl_member (member_name)
select coalesce('DL -' + b.member_name, a.donor_name)
FROM dba.tbl_donor a
left outer join dba.tbl_member b on a.donor_name = b.member_name
0
 

Author Comment

by:cottage125
ID: 38752642
Okay Thats but is there any way I can use if exist or someting like that and dont use any joins?? Just wondering.
0
 
LVL 41

Expert Comment

by:ralmada
ID: 38752665
>> if exist or someting like that and dont use any joins?? Just wondering. <<

Why would you do that? The above suggestions are more efficient than using exists
0
 
LVL 75

Accepted Solution

by:
Anthony Perkins earned 2000 total points
ID: 38757411
Thats but is there any way I can use if exist or someting like that and dont use any joins?
Something like this perhaps:
INSERT  INTO dba.tbl_member (member_name)
SELECT  CASE WHEN EXISTS ( SELECT   1
                           FROM     dba.tbl_member b
                           WHERE    a.donor_name = b.member_name ) THEN 'DL-' + a.donor_name
             ELSE a.donor_name
        END AS member_name
FROM    dba.tbl_donor a

Open in new window

0

Featured Post

Get proactive database performance tuning online

At Percona’s web store you can order full Percona Database Performance Audit in minutes. Find out the health of your database, and how to improve it. Pay online with a credit card. Improve your database performance now!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article shows the steps required to install WordPress on Azure. Web Apps, Mobile Apps, API Apps, or Functions, in Azure all these run in an App Service plan. WordPress is no exception and requires an App Service Plan and Database to install
This post contains step-by-step instructions for setting up alerting in Percona Monitoring and Management (PMM) using Grafana.
Via a live example, show how to set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.

777 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question