INSERT in two tables based on SELECT

Asked

Viewed 913 times

1

Imagine the following scenario:

inserir a descrição da imagem aqui

At a certain point it is necessary to duplicate people and their respective items.

If I have the following data:

Table Person

+----+------------+
| Id | Nome       |
+----+------------+
| 1  | Joãozinho  |
| 2  | Mariazinha |
+----+------------+

Table Person

+----+------------+--------+
| Id | PessoaId   | ItemId |
+----+------------+--------+
| 1  | 1          | 1      |
| 2  | 1          | 2      |
| 3  | 2          | 1      |
+----+------------+--------+

Item Table

+----+------------+
| Id | Descricao  |
+----+------------+
| 1  | Item1      |
| 2  | Item2      |
+----+------------+

One can conclude from this that Joãozinho owns the Item1 and Item2, and Mariazinha has only the Item1.

By duplicating the data, we would have:

Table Person

+----+------------+
| Id | Nome       |
+----+------------+
| 1  | Joãozinho  |
| 2  | Mariazinha |
| 3  | Joãozinho  |
| 4  | Mariazinha |
+----+------------+

Table Person

+----+------------+--------+
| Id | PessoaId   | ItemId |
+----+------------+--------+
| 1  | 1          | 1      |
| 2  | 1          | 2      |
| 3  | 2          | 1      |
| 4  | 3          | 1      |
| 5  | 3          | 2      |
| 6  | 4          | 1      |
+----+------------+--------+

Item Table Remains as before.

To duplicate only one table is easy, just run the following script:

INSERT INTO Pessoa
(Nome)
SELECT Nome
FROM Pessoa

But in this case, which involves more than one table? What is the best way to do this?

I am using SQL Server.

  • You have to duplicate one by one, verify that the ids are valid, disable triggers, in some cases constraints, etc.

  • I think the most appropriate thing would really be to create a loop within a transaction and do one by one.

  • Ok. Even because, in any case, in the frying of the eggs, will be made one by one.

  • I understand, I’m searching the internet, but I can’t find anything that can help me create this loop to work one by one inside it.

  • I’ll post an example.

  • Why do you want to denormalize your database?

  • I don’t want to, but the client has my Whatsapp, hahaha...

Show 2 more comments

1 answer

1


DECLARE @campo VARCHAR(30)
    , @campo2 VARCHAR(30)
    , @campo3 VARCHAR(60)

-- Cursor para percorrer os nomes dos objetos 
DECLARE itens CURSOR FOR
    SELECT campo, campo2, campo3 
    FROM
        tabela_origem


-- Abrindo Cursor para leitura
OPEN itens

-- Lendo a próxima linha
FETCH NEXT FROM itens INTO @campo, @campo2, @campo3

-- Percorrendo linhas do cursor (enquanto houverem)
WHILE @@FETCH_STATUS = 0
BEGIN

    INSERT INTO TABELA2 VALUES( @campo, @campo2, @campo3 )

    -- Lendo a próxima linha
  FETCH NEXT FROM itens INTO @campo, @campo2, @campo3
END

-- Fechando Cursor para leitura
CLOSE itens

-- Desalocando o cursor
DEALLOCATE itens
  • Thank you @Ginaldo-Rigo I made the adaptations and it worked perfect here.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.