Solved

Insert into

Posted on 2013-01-07
8
313 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
 
LVL 25

Expert Comment

by:TempDBA
Comment Utility
<<-- 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
Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

 
LVL 41

Expert Comment

by:ralmada
Comment Utility
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
Comment Utility
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
Comment Utility
>> 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
Comment Utility
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

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

Suggested Solutions

Introduction In my previous article (http://www.experts-exchange.com/Microsoft/Development/MS-SQL-Server/SSIS/A_9150-Loading-XML-Using-SSIS.html) I showed you how the XML Source component can be used to load XML files into a SQL Server database, us…
Does the idea of dealing with bits scare or confuse you? Does it seem like a waste of time in an age where we all have terabytes of storage? If so, you're missing out on one of the core tools in every professional programmer's toolbox. Learn how to …
Using examples as well as descriptions, and references to Books Online, show the documentation available for date manipulation functions and by using a select few of these functions, show how date based data can be manipulated with these functions.
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.

728 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

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now