Change the default database in SQL Server Management Studio - sql-server

When I open SQL Server Management Studio, I have many databases listed there, If I open a database and open the list of its tables and right click on a table and say Select Top 1000 rows it correctly queries that table of that database BUT it also automatically picks TempDb as the database in the available database combobox in the toolbar like the picture below.
So now if I want to write a query by clicking on "New Query" it will look at "tempDB", which is annoying. Is there a way to change this behavior ?

You can select a default database per connection in SQL Server, which might help. Once you've set it, each time you connect to that server, the default database will be selected.

It seems, that DavidG's comment is the best answer.
"Your user account determines the default database."
You can choose default database for your user account in the user properties.
Security - Logins - right mouse click - Properties - and select the default DataBase.
And so, every time when you open new or existed query window the current DataBase would be selected from the default dataBase setting in user porperties.

Related

Quick way to add user access to all databases

I have a database Server with 10 databases. One of the users is with only access to 1 database.
I would like to add this user with all the permissions that they already have to all other databases. Is there anyway where I can do it?
Or do I need to create the same user for each database?
I am using SQL Server Management Studio 2018.
Thanks.
In SSMS go to securities (under database) -> logins
Then select your user and with mouse right click select properties. Then in new window select user mapping tab. Here you select all the databases you want them to give access to. (Here you can also set roles for them). Click OK and you are done. Now they have access to all the databases.

Who created a database in my SQL Server instance?

