Solved

Insert into

Posted on 2013-01-07
8
319 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
The Ultimate Checklist to Optimize Your Website

Websites are getting bigger and complicated by the day. Video, images, custom fonts are all great for showcasing your product/service. But the price to pay in terms of reduced page load times and ultimately, decreased sales, can lead to some difficult decisions about what to cut.

 
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 500 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

The Ultimate Checklist to Optimize Your Website

Websites are getting bigger and complicated by the day. Video, images, custom fonts are all great for showcasing your product/service. But the price to pay in terms of reduced page load times and ultimately, decreased sales, can lead to some difficult decisions about what to cut.

Question has a verified solution.

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

Why is this different from all of the other step by step guides?  Because I make a living as a DBA and not as a writer and I lived through this experience. Defining the name: When I talk to people they say different names on this subject stuff l…
A Stored Procedure in Microsoft SQL Server is a powerful feature that it can be used to execute the Data Manipulation Language (DML) or Data Definition Language (DDL). Depending on business requirements, a single Stored Procedure can return differe…
Via a live example combined with referencing Books Online, show some of the information that can be extracted from the Catalog Views in SQL Server.
Using examples as well as descriptions, and references to Books Online, show the documentation available for datatypes, explain the available data types and show how data can be passed into and out of variables.

691 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