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
Related
I need to update around 1000 xml records with a new xml node, the xml's are stored in a sql table column, with xml datatype, So I've been trying to utilize the modify function documented HERE (specifically example A.4)
However, When I run this against my staging environment (SqlServer 2016 SP2-CU13) I receive the following error message:
Msg 2395, Level 16, State 1, Line 7
XQuery [production.dbo.organisations.Configuration.modify()]: There is no function '{http://www.w3.org/2004/07/xpath-functions}:AFTER()'
The code is rudimentary, I'm just stumped by the error message. Any help or insight is greatly appreciated
Code:
update [production].[dbo].[organisations] Set configuration.modify('INSERT <LegalEntityName>TEST</LegalEntityName> AFTER (/OrganisationMetaData/ExamPrepAssessModeFromAddress)[1]') where ID = 1
Remember XML is always case-sensitive, eg:
use tempdb
go
drop table if exists organisations
go
create table organisations(id int, configuration xml)
insert into organisations(id, configuration) values (1,'<OrganisationMetaData><ExamPrepAssessModeFromAddress></ExamPrepAssessModeFromAddress></OrganisationMetaData>')
update organisations Set configuration.modify('insert <LegalEntityName>TEST</LegalEntityName> after (/OrganisationMetaData/ExamPrepAssessModeFromAddress)[1]') where ID = 1
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
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).
I have 2 databases in SQL Server 2005.
I want a functionality that i have same table structure in 2 database for example i have a same table named as testData in 2 database named as dbTest1 and dbTest2.
Now i want a single query through which i can add all the records from table testData of database dbTest2 into table testData of database dbTest1.
I tried to use following query
insert into dbTest1.testData values select * from dbTest2.testData
but this query is not running and giving error.
I also tried
insert into dbTest1.testData(col1,col2,col3) values select * from dbTest2.testData
but this also gives error that "Invalid object name dbTest2.testData"
Could any one help in this
Thanks
Replace dbTest2.testData with dbTest2..testData - you have to specify 3 things (or optionally leave the middle blank for dbo).
i.e.
insert into dbTest1..testData
select * from dbTest2..testData
If the table doesn't already exist in dbTest1, you can do this:
select *
into dbTest1..testData
from dbTest2..testData
You need to specify all column names in query.
insert into dbTest1.dbo.testData(col1,col2,col3) select * from dbTest2.dbo.testData
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...