How to INSERT INTO SQLite with SELECT FROM SQL Server - sql

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 .

Related

How to Select Table SQL To Oracle

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

How to find linked_server name in sql server management studio?

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.

Query to copy rows from sql server to another sql server

I have two sql servers, SE1 and SE2, they are basically having the same schema, but each one has its own usability.
I need to find a query to copy some rows from SE1.Table1 Where Destination Like '%HR%' to SE2.Table2.
Is there such query ??
Thx in advance.
I find the easiest way is to add a remote connection from one to the other. So, go to the second server and do:
sp_addlinkedserver SE1
Then you can go to the database you want to use and do something like:
insert into database.dbo.Table1(<column list>)
select <column list>
from SE1.database.dbo.Table1
where col like '%HR%';
This uses the four-part naming convention to access the remove table.
SELECT * into tr FROM OPENROWSET( 'SQLOLEDB',
'Server=10.10.1.89\SQLEXPRESSR2;UID=sa;PWD=password',
'SET FMTONLY OFF;SET NOCOUNT ON; exec DBASE89.dbo.Getdata #UID=''21'''
)
select * from tr
consider 10.10.1.89\SQLEXPRESSR2 as remote server, we need to create stored procedure with select command for required data.
Here tr is Temp table, when execute above query the result will stored in tr table. From this tr table we can copy to required table in local server.
Note:
in 10.10.1.89\SQLEXPRESSR2 server, need to enable the OleAutomationEnabled set as True in SurfaceAreaConfiguration.

How do you reference a second SQL Server

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

How can I get all the database names in a sql server instance using tsql?

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