• Home
  • About Me
  • Resume
  • Useful Links
  • Português
  • Contact

EmanuelCosta.com

E-Commerce, SEO/SEM/Online Marketing, Web Development, Usability, WordPress & WooCommerce

  • LinkedIn
  • Twitter
Home » Archives for July 15, 2007

Archives for July 15, 2007

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

Posted on July 15, 2007 Written by EmanWeb

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

Filed Under: SQL Server and T-SQL

Find Here

About me

Hello, my name is Emanuel Costa. I build professional websites and help companies grow online. I write this blog to help myself about what I learned and perhaps help other people. I help organize meetups and whenever I can I attend tech events, specially WordCamps. Read more about me. (p.s.: Agora escrevo um blog em Português aqui: emanweb.com.br

Latest WordCamp:

WordCamps I Attended:

I am volunteering at WordCamp Las Vegas 2019″ title=

WordCamp Fortaleza

I'm attending WordCamp San Diego

I am Attending WordCamp Miami 2016


WordCamp US

Categories

  • ActionScript
  • Blogs
  • ColdFusion
  • CSS
  • Experience
  • Flex and Flash
  • Gadgets
  • Google
  • JavaScript
  • Linux
  • MySQL
  • PHP
  • SEO
  • SEO and Internet Marketing
  • SQL Server and T-SQL
  • Uncategorized
  • video
  • Web Development
  • WooCommerce
  • wordcamp
  • WordPress
July 2007
M T W T F S S
 1
2345678
9101112131415
16171819202122
23242526272829
3031  
    Aug »

Recent Comments

  • Peter Crouch on How to output a list of IDs in a field as recordsets / Como retornar uma lista de IDs como recordset
  • Simon on cPanel on AWS EC2 using Amazon Linux w/ Yours Nameservers
  • WordPress Developer? Really? - EmanuelCosta.com on Gracie Brazilian Jiu Jitsu and WordPress. What these two have in common?

Navigation

  • Home
  • About Me
  • Resume
  • Useful Links
  • Português
  • Contact

Need quick tech advice? Pick my brain:

Copyright © 2025 · Focus Pro Theme on Genesis Framework · WordPress · Log in