Solved

Insert into

Posted on 2013-01-07
8
315 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
  • 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
Use Case: Protecting a Hybrid Cloud Infrastructure

Microsoft Azure is rapidly becoming the norm in dynamic IT environments. This document describes the challenges that organizations face when protecting data in a hybrid cloud IT environment and presents a use case to demonstrate how Acronis Backup protects all data.

 
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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Suggested Solutions

Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
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.
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function

830 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