Sql Server, duvida inserir dados em tabela existente via QUERY/SELECT
12/04/2016
0
Gostaria de saber, se existe alguma solução para tal problema,
creio que seja algo simples como um BULK INSERT ou um INSERT INTO, porem não obtive sucesso, até o momento.
Tenho 10 tabelas, e preciso fazer um select simples nelas, e unir os resultados dos selects todos em uma tabela só,
até hoje faço manual, separo os resultados em TXT, e realizo o BULK INSERT nesta planilha padrão.
existiria alguma solução para que o resultado da QUERY seja adicionada automaticamente na tabela padrão?
obrigado desde já!
forte abraço!
Arthur Micheloni
Post mais votado
12/04/2016
insert into tabela_destino (codigo, nome, idade, telefone) select codigo, nome, idade, telefone from tabela_origem;
Raylan Zibel
Mais Posts
12/04/2016
Jair N.
Bem em tudo tem macetes e gambiarras que ajudam a resolver, eu mesmo tenho meu método personalizado de recuperação, principalmente de bases bichadas que não conseguem fazer backup...
12/04/2016
Arthur Micheloni
insert into tabela_destino (codigo, nome, idade, telefone) select codigo, nome, idade, telefone from tabela_origem;
dai eu add o where depois disso?
vou testar
12/04/2016
Arthur Micheloni
gratidão!
INSERT INTO SELECT inserts into an existing table.
SELECT INTO creates a new table and puts the data in it.
All of the columns in the query must be named so each of the columns in the table will have a name. This is the most common mistake I see for this command.
The data type and nullability come from the source query.
If one of the source columns is an identity column and meets certain conditions (no JOINs in the query for example) then the column in the new table will also be an identity.
Basic syntax
INSERT INTO SELECT
CREATE TABLE MyTable (name varchar(255));
GO
INSERT INTO MyTable
SELECT name
FROM sys.databases;
GO
SELECT INTO
SELECT name INTO MyTable
FROM sys.databases;
GO
fonte: https://www.toadworld.com/platforms/sql-server/b/weblog/archive/2014/12/03/insert-into-select-vs-select-into
Clique aqui para fazer login e interagir na Comunidade :)