USE <database name> is failing

Highlighted
Occasional Visitor

Hi,

I ve created a user named my_sys1, a db named my_sys1.

Then I ve created another user named my_sub_sys1 and a db named my_sub_sys1.

 

I created a connection using my_sys1@my_sys1 (in a java app) and I am trying to execute "use my_sub_sys1". At that time I am getting below exception:

com.microsoft.sqlserver.jdbc.SQLServerException: The prepared statement handle 1 is not valid in this context. Please verify that current database, user default schema, and ANSI_NULLS and QUOTED_IDENTIFIER set options are not changed since the handle is prepared.

 

Am I missing something? 

 

select IS_QUOTED_IDENTIFIER_ON, IS_ANSI_NULLS_ON from sys.databases where name in ('my_sys1','my_sub_sys1');
Output : Bother are ON for both databases;

 

Thank you.

 

1 Reply
Highlighted

@Sanat1984Where is your SQL Server that you are connecting to?  If this is an Azure SQL Server then you can not use the "use" statement because Azure SQL databases are a one database to one connection.  Thus your connection must specify a database if using Azure SQL Database.  But in general, you should limit a connection to a single database and the connection string should specify your database; "use" statements should be used only when in interactive mode or during database maintenance activities.