Copy one table data into another db by using INTO Select - sql-server

I am getting this error :
Msg 156, Level 15, State 1, Line 39
Incorrect syntax near the keyword 'in'
My code:
SELECT *
INTO EmployeesBackup IN 'DB2.mbd'
FROM Employee

Your syntax is off, and perhaps you intended to do an INSERT INTO ... SELECT:
INSERT INTO DB2.EmployeesBackup
SELECT *
FROM DB1.Employee;
This would work assuming that both databases are on the same server, and that your backup table EmployeesBackup has the same definition as the Employee table.

Try this:
INSERT INTO [DBName].dbo.EmployeesBackup
SELECT * FROM [DBName].dbo.Employee
Please note that, Either both database should be available on the same SQL Server OR if one of them is available on the different server then it should be Linked

Related

Invalid column name when running a query in SQL Server 2008

I am trying to run a query in SQL Server 2008. It looks like this:
IF EXISTS (SELECT name FROM sysobjects WHERE name = "Bonds" AND type = 'U')
DROP table Bonds
GO
When I run this, I get this error:
Msg 207, Level 16, State 1, Line 2
Invalid column name 'Bonds'.
Msg 28102, Level 16, State 1, Line 3
This query was created by SQL Server. I am trying to run it in a different computer. Then I face this issue.
I have tried Ctrl+Shift+R as this post: SQL Server Invalid Column name after adding new column. But it is not helping.
Need some guidance on this.
Change
WHERE name = "Bonds"
to
WHERE name = 'Bonds'
Otherwise "Bonds" is treated like a column-name which does not exist.
use single quotes in search condition
WHERE name = 'Bonds'
I think you can also use
SET QUOTED_IDENTIFIER OFF;
before your query.

Select Statements in SQL Server

I am trying to pull up one specific line from a database that I just typed information into. This is the SELECT Statement that I typed in:
SELECT John
FROM FoodLog
It comes up with the following error statement:
Msg 207, Level 16, State 1, Line 1
Invalid column name 'John'.
However if I type the following SELECT Statement, it pulls up all of the information that I typed into the database:
SELECT Person
FROM FoodLog
I can't figure out how to pull up just one particular line of information from the database.
SELECT *
FROM FoodLog
WHERE Person = 'John'

Copy table from one server to another using select statement

I need to copy a table from one server to another.for that I have did the below code,
select * into tbls from SNRJDI-32962\xxxmanagement.master.dbo.tbl
When I execute I got error Like,
Msg 102, Level 15, State 1, Line 2
Incorrect syntax near '-'.
but this is the actual server name(SNRJDI-32962\xxxmanagement)..Please do needful..
Thank you
You first have to add a linked server from the target server to the source server.
Then you can use a four-part name, separated by dots:
select * into [newtable] from [linked_server].[databasename].dbo.[tablename]
I would add to Andomar's answer that to have special characters in an object name, you need to surround the name in [square brackets] otherwise sql will interpret your "-" as a minus sign

create table from another table in different database in sql server 2005

I have a database "temp" with table "A". I created new database "temp2".
I want to copy table "A" from "temp" to a new table in "temp2" . I tried this statement but it says I have incorrect syntax, here is the statement:
CREATE TABLE B IN 'temp2'
AS (SELECT * FROM A IN 'temp');
Here is the error:
Msg 156, Level 15, State 1, Line 2
Incorrect syntax near the keyword 'IN'.
Msg 156, Level 15, State 1, Line 3
Incorrect syntax near the keyword 'IN'.
Anyone knows whats the problem?
Thanks in advance,
Greg
I've not seen that syntax before. This is what I normally use.
SELECT *
INTO temp2.dbo.B
FROM temp.dbo.A
You need to qualify enough of the table name for SQL Server to be able to identify the correct table.
The name structure is <server name>.<database name>.<schema>.<table>. As both tables live on the same server you can dispense with that, but still need the rest:
SELECT *
INTO temp2.dbo.B
FROM temp.dbo.A
If you want to create a new table in another DB from the current DB, run the query.
CREATE TABLE `destination_database_name`.table_dummy AS (
SELECT * FROM currentDB.table
)
If you don,t want the data and only want the shcema of table without data then u can use this approach also...
SELECT * INTO temp2.dbo.b
FROM temp.dbo.a where 1=0
Query should be:
SELECT * INTO temp2.dbo.b
FROM temp.dbo.a
If you don't want the data you can do:
SELECT TOP 0 * INTO temp2.dbo.b
FROM temp.dbo.a
The easiest way is by right click on table A from database temp, then click Script Table as => CREATE to => New Query Editor Window. This will create the script.
Then, change following 2 lines. and run it for new database.
USE [temp2]
....
CREATE TABLE [dbo].[B]
.....
Note that SELECT INTO wont copy the indexes. If you want them too, you can generate script from source;run in the target db and do insert into
insert into temp2.dbo.b (columns)
select columns from temp.dbo.a

MS SQL Server 2005 Copy data from one table to another

Hey all, I am trying to find out how to copy data from one table to another database table. I have two connections to two different databases. Ones called comp-DEV1 and the other SQLTEST. I am currently unable to copy data from my sorce table (SQLTEST) to my destination table (comp-DEV1).
This is the error:
Msg 102, Level 15, State 1, Line 2 Incorrect syntax near '-'.
Query:
INSERT INTO comp-DEV1.EMSSQL.dbo.tblCL
SELECT *
FROM SQLTEST.EMSSQL.dbo.tblCL
WHERE NOT EXISTS(SELECT *
FROM comp-DEV1.EMSSQL.dbo.tblCL
WHERE (SQLTEST.EMSSQL.dbo.tblCL.CID = comp-DEV1.EMSSQL.dbo.tblCL.CID)
)
Any help would be great :o)
David
Try wrapping your database names in brackets, such as:
INSERT INTO [comp-DEV1].EMSSQL.dbo.tblCL
SELECT *
FROM SQLTEST.EMSSQL.dbo.tblCL
WHERE NOT EXISTS(SELECT *
FROM [comp-DEV1].EMSSQL.dbo.tblCL
WHERE (SQLTEST.EMSSQL.dbo.tblCL.CID =
[comp-DEV1].EMSSQL.dbo.tblCL.CID)
)
Run the following statement first to check that you can read the source from the destination server:
SELECT *
FROM [comp-DEV1].EMSSQL.dbo.tblCL
Get that working first then you should be on your way...

Resources