ODBC string connect to specific table - sql-server

I'm connecting to a SQL Server spatial database by using ogr2ogr with the MSSQL driver.
C:\>ogr2ogr -f "GeoJSON" C:\Users\usr_m\Documents\shape_odbc.geojson "MSSQL:Driver=SQL Server;server=hksql;database=ql51pd;trusted_Connection=yes;"
as specified here and here
But I'd like to connect to download a certain table, any suggestions how I do that?
Maybe I need to fetch it in python or make another workaround.

As suggested on a side-note here. The string has to look like this:
C:\>ogr2ogr -f "GeoJSON" C:\Users\usr_m\Documents\shape_odbc.geojson "MSSQL:Driver=SQL Server;server=hksql;database=ql51pd;trusted_Connection=yes;" **<table>**

Related

SAS SQL Server connectivity without using ODBC

Is it possible to connect to a SQL Server via SAS without using ODBC?
If the answer is yes can you give a code example of how to do it?
OleDb. Those are your two choices. You could also use a web service to front the SQL Server db and use PROC HTTP or PROC SOAP (Preference would be REST/JSON). For direct access you need the SAS Access engine for SQL Server, ODBC, or OleDB.
Yes it is possible. You can do it either as libname or in proc sql. i suggest using insertbuff-option when uploading. Just make sure your username, passwords and server are not quoted, like in oleDb connections.
libname My_libname odbc noprompt =
"DRIVER=SQL Server; server=&server.; Uid=&userName.;Pwd=&password.; DATABASE=&Database.;"
INSERTBUFF=32767;
The second way is the
proc sql;
connect to odbc as my_conn
(noprompt = "DRIVER=SQL Server; server=&server; Uid=&user.; Pwd=&password.; DATABASE=&database.;");
create table Query_result as
select * from connection to my_conn (
select * from table_in_server_database
);
quit;
For more you can read in SAS support page
Whilst this technically uses ODBC/OLEDB, you don't need SAS licenced for it... write a VBScript (potentially receiving arguments passed from SAS) which connects to your data source using the system ODBC/OLEDB drivers, executes the query, and returns delimited records.
Then execute said VBScript via a pipe infile, read the records into SAS.
I do this regularly against multiple data sources (Informix, SQL Server, DB2, MSAccess/Excel).
I cannot comment on Chris J's answer but it is a cool idea. I would make some modifications (suggestions) to it. Skip VBScript since it is out of date and is not allowed in a lot of places. Use .NET Core/C# and Entity Framework. EF is a full ORM, easy to use, and will auto-magically discover the entire DB structure on almost every db out there. Stream the data back using a memory buffer.
Like Chris' answer but just updating it to more modern mechanisms. Will have to give it a try since it is a clever way around the issue.

How do you create an ITRS/Geneos alert for MSSQL DB

I'm new to ITRS Monitoring, and I am wondering if I could create a Geneos alert that can connect to a MSSQL DB and run a simple select to make sure connectivity is good. Is this possible with Geneos? Are there any plugins needed?
Yes - you can use sql toolkit plug-in. It supports Db2, Oracle, SQL Server, MySql and Sybase. It will look something like this:

Netezza ODBC connection - equivalent of "use database"

After connecting to a Netezza system is there any way to switch the database? For instance, in MSSQL one could send the command use database_name in order to switch to the database_name.
Is there anything in Netezza like "use" on mssql?
http://technet.microsoft.com/en-us/library/ms188366.aspx
My reason for asking is in regards to metadata queries; I can only access _v_table of the "currently connected database".
Prior to Version 7.0 there wasn't an equivalent to USE. You had to log in to specific databases on the server however you can still access any object using. database.schema.objectname
Post Version 7.0 the equivalent is Set Catalog
SET CATALOG <database_name>
Regarding your specific inquiry. Please consider using _V_TABLE_XDB instead of _V_TABLE. This system table should provide you with a list of all tables, not just those in the database you are connected to.
If you're using NPS v7.0+, then the SET CATALOG command will connect you to a different database dynamically (without having to disconnect and reconnect). The beauty of this command is that it can be submitted from an ODBC/JDBC client as well as in an nzsql script.
In Netezza you can to other database with following command -
nzsql -u <username> -pw <password> -db <databasename> -host <netezza server IP>
hope this will help you.
As we know while executing any query in netezza we need to specify database name in connection url , which we should to make dynamic according to required database on which we have to perform operation, so in case of simple java code we can use the below Case 1) and in case of spring boot we can use below Case 2).
Case 1)
before executing any query in netezza we can execute statement as
SET CATALOG #database name#;
so it will automatically switch to database provided in set command.
Case 2)
We can switch the database in netezza with runtime switching of database
Use Apache basic data source (BasicDataSource) instead of DriverManagerDataSource while creating bean for datasource
Execute - jdbcTemplate.execute("SET CATALOG #database name#")
Before executing any query.

Form info to database on another server

I have two websites; website1.com and website2.com. On website1 I have database with customer information and a neat CMS. Now I would like to make a registration form on website2 and insert that information in the database on website1.
Is this possible? Can I simply use mysql INSERT INTO~? Or do I need to use something else?
You can connect remotely to another server:
mysql -h host_name -D db_name -ppassword -c "insert into ..."
You need to allow MySQL remote connections if your websites are on different machines. Securing Remote MySQL
In both cases (remote or local), the method is the same: open a connection to website1's database and use INSERT INTO.

Cron Job with R and SQL Server

This is probably going to be an underspecified question, as I'm not looking for a specific fix:
I want to run a machine learning algorithm on some data in a SQL Server database. I'd like to use R to do the calculations -- which would involve using R to connect to the database, process the data, and write a table of results back to the database.
Is this possible? My guess is yes. Shouldn't be a problem using a client...
however, would it be possible to set this up on a linux box as a cron job?
Yes to all!
Your choices for scripting are either Rscript or littler as discussed in this previous post.
Having struggled with connecting to MSSQL databases from Linux, my recommendation is to use RJDBC for database connections to MSSQL. I used RODBC to connect from Windows but I was never able to get it working properly in Linux. To get RJDBC working you will need to have Java installed properly on your Linux box and may need to change some environment variables (seems I always have SOMETHING mis-configured with rJava). You will also need to download and install the JDBC drivers for Linux which you can get directly from Microsoft.
Once you get RJDBC installed and the drivers installed, the code for pulling data from the database will look something like the following template:
require(RJDBC)
drv <- JDBC("com.microsoft.sqlserver.jdbc.SQLServerDriver",
"/etc/sqljdbc_2.0/sqljdbc4.jar")
conn <- dbConnect(drv, "jdbc:sqlserver://mySqlServer", "userId", "Password")
sqlText <- paste("
SELECT *
FROM SomeTable
;")
myData <- dbGetQuery(conn, sqlText)
You can write a table with something like
dbWriteTable(conn, "myData", SomeTable, overwrite=TRUE)
When I do updates to my DB I generally use dbWriteTable() to create a temporary table on my database server then I issue a dbSendUpdate() that appends the temp table to my main table then a second dbSendUpdate() that drops the temporary table. You might find that pattern useful.
The only "gotcha" I ran into was that I could never get a Windows domain/username to work in the connection sequence. I had to set up an individual SQL Server account (like sa).
You may just write a script containing R code and put this in the first line:
#!/usr/bin/env Rscript
change the file permissions to allow execution and put in crontab as it would be a bash script.

Resources