MS SQL Server 2005 Copy data from one table to another - sql-server

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...

Related

Copy one table data into another db by using INTO Select

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

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.

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

Creating temporary view from a temporary table in SQL Server

I have a temporary table and I would like to create a temporary view over this temporary table.
Is it possible?
In following example I would like #Top10Records to be a view instead of a table so that I get
select * into #Top10Records from (select top 10 * from #MytempTable)
You can use a Common Table expression to do that:
WITH Top10Records AS
(
select top 10 * from #MytempTable
)
SELECT * FROM Top10Records
GO
Unfortunately, SQL Server doesn't support this:
Msg 4103, Level 15, State 1, Line 3
"#someView": Temporary views are not allowed.
Msg 4508, Level 16, State 1, Line 6
Views or functions are not allowed on temporary tables. Table names that begin with '#'
denote temporary tables.
SQL Server does not support temporary views as such and as stated above by Daryl, a Common Table Expression is probably the way to go. However, one limitation of a CTE is that it can't be used across multiple queries in a batch.
You can however create a standard view, use it as required then simply drop it at the end of the batch/transaction. (I know the OP question is whether or not you can create a temporary view, but this may also potentially apply - it requires a schema change, but a transient one for all intents and purposes).

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

Resources