Problem with inserting duplicate values in SQL table

Shweta

Well-known Member
Joined
Jun 5, 2011
Messages
514
Hi All,

I have create a table using the below syntax:

Code:
create table tblProductSales (ProductID int, QuantitySold numeric)

I have not defined any column as Primary key.

Below are the values I am inserting in the above table.

Code:
insert into tblProductSales
select '1','10' union
select '3','23' union
select '4','21' union
[COLOR=#ff0000]select '2','12' union[/COLOR]
select '1','13' union
select '3','12' union
select '4','13' union
select '1','11' union
[COLOR=#ff0000]select '2','12' union[/COLOR]
select '1','14'

Highlighted two rows have duplicate entries for both the columns. While executing it, it inserts only 9 rows in the table instead of 10 and ignoring the one complete row from the highlighted ones.

Second issue is, it is storing the data in a sorted manner by ProductId. However, I don't want that. There is no cluster index then why it is doing so. Please suggest.

Thanks in advance!!

Regards,
Shweta Jain
 
Last edited:

Excel Facts

How can you automate Excel?
Press Alt+F11 from Windows Excel to open the Visual Basic for Applications (VBA) editor.
The order the data is stored should be irrelevant, as your queries are where you extract at in the order you want

as for the unallowed duplicate, are there any settings on the table that prevent duplicates ?
 
Upvote 0
Union operator remove the duplicates. Use Union all instead of Union.

For sorting problem you can also use this:

Code:
Create table tblProductSales (ProductID int, QuantitySold numeric)
Insert into tblProductSales
values
    ('1','10'),
    ('3','23'),
    ('4','21'),
    ('2','12'),
    ('1','13'),
    ('3','12'),
    ('4','13'),
    ('1','11'),
    ('2','12'),
    ('1','14')
 
Last edited:
Upvote 0
Thank you so much Ombir!

Your solution works perfectly fine for me. Sorting problem also got resolved by using union all.
 
Upvote 0

Forum statistics

Threads
1,221,810
Messages
6,162,108
Members
451,743
Latest member
matt3388

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top