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

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;

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;

Copy from one table with auto increment column

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.

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 copy one table to another

I want to copy the contents of table1 into table2, but it's not a straight copy, as table2 contains more columns than table 1. The structure is similar to this:
Table1
{
column2
column4
column6
}
Table2
{
column1
column2
column3
column4
column5
column6
}
What I want to do is add every row from table1 to table2, and set default values for the missing columns. Any help would be appreciated.
You can just do an
INSERT INTO xxx
SELECT yyy
And in the select clause, put default values.
INSERT INTO Table2(column1, column2, column3, column4, column5, column6)
SELECT 'horse', column2, 2, column4, 'what ever I want', column6
FROM Table1
So int Table2, all column1 will have 'horse' value.
All column3 will have 2.
Etc.
use INSERT..INTO SELECT statement
INSERT INTO table2 (column2, column4, column6)
SELECT column2, column4, column6
FROM table1
so in this case, columns: column1, column2, column3 will have null values. or whatever default you have set.
Not great at SQL but something like this:
INSERT INTO [Table1] (column2, column4, column6) SELECT (column1, column2, column3) FROM [Table2]
Hope this helps. Link that may be useful, http://www.blackwasp.co.uk/SQLSelectInsert.aspx
vote me I need points :P
Please try
INSERT INTO
Table2 (
column1,
column2,
column3,
column4,
column5,
column6,
)
SELECT
'StaticValue1',
column2,
'StaticValue2'
column4,
'StaticValue3'
column6,
FROM
Table1
To copy a full table into a new table:
SELECT * INTO table2 FROM table1;
http://www.w3schools.com/sql/sql_select_into.asp
To copy a table into an existing table:
INSERT INTO table2
SELECT * FROM table1;
http://www.w3schools.com/sql/sql_insert_into_select.asp
insert into TABLE2
select null colum1, column2,null colum3,column4,null colum5,column6
from((
Select TABLE1.column2, TABLE1.column4, TABLE1.column6
from TABLE1, TABLE2
where TABLE1.primarykey=TABLE2.primarykey))

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.