I have a simple SQL that on a production database finds about 3000 rows:
SELECT *
FROM [CONTOSO].[BATID] (NOLOCK)
WHERE COMPANYNO = 1
AND MEASURINGPOINT = '592-98901_NPT'
AND TIMESERIESNO = 1
AND DATE_TIME >= '2013-01-31 23:00:00'
AND DATE_TIME <= '2013-02-28 22:59:00'
ORDER BY DATE_TIME
Is there some way that I can convert the output into some kind of big 'INSERT INTO....'-sql that would insert this data into the same table. Then I would easily be able to copy the sql into the other SQL Server Management Studio-window and execute it.
If your servers are not connected, you can use the SSMS import/ export wizard . This export creates a data file that you can then import at the remote server.
use INSERT INTO..SELECT
INSERT INTO TableName (collName1, colName2,...)
SELECT * -- this should match to the names of column define in the INSERT clause
FROM [CONTOSO].[BATID] (NOLOCK)
WHERE COMPANYNO = 1
AND MEASURINGPOINT = '592-98901_NPT'
AND TIMESERIESNO = 1
AND DATE_TIME >= '2013-01-31 23:00:00'
AND DATE_TIME <= '2013-02-28 22:59:00'
If create table as select is available in your version of SQL then you can use this:
Create table tab_name AS
SELECT... your query...
SSMS Tools Pack has a great Generate Insert Statements function: http://www.ssmstoolspack.com/ It allows you to easily enter the where clause to get just the data you want to export. This used to be totally free but now requires a small fee if using with SSMS 2012+
If you have more money and need to do this kind of thing a lot, then Red Gate's SQL Data Compare is a great tool: https://www.red-gate.com/products/sql-development/sql-data-compare/
You can write code that creates code.
I 've done it for the "MembershipProvider" database here: http://granadacoder.wordpress.com/2007/11/29/membershipprovider-helper-to-transfer-data/
You can modify to fit your needs.
Watch the double and single quotes, they'll throw a monkey wrench in the plans.
Here is a preview of the bigger code example:
Note, it creates code that can be run on the second sql server.
select
'INSERT INTO dbo.aspnet_Applications ( ApplicationName,LoweredApplicationName,ApplicationId,[Description] ) values (' as [--Comment],
char(39) + t1.ApplicationName + char(39) , ',' ,
char(39) + t1.LoweredApplicationName + char(39) , ',' ,
char(39) + convert(varchar(38) , t1.ApplicationId ) + char(39) , ',' ,
char(39) + t1.Description + char(39)
, ')'
FROM
dbo.aspnet_Applications t1
Here is a Northwind example that handles single quotes in the CompanyName and Address columns, and has a "where not exists" check on the PrimaryKey. Note, this table did not have any ints/numbers, but simply removing the single quotes will work. If you want to check for null values, that's a simple case statement as seen in the Fax column.
Select 'INSERT INTO dbo.Customers ( CustomerID,CompanyName,ContactName,ContactTitle,[Address],City,Region,PostalCode,Country,Phone,Fax ) ' as [--Comment],
'SELECT ' ,
char(39) + t1.CustomerID + char(39) , ',' ,
char(39) + REPLACE(t1.CompanyName , char(39) , char(39) + '+' + 'char(39)' + '+' + char(39) ) + char(39) , ',' ,
char(39) + t1.ContactName + char(39) , ',' ,
char(39) + t1.ContactTitle + char(39) , ',' ,
char(39) + REPLACE(t1.[Address] , char(39) , char(39) + '+' + 'char(39)' + '+' + char(39) ) + char(39) , ',' ,
char(39) + t1.City + char(39) , ',' ,
char(39) + t1.Region + char(39) , ',' ,
char(39) + t1.PostalCode + char(39) , ',' ,
char(39) + t1.Country + char(39) , ',' ,
char(39) + t1.Phone + char(39) , ',' ,
Fax = case
when t1.Fax is null then 'null'
else char(39) + t1.[Fax] + char(39)
end
, ' Where not exists (select null from dbo.Customers innerC where innerC.CustomerID = ' + char(39) + t1.CustomerID + char(39) + ')'
FROM
dbo.Customers t1
Also you can create Linked Server and then copy data from one table to another table on Linked Server.
sp_addlinkedserver
@server= N'NameofLinkedServer',
@srvproduct= N'',
@provider= N'SQLNCLI',
@datasrc= N'YourRemoteServer';
sp_addlinkedsrvlogin
@rmtsrvname = 'NameofLinkedServer' ,
@useself = 'FALSE' ,
@locallogin = 'local_login' ,
@rmtuser = 'remote_login' ,
@rmtpassword = 'remote_password'
After creating Linked Server execute this INSERT statement
INSERT [NameofLinkedServer].[your_databaseName].[CONTOSO].[BATID](Column1, Column2, ...)
SELECT (Column1, Column2, ...)
FROM [CONTOSO].[BATID] (NOLOCK)
WHERE COMPANYNO = 1
AND MEASURINGPOINT = '592-98901_NPT'
AND TIMESERIESNO = 1
AND DATE_TIME >= '2013-01-31 23:00:00'
AND DATE_TIME <= '2013-02-28 22:59:00'
ORDER BY DATE_TIME
For details, see the MSDN docs
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.