I have three SQL Servers A, B and C. I am trying to run a same query like select ##servername from A server and run the same query in B and C from Server A. I am loading this results in a server A table. Please let me know how to accomplish this one.
You could run the inserts from the two remote servers using OPENQUERY which essentially runs the select locally on the remote server to be able to use the proper local indexes etc.
insert into localTableOnA (col1,col2,...)
select col1,col2,... from [dbname].dbo.[tablename] --on ServerA
insert into localTableOnA (col1,col2,...)
select col1,col2,... from openquery([ServerB],'select col1,col2,... from [dbname].dbo.[tablename]')
insert into localTableOnA (col1,col2,...)
select col1,col2,... from openquery([ServerC],'select col1,col2,... from [dbname].dbo.[tablename]')
You can make using linked servers.
1- WITHIN A instance
INSERT INTO [DatabaseName].[SchemaName].[TableName] (...) SELECT ... FROM [B].[DatabaseName].[SchemaName].[TableName];
INSERT INTO [DatabaseName].[SchemaName].[TableName] (...) SELECT ... FROM [C].[DatabaseName].[SchemaName].[TableName];
2- WITHIN A instance (Dynamic SQL)
EXEC ('INSERT INTO [A].[DatabaseName].[SchemaName].[TableName] (...) SELECT ... FROM [DatabaseName].[SchemaName].[TableName]') AT [B];
EXEC ('INSERT INTO [A].[DatabaseName].[SchemaName].[TableName] (...) SELECT ... FROM [DatabaseName].[SchemaName].[TableName]') AT [C];
3- WITHIN Other instance
INSERT INTO [A].[DatabaseName].[SchemaName].[TableName] (...) SELECT ... FROM [DatabaseName].[SchemaName].[TableName];
Related
how can i insert from SQL Server to a SQLBase Database using linked Server?
Here are various examples of the correct syntax for SQLServer to SQLBase assuming your LinkedServer is called 'ISLANDLINK' . Note the two dots.
or Go here for the full script and explanation : SQLServer to SQLBase via LinkedServer
or Go here for another example: SQLServer to SQLBase via LinkedServer (more)
--Select:
SELECT * FROM OPENQUERY( ISLANDLINK, 'Select * from SYSADM.BUDGET where DEPT_ID = ''MIS'' ')
--Update:
UPDATE [ISLANDLINK]..[SYSADM].[BUDGET]
SET [BGT_YEAR] = 2016
WHERE DEPT_ID = 'MIS'
GO
--Insert:
INSERT INTO [ISLANDLINK]..[SYSADM].[EMPLOYEE]
([EMPLOYEE_ID]
,[LAST_NAME]) VALUES (99 ,'PLUMAS' )
GO
--Delete:
DELETE FROM [ISLANDLINK]..[SYSADM].[EMPLOYEE]
WHERE [LAST_NAME] = 'PLUMAS'
In SQL Server, I have a server A and I linked a server B to A, so from server A I can execute query like :
exec B.DataBase1.dbo.simplePs.
My question is how can I get the server who is executing the PS? I tried ##ServerName like this :
Create procedure simplePs
as
insert into TRACE(EventInfo) values (##SERVERNAME)
but I get server B not server A.
Please help.
You could try querying the sys.sysprocesses table - there's lots of useful connection information there e.g.
insert into TRACE(EventInfo)
values (select distinct hostname from sys.sysprocesses where spid = ##SPID)
will insert the source hostname of the connection.
You don't have such context information, for the server B,
the query is executed on its own instance/database.
The best you could do, is to pass the source server as a parameter in the procedure with ##SERVERNAME as default value.
Create procedure simplePs
#sourceServer as nvarchar(255) = ##SERVERNAME
as
insert into TRACE(EventInfo) values (#sourceServer)
I am trying to insert some data from my local table into linkedserver via sql server. this is what i am doing but it keeps on throwing syntax error.
TRY 1
EXEC(
'INSERT into test.testschema.testoperation
(viasatsubscriptionID, subscriptionrowdate, phonenumberday, viasatcustomerid)
SELECT * FROM rdata.dbo.testoperation'
)AT REDSHIFT64
TRY 2
EXEC(
'INSERT into test.testschema.testoperation
(viasatsubscriptionID, subscriptionrowdate, phonenumberday, viasatcustomerid)'
)AT REDSHIFT64
SELECT * FROM rdata.dbo.testoperation
Both fails.
Any thoughts where i am going wrong?
testoperation is your local table, and since your query runs on the remote server, the table does not exist.
Why don't you try inserting directly to the remote table:
INSERT into REDSHIFT64.test.testschema.testoperation
(viasatsubscriptionID, subscriptionrowdate, phonenumberday, viasatcustomerid)
SELECT * FROM rdata.dbo.testoperation
I have a linked server Remoteserver containing a table that holds file and folder names from a dir
When I am on the remote server I can run a built in procedure (xp_dirtree) and populate the 'files' table but what i need to do is to run a query from the local SQL server that does this:
Delete all records from the [Files] table on Remoteserver
Insert data that comes from the stored procedure:
INSERT [Remoteserver].[dbo].[files] (subdirectory,depth,isfile)
EXEC master.sys.xp_dirtree '\\Fileserver\DBBackup',1,1;
Select the 'subdirectory' column
I tried some things using openquery and i am able to select existing records but unable to do the insert.
Any help is appreciated.
Try this
INSERT INTO OPENQUERY([Remoteserver]
,'SELECT subdirectory, depth, [file] FROM [RemoteDB].[dbo].[files]')
EXEC master.sys.xp_dirtree '\\fileserver\DBBackup', 1, 1;
OR
INSERT INTO OPENQUERY([Remoteserver]
,'SELECT subdirectory,depth, [file] FROM [RemoteDB].[dbo].[files]')
select * from OPENQUERY([another_server_name], 'master.sys.xp_dirtree ''\\fileserver\DBBackup\temp'', 1, 1');
But in general you do not need to use OPENQUERY at all if Fileserver and Remoteserver are accessible from the local machine.
INSERT INTO [Remoteserver].[RemoteDB].[dbo].[files] (subdirectory, depth, isfile)
EXEC master.sys.xp_dirtree '\\Fileserver\DBBackup',1,1;
I've created an ADO.NET connection manager, and a DataReader source with the following SQL Command:
select
'test' as testcol
INTO
#tmp
select * from #tmp
If I click the refresh button in the DataReader component, I get SqlException "Invalid object name #tmp". The SQL statment itself is clearly valid and executes properly in sql server management studio. I've also tried setting DelayValidation on the connection manager, to no avail.
is the error on the INSERT or the SELECT?
if you are issuing only one command that contains both the INSERT and SELECT, try putting a semicolon before the SELECT.
EDIT after OP comment
encapsulate all the logic within a stored procedure:
CREATE PROCEDURE YourProcedureName
AS
select
'test' as testcol
INTO
#tmp
select * from #tmp
GO
the have your application run this single SQL command:
exec YourProcedureName
EDIT after next OP comment
OP doesn't say which SQL Server version they are using, if 2005 or up, try a CTE:
;with CTEtemp as
(
select
'test' as testcol
)
select * from CTEtemp
Why couldn't this be replaced with a "SELECT 'test' as testcol"? The SSIS query parser may be having trouble with it because there's a temp table involved and it expects a single statement, not an actual SQL script. Or, if what you're sharing above is only an example for illustration, maybe something like this:
SELECT *
FROM (SELECT 'test' AS testcol)
Can you elaborate on what you're trying to accomplish here and, if it is, why the temp table is required?
Use sp_executesql
Your command would become
exec sp_executesql #statement=N'
select
''test'' as testcol
INTO
#tmp
select * from #tmp'
You must use nvarchar string (hence the N), and escape single quotes by doubling them.
I had the same problem as you and this is how I just fixed it.