Netezza ODBC connection - equivalent of "use database" - netezza

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.

Related

Oracle container database 12c, connecting using JDBC

I'm trying to learn how to use Oracle Container database, and just do basic JDBC connections. I installed a dockerised version of Oracle:
https://hub.docker.com/_/oracle-database-enterprise-edition
Which according to the data sheet comes set up with a CDB database called ORCLCDB and a PDB database called ORCLPDB1.
So I figured out I can connect to it like this:
jdbc:oracle:thin:#localhost:1555:ORCLCDB
with username sys, password Oradoc_db1, and setting the special internal_logon jdbc parameter equal to "sysdba" to avoid the error "local oracle CDB: ORA-28009: connection as SYS should be as SYSDBA or SYSOPER"
And I figured out I can switch to the PDB by entering this:
ALTER SESSION SET CONTAINER=ORCLPDB1
And I can then create a new user:
CREATE USER MYUSER IDENTIFIED BY MYPASSWORD1
But then I'm stuck. I think there should be some way to connect directly to the PDB using a JDBC connect string. Every time I google about this, it talks about tnsnames blah blah, but people who use JDBC connections, are typically using Tomcat on a server, or otherwise don't have the Oracle Client installed. They expect to be able to connect to Oracle just with the thin driver installed, nothing else.
I've tried the obvious using:
jdbc:oracle:thin:#localhost:1555:ORCLPDB1
with username myuser or sys, but I always get:
ORA-12505, TNS:listener does not currently know of SID given in connect descriptor
At this point I'm stuck.
You need to use a SERVICE_NAME in order to connect to an Oracle container database
Please alter your connect string like this:
jdbc:oracle:thin:#localhost:1555/ORCLPDB1
A SERVICE_NAME is denoted by a "/"
A SID (SystemIDentifier) is denoted by a ":" (not to be used)
Note! Default listener port is 1521, not sure why you specifically want a different port.
Best of luck!
Apparently the correct answer is this...
jdbc:oracle:thin:#localhost:1521/ORCLPDB1.localdomain
Then I can connect as SYS using the method above. If I want to connect as the created user, I also need...
grant create session to myuser;
and then, turn off the internal_logon jdbc parameter.

Restricting a user to only have access in a ssis package

We are running SQL Server 2016. For in-house political reasons, I can't restrict access the way I want to. Assume that I can't change the premise.
We have a user that is used in SSIS packages. Unfortunately, some devs are logging directly into the db with ssms using this user. I need to prevent this without changing the password or something. What I need is to be able to allow a user access to the database ONLY if it is running from an SSIS package and NOT if it is coming in any other way.
I am not looking for other suggestions of how to fix this issue. I understand most of them already, I am stuck because of management decisions that I cannot change.
Can anyone tell me how to restrict a user in such a way?
An approach is to use a LOGON trigger
A first blush approach might be to reject any process that look's like the SSMS application
CREATE OR ALTER TRIGGER logon_developer_check
ON ALL SERVER
FOR LOGON
AS
BEGIN
IF (ORIGINAL_LOGIN() = 'triskydeveloper'
and EXISTS
(
SELECT
*
FROM
sys.sysprocesses AS S
WHERE
S.spid = ##SPID
AND S.program_name LIKE 'Microsoft SQL Server Management%'
)
BEGIN
ROLLBACK
END
END
But developers, being devious little buggers, will then write their own .NET application or use SQLCMD so you'd fall into a rat race trying to identify all the program_names that might show up.
I would instead look at the hostname column on sys.sysprocesses - if the connection isn't coming from the server itself, just reject it. Unless you have to deal with developers able to RDP onto the server.
Oh and if you mangle the logon trigger and it's rejecting everything, use SQLCMD and the dedicated admin console, DAC, and
sqlcmd.exe -S localhost -d master -Q "DISABLE TRIGGER ALL ON SERVER" -A

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 to give database name in SQLAlchemy URI?

I am using Apache Superset and to connect to the SQLServer, I am using the below url which works fine but connects to master DB on MSSQL. I wanted to connect to another DB on MSSQL but do not know how to do that
mssql+pymssql://<username>:<password>#<freetds_name>/?charset=utf8
Is there a way I can explicitly mention the DB name in the url ?
Another issue I have is my db name has space in it, it is "Data Analytics"
Try
mssql+pymssql://user:pass#host/db
Reference: http://docs.sqlalchemy.org/en/latest/dialects/mssql.html
I was not able to find the required parameter where I can just mention the name of the DB in url. Although there are ways of doing it but if you are using freetds_name, I have not see any option of setting DB name.
I tried setting default Database name in odbc.ini but for some reason it did not work.
The easiest way is to execute any of the below SQL
EXEC '<Login_name in Quotes>', '<DB name in Quotes>'
OR
ALTER LOGIN <Login_Name_noQuotes>
with DEFAULT_DATABASE = <DB_name_no_quotes
Since my DB name had space in it, I implemented the first statement and it worked.
You can check the Default DB by executing below SQL
select name,
loginname,
dbname as DefaultDB
from syslogins

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