I am trying to query the following in sql server management studio:
select colname from openquery (linked_server, 'exec XX.YY.PrcName #Parameter')
When i try to give XX as linked server name i am getting an error.
Is that right or how do i find the linked_server name ?
To check linked server name :
select * from sys.sysservers
you can easily run any procedure in another server through linked server as:
exec <procedure name><parameneters> at <linked server name>
To get a list of linked servers use:
EXEC SP_LINKEDSERVERS
If you want to SELECT something from Linked server:
SELECT *
FROM [SOMESERVER\SOMEINSTANCE].somedatabase.dbo.sometable;
To EXECUTE something on linked server:
EXEC [SOMESERVER\SOMEINSTANCE].somedatabase.dbo.somestoredprocedure
SELECT *
FROM OPENQUERY([SOMESERVER\SOMEINSTANCE].somedatabase.dbo.somestoredprocedure)
More info:
sqlauthority
MSDN
You also can use sys.servers
Select * From SYS.SERVERS
When server_id = 0, this is the server name.
When server_id >0 , this is the local name of linked server.
Related
I want to select data from SQL Server to Oracle (Toad Apps).
If from Oracle to SQL Server its done like this
Insert Into M_CLASSIFICATION_ORACLE
SELECT * From OPENQUERY ([B1APPS], 'select * from V_Classification_Asset' ) AS derivedtbl_1
How about in Oracle (Toad apps)?
You can able to do it by using CREATE DATABASE LINK:
Create a Database Link to connect to SQL Server:
CREATE DATABASE LINK link-name ...
Query SQL Server using the Database Link:
SELECT * FROM sqlserver-table-name#link-name;
Documentation:
https://www.oracletutorial.com/oracle-administration/oracle-create-database-link/
Calling Stored Procedure using Database Link:
https://dba.stackexchange.com/questions/1856/running-a-stored-procedure-across-db-link
I want to SELECT a table in a SQL Server database and then INSERT it into a local SQLite database.
IS it possible to do this entirely with a query in the vein of:
INSERT INTO table1 ( column1 )
SELECT col1
FROM table2
but passing connection information?
Setup a local odbc data source, DSN, for SQLite file. Call it SQLite_DataSource
Then setup a linked server in SSMS:
USE [master]
GO
EXEC sp_addlinkedserver
#server = 'SQLite', -- the name you give the server in SSMS
#srvproduct = '', -- Can be blank but not NULL
#provider = 'MSDASQL',
#datasrc = 'SQLite_DataSource'
GO
Then you can use it like any other db:
INSERT into SQLite_DataSource (column1)
SELECT col1
FROM table2
Lookup SQL Server's documentation for sp_addlinkedserver for more details.
You need to create a SQL Server Linked Server to SQLite, take a look at this post for more information .
How can I reference a second server in SQL.
SELECT A.datasetid,
A.dsdate,
B.datasetid AS Expr1,
B.dsdate AS Expr2
FROM we_ci_db.tblopportunitydatasets AS A
INNER JOIN we_ci_db.tblopportunitydatasets AS B
ON A.datasetid = B.datasetid
Assume that table 'B' is on a different server, what would the syntax be. I've tried putting the server name before the schema, but it doesn't recognise it
You must configure a linked server. Once that is configured, the linked server may be referenced as
server.database.schema.object
You'll first need to create a Linked Server by running the addlinkedserver stored procedure:
USE [master]
GO
EXEC master.dbo.sp_addlinkedserver
#server = N'SRVR002\ACCTG',
#srvproduct=N'SQL Server' ;
GO
After that, you can refer to the linked server with the syntax:
select *
from [SRVR002\ACCTG].[database name].[owner name].[table name]
More Info
I have a linked Firebird database on my SQL Server 2008 via ODBC.
I can execute a query like this and I get the wanted results:
SELECT * FROM OPENQUERY(LINKED_SERVER_NAME, 'SELECT * FROM TABLE_NAME')
Now i wonder how can I execute stored procedure with one parameter input.
I have tried:
SELECT * FROM OPENQUERY(LINKED_SERVER_NAME, 'STORED_PROCEDURE_NAME 00001')
and
EXEC LINKED_SERVER_NAME.STORED_PROCEDURE_NAME '00001'
with no success...
Any tip would be appreciated !
I dont know in MSSQL but you can try
SELECT * FROM OPENQUERY(LINKED_SERVER_NAME, 'SELECT * FROM STORED_PROCEDURE_NAME(00001)')
How can I get all the database names in a sql server instance using tsql?
SELECT * FROM sys.databases
----SQL SERVER 2005 System Procedures
EXEC sp_databases
EXEC sp_helpdb
----SQL 2000 Method still works in SQL Server 2005
SELECT name
FROM sys.databases
SELECT name
FROM sys.sysdatabases
----SQL SERVER Un-Documented Procedure
EXEC sp_msForEachDB 'PRINT ''?'''
to know more about database : http://blog.sqlauthority.com/2007/05/12/sql-server-2005-list-all-the-database/
this should work on pretty much any version of sql server
USE master;
SELECT NAME FROM sysdatabases;
[edit : it could be SELECT NAME FROM sys.databases too, microsoft's website says both and i'm not on my windows box to test, sorry!]
you could also use (sql 2005 only)
USE master;
EXEC sp_databases;
And for practical use added couple of common filters:
select database_id, [name] database_name
from master.sys.databases
WHERE state <> 6 -- skip offline
AND database_id > 4 -- skip system dbs
AND HAS_DBACCESS([name]) = 1 -- with User Access
SQL 2000
use master
select name from sysdatabases
or
(no need for use master, includes database_size)
exec sp_databases