Removing duplicated values from a table / Removendo valores duplicados de uma tabela

In T-SQL you may use "insert" to remove duplicates. What you must do is create another work table with the IGNORE_DUP_KEY option set.

So you copy the data from one table to the other. Drop the original table and rename the work table to your original table name and that's it. You will have a plain, clean, non duplicated records table. Something like:

CREATE TABLE tableCleanDup
(idfield int, field1 varchar(30), field2 varchar(30))
CREATE UNIQUE INDEX removeduplicates ON tableCleanDup (field1,field2) WITH IGNORE_DUP_KEY
GO
INSERT tableCleanDup SELECT * FROM tableOriginal

It will return to you a message "duplicate key was ignored" but that is fine. The tableCleanDup will have the records from tableOriginal but without duplicates.

p.s.: Thanks to Ken Henderson for this T-SQL advice.

Versão em Português

[More]

Must have books for web developers / Livros todo desenvolvedor web tem que ter

I am addicted to IT books. If this blog helped you somehow and you want to contribute please visit my amazon wish list

  • T-SQL (SQL Server 2000)
    1. To become a guru: All from Ken Henderson
    2. For begginers in T-SQL: SQL Server 2000 Programming by Example - Rojas
  • PHP
    1. PHP and MySQL Web Development - Luke Welling and Laura Thomson
  • ColdFusion
    1. All from Ben Forta
  • Javascript
    1. Javascript bible (gold edition)
  • CSS
    1. All from Eric Meyer
  • MySQL
    1. Quick Guide MySQL Crash Course - Ben Forta
    2. For critical web systems: High Performance MySQL - Jeremy D. Zawodny and Derek J. Balling
  • Web server performance and optimization
    1. Performance by Design: Computer Capacity Planning By Example


Eu sou viciado em livros especialmente de Tecnologia da Informação. Se esse blog lhe ajudou de alguma forma e vc quer retribuir por gentileza visite minha lista na amazon.com

Segue uma lista de livros que recomendo para todos os desenvolvedores web que trabalham fazendo sites dinâmicos. 

  • T-SQL (SQL Server 2000)
    1. Para se tornar um guru: Todos do Ken Henderson
    2. Para iniciantes em T-SQL: SQL Server 2000 Programming by Example - Rojas
  • PHP
    1. PHP and MySQL Web Development - Luke Welling and Laura Thomson
  • ColdFusion
    1. Todos do Ben Forta
  • Javascript
    1. Javascript bible
  • CSS
    1. Todos do Eric Meyer
  • MySQL
    1. Guia Rápido: MySQL Crash Course - Ben Forta
    2. Para sistemas criticos: High Performance MySQL - Jeremy D. Zawodny e Derek J. Balling
  • Web server performance and optimization
    1. Performance by Design: Computer Capacity Planning By Example

How to output a list of IDs in a field as recordsets / Como retornar uma lista de IDs como recordset

Sometimes we face issues on our jobs where we can't apply the right thory that we learned from the books and at school about database design.

Algumas vezes encontramos situações no trabalho onde não podemos aplicar a teoria correta no desenho do Banco de Dados.

In today's T-SQL tip, with the help of a DBA friend I have, I will demonstrate how to return a recordset from two tables where in one of them the content for a field is a list of IDs.

Na dica de T-SQL de hoje, com a ajuda de um amigo DBA, eu demostro como retornar um recordset apartir de duas tabelas onde em uma delas o conteúdo dos campos é uma lista de IDs.

Supose that the database tables structure can't be modified to a more normalized and theoretically correct. So imagine this two sample tables:

 Supondo que o banco de dados não pode ser alterado para uma forma normalizada teóricamente mais eficiente. Imagine essas duas tabelas:

CREATE TABLE [dbo].[products]
   ([id] [int] IDENTITY (1, 1) NOT NULL ,
   [name] [varchar] (50) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL) ON [PRIMARY]
GO
-- insert some sample data
INSERT INTO products (name)
VALUES ('product 1')
GO
INSERT INTO products (name)
values ('product 2')
GO
INSERT INTO products (name) values ('product 3')
GO
INSERT INTO products (name) values ('product 4')
GO
INSERT INTO products (name) values ('product 5')
GO
CREATE TABLE [dbo].[orders]
   ( [id_order] [int] IDENTITY (1, 1) NOT NULL ,
   [products] [varchar] (50) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL ) ON [PRIMARY]
GO
-- insert some sample data
INSERT INTO orders (products) VALUES ('1,3,4')
GO
INSERT INTO orders (products) VALUES ('2,3,5')
GO
INSERT INTO orders (products) VALUES ('3,4')
GO
INSERT INTO orders (products) VALUES ('1,5')
GO
INSERT INTO orders (products) VALUES ('3')
GO
INSERT INTO orders (products) VALUES ('2')

Now the select statement that makes the join between the two tables and return the recordset for each products on the orders table

Agora, o select que faz a junção das duas tabelas e retorna um recordset com cada produto na tabela orders.

SELECT o.id_order, p.name
FROM orders o INNER JOIN products p ON CHARINDEX(','+CAST(p.id as varchar)+',',','+o.products+',') > 0
ORDER BY o.id_order

The secret is the use of charindex with the commas at the start and end to find the IDs in the orders table.

O segredo esta em usar o charindex com as virgulas (',') no inicio e no final para encontrar os IDs contidos na tabela orders.

Update 1: A friend requested to make the select for a MS Access database. I don't recommend Access for web applications but this is the subject for another post. See the code bellow .

Atualização 1: Um amigo pediu para fazer o select se o banco de dados fosse Microsoft Access. Não recomendo o Access para aplicações web por diversos motivos. Principalmente por problemas de performance e recursos que lhe faltam e serão motivos para um outro post. Segue o Select no Access.

SELECT o.id_order, p.name
FROM orders o INNER JOIN products p ON INSTR(',' & o.products & ',' , ',' & CSTR(p.id) & ',') > 0
ORDER BY o.id_order ASC

If you need to migrate from Access to SQL or you need to know the main difference between them visit this page

Se vc precisa migrar do Access para o SQL Server ou precisa saber as diferenças principais veja essa página.


Consumed By Feed-Squirrel.com