To get data from table in script from sql server 2005 - sql

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!

Related

Drop tables using table names from a SELECT statement, in SQL (Impala)?

How do I drop a few tables (e.g. 1 - 3) using the output of a SELECT statement for the table names? This is probably standard SQL, but specifically I'm using Apache Impala SQL accessed via Apache Zeppelin.
So I have a table called tables_to_drop with a single column called "table_name". This will have one to a few entries in it, each with the name of another temporary table that was generated as the result of other processes. As part of my cleanup I need to drop these temporary tables whose names are listed in the "tables_to_drop" table.
Conceptually I was thinking of an SQL command like:
DROP TABLE (SELECT table_name FROM tables_to_drop);
or:
WITH subquery1 AS (SELECT table_name FROM tables_to_drop) DROP TABLE * FROM subquery1;
Neither of these work (syntax errors). Any ideas please?
even in standard sql this is not possible to do it the way you showed.
in standard sql usually you can use dynamic sql which impala doesn't support.
however you can write an impala script and run it in impala shell but it's going to be complicated for such task, I would prepare the drop statement using select and run it manually if this is one-time thing:
select concat('DROP TABLE IF EXISTS ',table_name) dropstatements
from tables_to_drop

How to create a table using the INSERT INTO clause using linked servers in SQL Server Management Studio

I have a server called GreatPlains and I would like to create a new table (not already defined) using the INSERT INTO clause onto my local server's reporting database. We have a linked server set up for the GreatPlains server and our main production server.
Simplified version of current query:
SELECT *
INTO [local].[Reporting].[dbo].[NewTable]
FROM [linked].[Main].[dbo].[Orders]
I'm also getting the error:
The object name 'local.Reporting.dbo.NewTable' contains more than the
maximum number of prefixes. The maximum is 2.
There are two mistakes in your query
1.INTO clause support maximum of 2 prefixes. You cannot include SERVER NAME
DATABASE_NAME.SCHEMA_NAME.TABLE_NAME
2.Unwanted INSERT ketword
So your query should be
SELECT *
INTO [Reporting].[dbo].[NewTable]
FROM [linked].[Main].[dbo].[Orders];
I think you have an extra insert:
SELECT *
INTO [local].[Reporting].[dbo].[NewTable]
FROM [linked].[Main].[dbo].[Orders];
If the table is already defined and has the same columns in the same order, then you can do:
INSERT INTO [local].[Reporting].[dbo].[NewTable]
SELECT *
FROM [linked].[Main].[dbo].[Orders];
If you are running the script in your local server and table already exists in the database,Use the below script.
USE [Reporting]
GO
INSERT INTO [dbo].[NewTable]
SELECT *
FROM [linked].[Main].[dbo].[Orders]
If you don't have the table in your database,use the below script.
USE [Reporting]
GO
SELECT *
INTO dbo.[NewTable]
FROM [linked].[Main].[dbo].[Orders]

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

Inserting the bulk data into different data tables

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

Create Table by Copying Structure of Existing Table

I am trying to create a new table by copying an existing table in SQL Server 2008 using Management Studio. The existing table contains no data. I am using the following code but am receiving an error for Incorrect Syntax near AS. I am not sure what is wrong here. I am a SQL newb and any help would be appreciated. Thanks.
CREATE TABLE Drop_Centers_Detail
AS (Select * From Centers_Detail)
like this, however this will not create indexes and constraints
select * into Drop_Centers_Detail
from Centers_Detail
where 1 = 0
In Sql Server Managment Studio, right-click your existing table and select Script Table as > Create to > New Query Editor Window. This will give you a better starting script that you can use as the base for your new schema.
1) I would suggest generating a create script from the table you want to copy, and then run that on your destination database.
2) Write a Insert statement in another SQL Query window to import that data
Insert Into Database1.Table1(Field1, Field2)
Select Field1, Field2 From Database2.Table