Copy from one table with auto increment column - sql

I want to copy the data from one table1 into table2. table1 does not have one of the columns that exists in table2 but it needs to be unique for my purpose.
My code is this so far:
set #uidfield=1000;
insert into table2 (column1, colulmn2, column3)
select column1, column2, (#uidfield := #uidfield+1)
from table1
This will copy about 6000 records, I don't have a good way to test my SQL statement, and I want to make sure its somewhat right before I send it to my SR analyst.
Thanks!

Are you looking for this?
insert into table2 (column1, colulmn2, column3)
select column1, column2,
1000 + row_number() over (order by (select null))
from table1;
This would be the SQL Server equivalent of your syntax, if I interpret your syntax as MySQL syntax.

Related

SQL Query - Ensuring my code is correct/Assistance with multiple queries in one Query (Subquerying)

I have a query today regarding SQL.
Basically here is what I am trying to do (this will also be useful for a couple other tables I have in this DB)
Table 1 = Members
Table 2 = Payments
Essentially trying to insert record into 1 table however have the query also copy over a memberID field if it's present for the individual.
INSERT INTO Payments (FirstName, LastName, PaymentMade)
VALUES ('', '', ''); AND UPDATE Payments
SET Payments.MemberID = Members.MemberID
FROM Members INNER JOIN Members ON Payments.MemberID = Members.MemberID;
Question is: Have I performed this correctly or have I missed a critical step here?
Many thanks! :)
I'm guessing you want to insert data from Table1 into Table2.
INSERT INTO table2 (column1, column2, column3, ...)
SELECT column1, column2, column3, ...
FROM table1
Use a Where condition for filtering out unwanted records.
INSERT INTO table2 (column1, column2, column3, ...)
SELECT column1, column2, column3, ...
FROM table1
WHERE condition;

SQL: copy new values from one SQL table to another

I am trying to copy values from one SQL table to another. The problem I have is that some of the values already exists in both tables and I only want to copy unique values. The below code should show what I ideally want (but which doesn't work).
INSERT INTO newTable (column1, column2, column3,ID)
SELECT column1, column2, column3,ID
FROM oldTable
WHERE Newtable.ID <> oldTable.ID
Can someone present a solution that works? Thanks in advance.
If you are using SQL server 2008+ you can use an except statement:
INSERT INTO newTable
(
column1,
column2,
column3,
ID
)
SELECT column1,
column2,
column3,
ID
FROM oldTable
EXCEPT
SELECT column1,
column2,
column3,
ID
FROM newTable;
You can try to use NOT exists on where clause.
INSERT INTO newtable
(column1,
column2,
column3,
id)
SELECT column1,
column2,
column3,
id
FROM oldtable o
WHERE NOT EXISTS (SELECT 1
FROM newtable t
WHERE t.id = o.id);
SQL-Server SQLFIDDLE
My-sql SQLFIDDLE
Oracle SQLFIDDLE
It really depends on the number of records you're dealing with.
This guy has benchmarked all the different ways of doing it. Which way is "better" depends on the number of records.
Also, if this is an ongoing process, you'll be better off with an INSERT trigger so you can just add the new records when they happen instead of needing to query the whole table every time.

How to delete the rownames column from a table in SQL Server?

I want to transfer rows from one table to another. However, one table has rownames while the other doesn't.
The error I get is:
Incorrect syntax near the word 'from'
How to handle this situation?
You can try:
INSERT INTO Table1 (Column1, Column2, Column3)
SELECT Column1, Column2, Column3
FROM Table2
WHERE Table2.Column1 = 'Test'
IF the columns are same in the both the table :
INSERT INTO table2
SELECT * FROM table1
WHERE condition;
If you want to transfer few rows but the the column names are different :
INSERT INTO table2 (column1, column2, column3) SELECT (column1, column2, column3) FROM table1 WHERE condition;

appending 2 columns into one list in sql

I have 2 tables and each table has some 3 columns. i want to get one column such that one column from each table are apended one after the other
eg:- suppose one column in a table contains hai, how, are, you.
and another column in another column contains i, am, fine.
i want a query which gives hai, how, are, you,i,am,fine. in just one column
can anybody give a query for this in sql...
If I understand your schema correctly you have this
Table1: Column1
hai,
how,
are,
you.
Table2: Column2
i,
am,
fine.
Do This:
Insert Into Table1 (Column1)
Select Column2 From Table2
You will get this:
Table1: Column1
hai,
how,
are,
you.
i,
am,
fine.
If you have 3 Columns
Then just do this:
Insert Into Table1 (Column1, Column2, Column3) //the (Column1, Column2, Column3) is not neccessary if those are the only columns in your Table1
Select Column1, Column2, Column3 From Table2 //the Select Column1, Column2, Column3 could become Select * if those are the only columns of your Table2
EDIT: Do this if you don't want to modify any tables.
Select Column1, Column2, Column3
From Table1
UNION ALL
Select Column1, Column2, Column3
From Table2
Your question isn't very clear. One interpretation of it is that you want to UNION the two:
select column
from table1
union
select column
from table2;
If you really want all rows from both tables (and not the distinct values), UNION ALL will be faster than UNION.
If you want the rows in a certain order be sure to specify an ORDER BY clause.

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';