Inserting the bulk data into different data tables - sql

In SQL Server 2008 i am having two different databases in both the i am having a same table here i want to copy data from one database to another database
how can we this can be implemented in sql server 2008?

if the two database is database1 and database2,the table name is Tablename,you can use:
Insert into dbo.database1.TableName select * from dbo.database2.Tablename

You can use different methods for this.
Use DTS tool of SQL Server 2008.
INSERT INTO DBName.Schema.TableName
SELECT F1,F2 FROM TableName

Related

SQL Server - define target database name dynamically for MERGE statement

I have 2 tables on 2 different databases on the same server with SQL Server 2012.
I'm synchronizing those 2 tables using the MERGE statement but for the target table need to specify the database name dynamically in a query i.e [targetdb].[dbo].[tablename]
I have prepared a query and use EXEC to execute the dynamic query, but Is it possible to use variable value for [targetdb] database

How can I use a SQL Server table in an openquery to an Oracle database?

I have a database on SQL Server and would like to use a column in one of my tables in a linked server openquery I'm running to an Oracle database in order to match values between the two and insert the result into columns in my table in SQL Server .
Essentially I want it to be like this:
SELECT col1, col2, col3, col4
FROM OPENQUERY(link, 'SELECT * FROM Oracle_Table
WHERE ID = MSSQL.dbo.table.ID`)
So I'd like to be able to use my internal table column values to query an external database. They are related tables but different systems.
Would it be possible to get a big list of the values in the SQL Server table column and use it as a variable in the Oracle query? I've searched extensively online but haven't been able to find this one.
You can't pass parameters like I wanted to, but I ended up creating a bunch of queries in Powershell using a for loop and variables within the string to create my large query, then put a UNION ALL after each SELECT FROM OPENQUERY()

how to update table from linked server in sql server?

I have few tables I need to load from linked firebird server into SQL Server from time to time. I use statement like this:
SELECT * into dekr FROM OPENQUERY ( [PLINK] ,'select * from dekr' )
It takes a while since it's going over network etc, is there a way to update once created
table dekr only with changes since last time?
thanks

Oracle and Sybase compatibility for create table new_table as

I am trying to write an SQL query which needs to be compatible on both a Sybase and Oracle database. The query looks like the following :
SELECT *
INTO new_table
FROM other_table
This query is working great on a Sybase database but not on an Oracle one. I found the equivalent for Oracle :
CREATE table new_table AS
SELECT *
FROM other_table
Is there a way to write a third query that would do the same and that can be executed on a Sybase and on an Oracle database?
As you found, Oracle supports INTO but doesn't use it like Sybase/SQL Server do. Likewise, Sybase doesn't support Oracle's extension of the CREATE TABLE syntax.
The most reliable means of creating a table & importing data between the systems is to use two statements:
CREATE TABLE new_table (
...columns...
)
INSERT INTO new_table
SELECT *
FROM OLD_TABLE
Even then, syntax is different because Oracle requires each statement to be delimited by a semi-colon when TSQL doesn't.
Creating a table & importing all the data from another table is a red flag to me - This is not something you'd do in a stored procedure for a production system. TSQL and PLSQL are very different, so I'd expect separate scripts for DML changes.
There is no query that would do what you want. These environments are very different.
It is possible.
SELECT * INTO new_table FROM existing_table;
Thanks

To get data from table in script from sql server 2005

I am using sql server 2005
I have a table [say tblHistory] and this table contains 100 rows.
I have created the same table at the server, but the table doesn't have the data, I want data from tblHistory to convert into
INSERT INTO tblHistory ------
so that I could run the script on the server to fill the database.
To generate all the INSERT INTO statements you need based on table data, take a look at this project: http://www.codeproject.com/KB/database/sqlinsertupdategenerator.aspx
you need to create a linked server between the two servers and then you do something like this
INSERT INTO tblHistory
select * from LinkedServerNAme.DatabaseName.SchemaName.tblHistory
To add a linked server read this http://msdn.microsoft.com/en-us/library/ms190479.aspx
BTW you can also use OPENROWSET, OPENDATASOURCE, SSIS or bcp out and bcp in
Not sure I understand the question... you just want to copy the contents of one table into another?
INSERT INTO newTable SELECT * FROM tblHistory
If the new table doesn't already exist, you can use SELECT INTO:
SELECT *
INTO new_table
FROM tblHistory
But that's the caveat - it has to be a new table, no data already in there.
Otherwise, use:
INSERT INTO new_table
SELECT x.* --preferrable to actually define the column list than use *
FROM OLD_TABLE x
You should check out the SSMS Tools Pack - one of its feature is the ability to generate those INSERT scripts you're looking for!
Get the SSMS Tool Pack from this site - it's an add-in for SQL Server Management Studio - highly recommended!