I'm trying to determine who created a database in my SQL Server instance. The .trc logs seem to have been purged and I can't locate a backup of them. I know when the database was created and have found the .bak file that was used to create the database, but I can't determine WHO created it.
Any other ideas how I can figure this out? (SSMS schema history report also doesn't go back far enough)
Based on the following article:
There is no dbo concept for server scope securables. They are always owned by the login that created them, no matter of any server roles that the login might be a member of.
So by default, the database owner is the one who created the database, but you have to make sure that no one changed this property:
To check the database owner, in SQL Server management studio, Right click on the database and in the Properties window >> General Tab >> check the owner property:

deployment of adventureworks cube fails: 'user does not have permission to create a new object in 'GARY-PC' or the object does not exist

when I deploy adventure works cube it fails, and i get: user does not have permission to create a new object in 'GARY-PC', or the object does not exist.
trying to process cube from adventureworks DW and having what seem like permissions issues (?).
took following steps (i am using sql server 2008 R2 developer edition and VS 2008):
1 downloaded and successfully created adventureworksDW (2008R2) database from
2 successfully created Datasource and DSV for a cube with 2 fact tables and several dimensions.
3 Click deploy
I see the following 2 prompts
Login: greyed out, can’t type anything here
the password is required for the impersonation account of data source Adventure Works DW.
Now, Whether I enter a password or not, I get:
Error 3 Either the 'Gary-PC\Gary' user does not have permission to create a new object in 'GARY-PC', or the object does not exist. 0 0
what objects is SSAS trying to create? are these objects in the relational database?
You have to add your user account as an administrator in the Analysis Services portion of the SQL server.
For some reason the database and the analysis services portion of the server do not share login information. The user you run Visual Studio under needs to have administrative access to the Analysis Services engine; this is the reason running as administrator works. The account you use to access the Database Engine is arbitrary.
Right-click on the SQL Server Management Studio icon and then select "Run as Administrator"
Select "Analysis Services" from the "Server type:" drop-down list in the "Connect to Server" dialog box, then click connect.
Right-click on the localhost definition in the Object Explorer panel and select Properties.
Click on Security in the left panel of the Analysis Server Properties.
Click the Add... button and type your user name and click the Check Names button to make sure you typed it right. Then click OK.
Click OK.
Note: This may not the most secure solution, but it enables not running Visual Studio as administrator every time and possibly opening up yourself to attack.
The Error message is:
"domain\user does not have permission to alter object 'mf20'..................."
I gone through the work around and found the solution as below:
Open Services, go to Analysis services--->right click on it -->
Properties-->LogOn --> select This Account -->give server and your
user name here. confirm it is your user, click ok.
Stop the service and again start the service. check the service Log On As in your user name.
That's it. save your cube in BIDS and close and reopen the cube, then deploy the cube. it will deploy and process it.
I hope this will help.
I had the same exact issue, but I was using the Adventure Works DW 2012. The problem is not on the database end but how your trying to deploy. When you run BIDS (or SQL Data Tools 2012 or Visual Studio 2010) use the "Run as Administrator" menu option. It will then ask you to authenticate, but the authentication should be accepted.
The user account you're using to deploy the SSAS database must have appropriate access to the SSAS instance in order to create the database and objects inside. Since this is a dev-setup, you could try adding your user account to the Server Admins.
In SSMS, log into the SSAS instance and right click on it (in the object explorer) and select properties. The last option in the left-pane is security and here is where you can add admin users.
are you talking about the " impersonation information" tab when you double click on the data source ? You should set it to "use service account"
In BIDS Solution Explorer → select the (in my case xx=08) Data Source and double-click on it. From the Data Source Designer screen click on the “Impersonation Information” tab, and enter the username and password of the user who has access for that remote data base or data source, and click OK.

Created a new database on SQL Server, can't be seen by web apps because permissions aren't set

So I created a new database on my SQL Server box and then added an ODBC entry so my ASP code knows what it is. Now I am getting this error:
Cannot open database "DB_NAME" requested by the login. The login failed.
I checked out the permissions by right clicking the db in Management Studio and checked permissions and low and behold it is empty.
I am just trying to duplicate the permissions of one of any of the other twenty or so databases sitting on the box. Is there a quick way to do this? Either way I just need to open up the lines of communication between my ASP code and my SQL Server db
Here is my connect code in ASP:
Set sqlConnection = Server.CreateObject("ADODB.CONNECTION")
sqlConnection.Open "DB_NAME"
Is there some reason to avoid using a proper connection string:-
sqlConnection.Open "Provider=sqloledb;Data Source=myServerAddress;Initial Catalog=DB_NAME;User Id=myUsername;Password=myPassword;"
On top of what John said, when you create a new database, you have to map the login (either SQL login or Windows login from Application Pool) to this database. You can use management studio to do this - open login properties and map it to the new database. Open Object Explorer, then click Security, Logins and rightclick login that is used by your application. Select Properties. Click User Mapping and add appropriate entry for your database in the grid you will see.
The other option is to run statement to create user within the database:
Use DB_NAME
go
create user [web_user] from login [web_login]
go
Another question is to see what rights have to be assigned to the user in the database. You have to check users permissions in one of the existing databases. Again in Object Explorer, click your existing database, then Security, Users and rightclick Properties of the user that you want to check. Observe information displayed on the dialog box in the General tab and check if there are any Securables assigned to the user. You have to copy these settings to the user in your new database.
HTH
Piotr
Try adding the user id and password into your connection string.

How to change default database in SQL Server without using MS SQL Server Management Studio?

I dropped a database from SQL Server, however it turns out that my login was set to use the dropped database as its default. I can connect to SQL Server Management Studio by using the 'options' button in the connection dialog and selecting 'master' as the database to connect to. However, whenever I try to do anything in object explorer, it tries to connect using my default database and fails.
Does anyone know how to set my default database without using object explorer?
What you can do is set your default database using the sp_defaultdb system stored procedure. Log in as you have done and then click the New Query button. After that simply run the sp_defaultdb command as follows:
Exec sp_defaultdb #loginame='login', #defdb='master'
Alternative to sp_defaultdb (which will be removed in a future version of Microsoft SQL Server) could be ALTER LOGIN:
ALTER LOGIN [my_user_name] WITH DEFAULT_DATABASE = [new_default_database]
Note: user and database names are provided without quotes (unlike the sp_defaultdb solution). Brackets are needed if name had special chars (most common example will be domain user which is domain\username and won't work without brackets):
ALTER LOGIN me WITH DEFAULT_DATABASE = my_database
but
ALTER LOGIN [EVILCORP\j.smith28] WITH DEFAULT_DATABASE = [prod\v-45]
To do it the GUI way, you need to go edit your login. One of its properties is the default database used for that login. You can find the list of logins under the Logins node under the Security node. Then select your login and right-click and pick Properties. Change the default database and your life will be better!
Note that someone with sysadmin privs needs to be able to login to do this or to run the query from the previous post.
Thanks to this post, I found an easier answer:
Open Sql Server Management Studio
Go to object Explorer -> Security -> Logins
Right click on the login and select properties
And in the properties window change the default database and click OK.
If you don't have permissions to change your default DB you could manually select a different DB at the top of your queries...
USE [SomeOtherDb]
SELECT 'I am now using a different DB'
Will work as long as you have permission to the other DB
Click on Change Connection icon
Click Options<<
Select the db from Connect to database drop down
Click on options on the connect to Server dialog and on the Connection Properties, you can choose the database to connect to on startup. Its better to leave it default which will make master as default. Otherwise you might inadvertently run sql on a wrong database after connecting to a database.
I'll also prefer ALTER LOGIN Command as in accepted answer and described here
But for GUI lover
Go to [SERVER INSTANCE] --> Security --> Logins --> [YOUR LOGIN]
Right Click on [YOUR LOGIN]
Update the Default Database Option at the bottom of the page
Tired of reading!!! just look at following
In case you can't login to SQL Server:
sqlcmd –E -S InstanceName –d master
Reference:
https://support.microsoft.com/en-us/kb/307864
This may or may not exactly answer the question, but I ran into this issue (and question) when I had changed my account to have a new database I had created as my "default database". Then I deleted that database and wanted to test my creation script, from scratch. I logged off SSMS and was going to go back in, but was denied -- cannot log into default database was the error. D'oh!
What I did was, on the login dialog for SSMS, go to Options, Connection Properties, then type master on the "Connect to database" combobox. Click Connect. Got me in. From there you can run the command to:
ALTER LOGIN [DOMAIN\useracct] WITH DEFAULT_DATABASE=[master]
GO
There is a little icon for change the connection, click on that and then go to Options and Select the db from Connect to database drop down
With the MSSQL queries below, you can change database on sqlcmd:
USE testdb
GO
Then, you can check the currently used database:
SELECT DB_NAME()
GO
testdb
Then, you can show all the existed databases:
SELECT name FROM master.sys.databases
GO
master
tempdb
model
msdb
testdb
In addition, if you don't specify a database on sqlcmd, "master" database is used by default.
If you use windows authentication, and you don't know a password to login as a user via username and password, you can do this: on the login-screen on SSMS click options at the bottom right, then go to the connection properties tab. Then you can type in manually the name of another database you have access to, over where it says , which will let you connect. Then follow the other advice for changing your default database
https://gyazo.com/c3d04c600311c08cb685bb668b569a67

Resources