Solved

Insert into

Posted on 2013-01-07
8
318 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
Creating Instructional Tutorials  

For Any Use & On Any Platform

Contextual Guidance at the moment of need helps your employees/users adopt software o& achieve even the most complex tasks instantly. Boost knowledge retention, software adoption & employee engagement with easy solution.

 
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

Ransomware: The New Cyber Threat & How to Stop It

This infographic explains ransomware, type of malware that blocks access to your files or your systems and holds them hostage until a ransom is paid. It also examines the different types of ransomware and explains what you can do to thwart this sinister online threat.  

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Select Sum query with group by 8 41
VM SQL server license. 1 52
What does "Between" mean? 6 34
Need split for SQL data 2 17
I have a large data set and a SSIS package. How can I load this file in multi threading?
In this article we will learn how to fix  “Cannot install SQL Server 2014 Service Pack 2: Unable to install windows installer msi file” error ?
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.
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.

736 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