How to insert data from one table to anther table? - sql-server-2008

I have two tables,
tblA(id, num, col1, col2, col3),
tblB(col1, col2, col3)
col1, col2 and col3 are the same in both tables. Now I have following sql:
declare #num...(same type as num)
insert into tblA
select #num, * from tblB
id in tblA is an indentity column.
But I got following error,
Column name or number of supplied values does not match table definition.
Can anyone help me to fix it?

Can you please try providing the column names as well,
declare #num...(same type as num)
insert into tblA(num, col1, col2, col3)
select #num, * from tblB
Please don't worry about identity column as it will get filled automatically.

Just INSERT using named columns, and skip the identity column - it will be filled automatically:
INSERT INTO tblA (num, col1, col2, col3) SELECT #Num, col1, col2, col3 FROM tblB

I think the error message is quite explicative: the SELECT and the INSERT has to have the same number of columns.
in your case
declare #num...(same type as num)
insert into tblA(num,col1, col2, col3)
select #num,col1, col2, col3 from tblB
if the key on tblA is not auto-generated you have to consider it in the INSERT
more info here

It simply based on your column name they should be of same type:
insert into tblA(col1,col2,col3)
select col1,col2,col3
from tblB

Related

INSERT INTO with SELECT and exterior value

I'm trying to copy a line from a table to another table. But the second table have to received two additional parameters (hour and autor) which are not in the first table.
Here is my try:
INSERT INTO Individus_corbeille
(
SELECT *
FROM Individus
WHERE ID_individu='706782','NOW()','autor'
)
But it absolutely doesn't work...
INSERT INTO Individus_corbeille
SELECT *, NOW(), 'autor'
FROM Individus
WHERE ID_individu='706782'
But you should actually also name the columns you insert into and the ones you select like this
INSERT INTO Individus_corbeille (col1, col2, col3, col4)
SELECT col1, col2, NOW(), 'autor'
FROM Individus
WHERE ID_individu='706782'

Check whether a column exists or not in a table?

If you don't know what are the columns(or how many columns are there) in a table, how to check whether a particular column exists or not in that table using MySQL?
For example,
A table "DemoTable" contains columns Col1, Col2, Col3 with some data. And you write a select query like,
Select col1, col2, col3, col4 from DemoTable. But col4 does not exists in the DemoTable, so it will through an error. How to check whether col4 exists in the DemoTable in the select query or before the select query? Without a procedure.
There are two tables with same name in two DB's, in those tables there are few columns with same name, while others columns have different names. For example, DemoTable has columns col1, col2, col3, col4 in one DB and in other DB, DemoTable has columns col1, col2, col3, col5, col6. But the condition is, a single select query should be used to fetch data from the table without an error, so we need to check whether that columns first exists or not. Query like Select col1, col2, col5 from DemoTable from any DB.

MySql Duplicate a row and replace part of a field in the duplicate

I'm trying to duplicate a row in a mysql db and at the same time replace part of a string in one field of the duplicated row.
I've figured out how to:
duplicate a row
INSERT INTO account_external_ids
SELECT * FROM account_external_ids
where external_id like '%gerrit:%';
do the replace of the string but cant figure out how to do both in the same query.
UPDATE account_external_ids
SET external_id = REPLACE(external_id,'gerrit:','username:')
WHERE external_id like '%gerrit%';
But can't figure out how to do both in the same query, something like:
INSERT INTO account_external_id
select * from account_external_ids
set external_id = replace(external_id, 'gerrit:', 'username:')
where external_id like '%gerrit%';
Any pointers would be great.
thanks
You have to list all the columns explicitly, you can't use SELECT * if you're modifying any of the columns
INSERT INTO account_external_id (col1, col2, col3, col4, external_id)
SELECT col1, col2, col3, col4, REPLACE(external_id, 'gerrit:', 'username:')
FROM account_external_ids
WHERE external_id LIKE '%gerritt:%'

Copy all records between tables with the exception of a line [duplicate]

This question already has answers here:
INSERT INTO ... SELECT without detailing all columns
(11 answers)
Closed 8 years ago.
I have a syntax that can copy all records from tables that have the same fields:
INSERT INTO table2 SELECT * FROM table1 WHERE id = '7'
So I would like to know how do I keep copying all values ​​except the 'id' field (because him is primary key and auto increment).
You list the columns:
insert into table2(col1, . . . coln)
select col1, . . ., coln
from table1
where id = '7';
Under most circumstances, you should list the columns explicitly. You can run into problems even when tables have the same columns but in a different order.
(The one exception in code that I would write is in a script where table1 is created from table2 using create table as.)
You have to list all the columns specifically:
INSERT INTO table2 (col1, col2, col3, ...)
SELECT col1, col2, col3, ...
FROM table1
WHERE id = '7'
You'd need to specify the fields you're copying explicitly:
INSERT INTO table2 (col1, col2, col3, etc)
SELECT col1, col2, col3, etc
FROM table1
WHERE id = '7'

SQL Synchronising large table data

Hi i m using SQL 2008 R2.What is the faster way to insert data of 10 mil. records from one table A into another empty table B.
Table A and B dont have same schema similar but not the same.
It's only 10 million rows. Just use a normal INSERT
INSERT tableB (col1, col2, col3, ...)
SELECT col1, col2, col3, ... FROM tableA
or
--assumes no table b already
SELECT col1, col2, col3, ..., INTO TableB FROM tableA
.. now add some columns etc
Your using sql?
why not create a new table
and then insert records from table a into new table b??