Insert values from another Table in sql server 2008 - sql

I have to copy all column data to another table.I have created a new blank table.How to insert the values in it.I am avoiding writing the column name manually because it contain 35 column name in it.Sequence & name of column are same in both the table..?

If the tables have the same columns and types, just do;
INSERT INTO table2 SELECT * FROM table1;
Demo here.

use following stcript:
INSERT INTO "table1" ("column1", "column2", ...)
SELECT "column3", "column4", ...
FROM "table2"
for more information see:
http://www.1keydata.com/sql/sqlinsert.html

Create table2 with columns and datatype for each column. If the columns match up exactly on both tables, then insert into table2 from table1
Create table table2(
column1 datatype,
column2 datatype,
column3 datatype,
column35 datatype
}
INSERT INTO table2
SELECT * from table1

create table2
insert into table2
select * from table1

Please find my verion . i had same column name in both tables
INSERT INTO first_table
(column_1,
column_2,
column_3,
column_etc)
SELECT tab2.column_1 AS column_1,
10 AS column_2,
Getdate() AS column_3,
'some_text' AS column_etc
FROM second_table tab2 (nolock)

insert into dbo.FolderStatus
(
[FolderStatusId],
[code],
[title],
[last_modified]
)
select
[code],
[code],
[title],
[last_modified]
from dbo.f_file_stat

Related

select table to select from, dependent on column-value of already given table

for my intention I have to select a table to select columns from dependent on the column-value of an already given table.
First I thought about a CASE construct, if this is possible with sqlite.
SELECT * FROM
CASE IF myTable.column1 = "value1" THEN (SELECT * FROM table1 WHERE ...)
ELSE IF myTable.column1 = "value2" THEN (SELECT * FROM table2 WHERE ...)
END;
I am new to SQL. What construct would be the most concise (not ugly) solution and if I cannot have it in sqlite, what RDBM would be the best fit?
Thanks
Here is a proposal for associating a value from one of two tables for each entry in mytable. I.e. this is making the assumption that mytable does not only contain a single entry for choosing the secondary table.
For details on what this means, see "MCVE" at the end of this answer.
If you want to switch between two secondary tables, based on a single entry in main table, see at the very end of this answer.
Details:
a hardcoded "value1"/"value2" as column1 added on the fly to the result from secondary tables
joining by the faked colummn1 and a secondary join-key, assumption here id
a union all to make a single table from both secondary tables (including the fake column1)
select *
from mytable
left join
(select 'value1' as column1, * from table1
UNION ALL
select 'value2' as column1, * from table2)
using(id, column1);
Output (for the MCVE provided below, "a-f" from table1, "A-Z" from table2):
value1|1|a
value2|2|B
value1|3|c
value1|4|d
value2|5|E
value2|6|F
MCVE:
PRAGMA foreign_keys=OFF;
BEGIN TRANSACTION;
CREATE TABLE mytable (column1 varchar(10), id int);
INSERT INTO mytable VALUES('value1',1);
INSERT INTO mytable VALUES('value2',2);
INSERT INTO mytable VALUES('value1',3);
INSERT INTO mytable VALUES('value1',4);
INSERT INTO mytable VALUES('value2',5);
INSERT INTO mytable VALUES('value2',6);
CREATE TABLE table2 (value varchar(2), id int);
INSERT INTO table2 VALUES('F',6);
INSERT INTO table2 VALUES('E',5);
INSERT INTO table2 VALUES('D',4);
INSERT INTO table2 VALUES('C',3);
INSERT INTO table2 VALUES('B',2);
INSERT INTO table2 VALUES('A',1);
CREATE TABLE table1 (value varchar(2), id int);
INSERT INTO table1 VALUES('a',1);
INSERT INTO table1 VALUES('b',2);
INSERT INTO table1 VALUES('c',3);
INSERT INTO table1 VALUES('d',4);
INSERT INTO table1 VALUES('e',5);
INSERT INTO table1 VALUES('f',6);
COMMIT;
For selecting between two tables based on a single entry in main table (in this case "mytable2":
select * from table1 where (select column1 from mytable2) = 'value1'
union all
select * from table2 where (select column1 from mytable2) = 'value2';
Output (with mytable2 only containing 'value1'):
a|1
b|2
c|3
d|4
e|5
f|6

Combining INSERT SELECT with constants

I'm trying to insert records into a table where one of the columns comes from another table.
The other two columns are the same for each record.
All three columns are keys.
I'm trying this embedded INSERT SELECT which I see is not allowed?
INSERT INTO TABLE (COLUMN_A, COLUMN_B, COLUMN_C)
VALUES (1,(SELECT COLUMN_NAME FROM TABLE) ,2)
Your syntax is off - this is the correct syntax for this:
Insert Into Table
(Column_A, Column_B, Column_C)
Select 1, Column_Name, 2
From OtherTable
I would post a comment but I don't have enough 'status' yet.
The insert below works in PostgreSQL. What database are you using?
insert into people_experiences (qty, created_at, updated_at, person_id, experience_id) values (1, now(), now(), (select id from people where first_name='Joseph'), (select id from experiences where experience='MS'));
INSERT 0 1
We can even use this
Insert Into Table
(Select "value-1" as column, "value-2" as column, ot.* from otherTable ot)
for two column i want constant values and rest should be use as it is.
Note: Insert select will not allow virtual column.

Copy data into another table

How to copy/append data from one table into another table with same schema in SQL Server?
Edit:
let's say there is a query
select *
into table1
from table2
where 1=1
which creates table1 with the same schema as well as data as in table2.
Is there any short query like this to only copy entire data only into an already existing table?
If both tables are truly the same schema:
INSERT INTO newTable
SELECT * FROM oldTable
Otherwise, you'll have to specify the column names (the column list for newTable is optional if you are specifying a value for all columns and selecting columns in the same order as newTable's schema):
INSERT INTO newTable (col1, col2, col3)
SELECT column1, column2, column3
FROM oldTable
Simple way if new table does not exist and you want to make a copy of old table with everything then following works in SQL Server.
SELECT * INTO NewTable FROM OldTable
This is the proper way to do it:
INSERT INTO destinationTable
SELECT * FROM sourceTable
INSERT INTO table1 (col1, col2, col3)
SELECT column1, column2, column3
FROM table2
Try this:
INSERT INTO MyTable1 (Col1, Col2, Col4)
SELECT Col1, Col2, Col3 FROM MyTable2
Try this:
Insert Into table2
Select * from table1
Insert Selected column with condition
INSERT INTO where_to_insert (col_1,col_2) SELECT col1, col2 FROM from_table WHERE condition;
Copy all data from one table to another with the same column name.
INSERT INTO where_to_insert
SELECT * FROM from_table WHERE condition;
INSERT INTO DestinationTable(SupplierName, Country)
SELECT SupplierName, Country FROM SourceTable;
It is not mandatory column names to be same.
CREATE TABLE `table2` LIKE `table1`;
INSERT INTO `table2` SELECT * FROM `table1`;
the first query will create the structure from table1 to table2 and second query will put the data from table1 to table2
Copy all columns from one table to another table:
INSERT INTO table2
SELECT * FROM table1
WHERE condition;
Copy only some columns from one table into another table:
INSERT INTO table2 (column1, column2, column3, ...)
SELECT column1, column2, column3, ...
FROM table1
WHERE condition;
You can duplicate or "clone" a table's contents by executing:
CREATE TABLE new_table AS SELECT * FROM original_table;
-- for Sql Server users.
if you don't have the new table then you can create the new table with same structure as old table, and also copy data over from old table to the new table. For example:
select * into new_table
from old_table;
also you can copy the column / table structure, and just some of data. For example:
select * into new_table
from old_table
where country = 'DE';

