If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Database Server Software > Microsoft SQL Server > Union returns duplicates

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 01-07-12, 06:36
bevans bevans is offline
Registered User
 
Join Date: Jan 2012
Posts: 1
Union returns duplicates

This SQL is meant to show the changes that will be made, when removing a selected user's email address from a batch.
However, when executed, each row is duplicated, and in the duplication, the semi-colon or comma isn't removed.
For example, if I wanted to remove user "sam@mail.com"

The table results displayed would be:

Row 1:
BatchID: 50
ParamName:EmailTo
ParamValue: jack@mail.com;sam@mail.com;frank@mail.com
NewParamValue: jack@mail.com;frank@mail.com

Row 2:
BatchID: 50
ParamName:EmailTo
ParamValue: jack@mail.com;sam@mail.com;john@mail.com
NewParamValue: jack@mail.com;;frank@mail.com

Ideally, it should only display each row once, and not have the semicolon error.
It seems to be a union error, because when I comment out the First and second union statements, it runs fine.



-- Delete email address from a.Batch

IF(@EmailAddress IS NOT NULL)
BEGIN
IF(LEN(@EmailAddress) > 0)
BEGIN
IF(@ShowOnly = 1)
BEGIN
SELECT DISTINCT BatchID, Name ParamName, Value ParamValue, NewParamValue
FROM (
SELECT *, REPLACE(VALUE, @EmailAddress + ',', '') NewParamValue
FROM a.Batch
WHERE VALUE LIKE '%' + @EmailAddress + ',' + '%'
UNION
SELECT *, REPLACE(VALUE, @EmailAddress + ';', '') NewParamValue
FROM a.Batch
WHERE VALUE LIKE '%' + @EmailAddress + ';' + '%'
UNION
SELECT *, REPLACE(VALUE, @EmailAddress, '') NewParamValue
FROM a.Batch
WHERE VALUE LIKE '%' + @EmailAddress + '%'
) emails
END


Any help is much appreciated
Reply With Quote
  #2 (permalink)  
Old 01-09-12, 13:01
blindman blindman is online now
World Class Flame Warrior
 
Join Date: Jun 2003
Location: Ohio
Posts: 11,733
Duplicates are being returned because the rows are not really duplicates. There is some difference between them.
The culprit is likely the "SELECT *", which is not good practice. Enumerate only the columns you need.
__________________
If it's not practically useful, then it's practically useless.

blindman
www.chess.com: "sqlblindman"
Reply With Quote
  #3 (permalink)  
Old 01-09-12, 16:02
Brett Kaiser Brett Kaiser is offline
Window Washer
 
Join Date: Nov 2002
Location: Jersey
Posts: 10,303
VALUE?

This is a forum for SQL Server, not DB2
__________________
Brett
8-)

It's a Great Day for America everybody!

dbforums Yak CorralRadio 'Rita
dbForums Member List
I'm Good Once as I ever was

The physical order of data in a database has no meaning.
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On