How do I set the default database in Sql Server from code? - sql-server

I can't seem to figure out how to set the default database in Sql Server from code. This can be either .Net code or T-Sql (T-Sql would be nice since it would be easy to use in any language). I searched Google and could only find how to do it in Sql Server Management Studio.

ALTER LOGIN should be used for SQL Server 2005 or later:
http://technet.microsoft.com/en-us/library/ms189828.aspx
ALTER LOGIN <login_name> WITH DEFAULT_DATABASE = <default_database>
sp_defaultdb eventually will be removed from SQL Server:
http://technet.microsoft.com/en-us/library/ms181738.aspx

from: http://doc.ddart.net/mssql/sql70/sp_da-di_6.htm
sp_defaultdb [#loginame =] 'login' , [#defdb =] 'database'

Thanks Stephen.
As a note, if you are using Windows Authentication, the #loginname is YourDomain\YourLogin (probably obvious to everybody else, but took me a couple tries.
sp_defaultdb #loginame='YourDomain\YourLogin', #defdb='YourDatabase'

If you're trying to change which database you are using after you are logged in, you can use the USE command. E.g. USE Northwind.
https://www.tutorialspoint.com/sql/sql-select-database.htm

Related

Cannot select data on linked SQL Server using SQL authentication

I have created a linked server using SQL authentication and the connection seems to work as I can select from the master database.
However, I cannot select from a specific database to which the SQL account I use in the linked server has access.
I tried following the suggestions in other similar posts in here but to no avail. Any help really appreciated. I am attaching a screenshot of the security settings.
Thanks.
This is a bit of a long shot, but since no one else is answering so we'll give it a go. If you restored the DB from another server, it's possible the ssid's are out of whack. Switch context to the DB on the linked server and run the following.
EXEC sys.sp_change_users_login #Action = 'Report'
If any results show up with the login in question, remove it by running this.
EXEC  sp_change_users_login #Action='update_one', #UserNamePattern='your_user',#LoginName='your_login';

How do I set the default database in SQL Server using T-SQL? (updated)

Corrective update/replacement for this question, as applicable to currently supported versions of SQL.
How do I set the default database for a particular login, using T-SQL code?
The accepted answer uses a deprecated system stored procedure; the MSDN/BOL documentation prefers the more standard & current "ALTER LOGIN" syntax, as shown in the newer answer from Tim.
https://stackoverflow.com/a/106015/112764
ALTER LOGIN should be used for SQL Server 2005 or later:
http://technet.microsoft.com/en-us/library/ms189828.aspx
ALTER LOGIN <login_name> WITH DEFAULT_DATABASE = <default_database>
Sample usage:
ALTER LOGIN [JoeShmoe] WITH DEFAULT_DATABASE = [tempdb]
Extra note: although MSDN/BOL pages have been updated to exclude 2005 from the "applies to" list (it usually reads "SQL Server (starting with 2008)" or similarly), in this case, I verified that the command still works on 2005.

SQL Server Server Properties

I want to change the following server properties and settings in the SQL Server 2012. I just want to change the language from English(United States) to English(United Kingdom). Please suggests me the possible ways in order to get through.
The language setting of the server doesn't matter, that's just a default setting for new added user, where you haven't define a language setting.
You have to change the language for your used account in SQL Server. In SSMS goto "Security" => "Logins" and open the properties of your account, at the bottom of the dialog you will find "Default language", I guess it's now "English", change it to "British English".
Or us a T-SQL Command:
USE [master]
GO
ALTER LOGIN [YourAccountName] WITH DEFAULT_LANGUAGE=[British]
GO
Source : https://social.msdn.microsoft.com/Forums/sqlserver/en-US/917d9534-ca26-4321-ab5a-1bf084cc7ee3/how-can-i-change-language-from-us-english-to-uk-english?forum=sqlexpress
This might help you out
https://dba.stackexchange.com/questions/52402/how-to-change-default-language-of-sql-server-management-studio-2008r2
But, see that this is for SQL server 2008 R2

SQL Server trigger to determine crud source

I have a web application and the database it is on my client building. I want to know if the CRUD (create, update, delete) actions made from my application of "someone" for any reason, done it from SQL Server Management Studio.
Thanks in advance
You could define a trigger for update/insert on the tables you want to audit, then adapt the following code from this MSDN article on AppName() :
DECLARE #CurrentApp varchar(40)
SET #CurrentApp = APP_NAME()
IF #CurrentApp <> 'SQL Server Management Studio - Query'
PRINT 'This process was not started by a SQL Server Management Studio query session.';
I believe it might be open to spoofing though, as I think programs can specify the application name in the connection string.

Querying a view that's not located on the same server (SQL Server 2005)

I'm trying to query a database view that's not located on the same server as the stored procedure I'm running.
I heard about using "linked servers", but I have no access to the server's configuration at all ...
Thanks in advance !
Use OPENDATASOURCE:
SELECT *
FROM OPENDATASOURCE(
'SQLOLEDB',
'Data Source=ServerName;User ID=MyUID;Password=MyPass'
).Northwind.dbo.Categories
You can do this, but it does require the DBA to set up the link. If you don't have access to the server's configuration and the DBA is not on board, you're out of luck.

Resources