How to select the record from table and insert into another table?

I wanted to select the last record from the table1 and insert into another table .Here is my query.
Insert into table2 values(select top 1 col1,col2 from table1 order by id desc).
I know for adding the value into table,need to be in cotation.But where to add?
You can select literals to fill in the other columns that table1 can't provide, something like this:
insert into table2 (col_a, col_b, col_c, col_d)
select top 1 col1, col2, 'foo', 'bar'
from table1
order by id desc
Any columns you do not name in the column list will get the default value, or null if no default is defined.
The number and type of columns selected must match the number and type of columns in the insert column list.
In SQL, there are essentially basically two ways to INSERT data into a table: One is to insert it one row at a time, the other is to insert multiple rows at a time. Let's take a look at each of them individually:
INSERT INTO table_name (column1, column2, ...)
VALUES ('value1', 'value2', ...)
The second type of INSERT INTO allows us to insert multiple rows into a table. Unlike the previous example, where we insert a single row by specifying its values for all columns, we now use a SELECT statement to specify the data that we want to insert into the table. If you are thinking whether this means that you are using information from another table, you are correct. The syntax is as follows:
INSERT INTO table1 (column1, column2, ...)
SELECT t2.column3, t2.column4, ...
FROM table2 t2
So, in you case, you can do it like this:
Insert into table2
select top 1 t1.col1,t1.col2 from table1 t1 order by id desc
Or you can use your syntax like this:
declare #col1 type_of_col1, #col2 type_of_col2
select top 1 #col1 = t1.col1, #col2 = t1.col2 from table1 t1 order by id desc
Insert into table2 values(#col1, #col2)
Offcourse, this all works assuming that the column datatypes are matched.

select from one table and insert into another

I've got two tables.
Table_A (nid, vid, type, title, uid)
Table_B (id, questiontext)
I need to insert records from Table_B into Table_A. I tried this:
INSERT INTO Table_A (nid, vid, type, title, uid)
VALUES ('', '', multichoice', (SELECT questiontext from Table_B), '1')
but it's throwing an error.
What should be the correct statement?
UPD: I should add that nid is autoincrement and the value of vid should be same as nid.
Have you tried
INSERT INTO Table_A (nid, vid, type, title, uid)
SELECT '',
'',
'multichoice',
questiontext ,
'1'
from Table_B
Have a look at INSERT ... SELECT Syntax
You should use the following SQL query:
INSERT INTO Target(A, B, C)
SELECT A, B, C
FROM Source
According to the the MySQL reference for INSERT SELECT:
INSERT INTO table_name SELECT FROM other_table [ WHERE ... something ... ]
insert into table2(columnname)select columnname from table1
use this method
INSERT INTO destination (column names ) (select columnaes from example 3 );
Column should be same type here .
INSERT INTO Table_1 (column_1, column_2, column_3)
SELECT column_1, column_2, column_3
FROM Table_2
You can also set some conditions for inserting by as follows:
INSERT INTO Table_1 (column_1, column_2, column_3)
SELECT column_1, column_2, column_3
FROM Table_2
WHERE (Condition)
// if dataset is unique then only we can append data to master table otherwise no data will be appended
// To append unique data you can use this logic
INSERT INTO ut_axis_karvy_master
(sr_no, funding_ac, funding_nm, micr_no,abc)
SELECT
t1.sr_no,
t1.funding_ac,
t1.funding_nm,
t1.micr_no,
t2.id
from ut_axis_karvy
left join
ut_axis_karvy_master as t2
on t1.micr_no = t2.micr_no
having t2.id is null;
I think the correct answer to this might be select into, from what I see from the other answers is that you guys insert before getting the value from table B as you should first get the value that is : SELECT from table B then insert into table A. you should be searching on the lines of select into