how to give database name in SQLAlchemy URI? - sql-server

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

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.

how to check the history of services names changed in oracle database server

I am using oracle 11g database. When I were connecting this DB i were using "test"
in service name. But someone has changed it. Can you please let me know how to find my latest service name of DB and also the history of person who has changed this service name?
this how to find your service name...
select sys_context('userenv','service_name') from dual;
I am not sure If you can know who change that .

Change password in ORACLE from SQL Server via linked server

I have an Oracle database connected to a SQL Server. The connection works correctly, but unfortunately, I don't know the password used for that connection. Now I need to change the password in Oracle.
Is it a good idea to run something like
SELECT * FROM OPENQUERY([oracle], '
ALTER USER OracleUser IDENTIFIED BY pswd;
SELECT 1 FROM DUAl')
Or maybe, is there another solution?
UPD
My query ends with error
The OLE DB provider "OraOLEDB.Oracle" for linked server "oracle" indicates that either the object has no columns or the current user does not have permissions on that object
Correct syntax for changing password in Oracle from SQL Server via linked server is
EXEC ('ALTER USER OracleUser IDENTIFIED BY new_password REPLACE old_password') AT [oracle]

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.

Use local database from master in MS SQLEXPRESS

I have created a local db using SQLEXPRESS through Visual Basic.
I intend to use LINQ to connect to the database from the application. Here is my statement to initially connect to the database:
Dim db As New DataContext("Data Source=localhost\SQLEXPRESS; Initial Catalog=master; Integrated Security=True;")
Ideally, my database would be entered for Initial Catalog, but that was giving me authentication errors for some reason. Now that this statement executes, my next step is to connect to my specific database. However, when I try to connect with a statement like this:
Dim TestCommand = db.ExecuteCommand("Use MyDB.mdf")
I get an error that the database does not exist.
When I query my database with the following commands:
SELECT name FROM master.sys.databases
The returned values are master, tempdb, model, msdb, and C:USERS\MY NAME\DOCUMENTS\MyDB.mdf
I have tried the above "TestCommand" writing out the directory for the database, but I get an error at "C:".
So, my db exists, but can someone explain to me the syntax I should use to "USE" my database?
You should not use the use command this way! You must connect to the application's database directly by setting it as Initial Catalog. If you're not authorized to do so, a use command won't let you either, by the way. So you have to fix the authorization for the database: create a login for your windows account in Sql Server Management Studio and grant it read/write access to the application's database.

Resources