Sysadmin and sa in sql server 2000 - drop and recreate login? - sql-server-2000

A somewhat open-ended question so any information that closes the gaps in my knowledge would be appreciated.
When doing a SQL Server upgrade from 2000 to 2008 the upgrade advisor can give the following warning:
The Upgrade Advisor detected one or more user-defined login names that match the names of fixed server roles. Fixed server role names are reserved in SQL Server 2008. Rename the login before upgrading to SQL Server 2008.
I believe this is because say for example a login of 'sysadmin' in a sql server 2000 database will clash with a fixed server role of 'sysadmin' in 2008..
Is there a way to safely drop and re-create these logins or could dropping a login of say 'sa' have unintended consequences?

Repro of issue :
1) Delete the Builtin/administrator account .
2) Try to connect to SQL Server through sqlcmd or Management studio .You will get the error 18456 Level 14 State 1.
3) Assume that i have forgotten the SA password as well.
Solution:
1) Login to the server using administrator account .
2) Stop SQL Server service and start it with -m switch(single user mode) .
2) Type sqlcmd -E and hit enter .If its named instance then sqlcmd -S -E and hit enter.
4) You will see > sign
5) Commands you need to use
CREATE LOGIN [BUILTIN\Administrators] FROM WINDOWS WITH DEFAULT_DATABASE= [master], DEFAULT_LANGUAGE=[us_english]
go
6) Give sysadmin role to the login we just created
EXEC master..sp_addsrvrolemember #loginame = N'BUILTIN\Administrators', #rolename = N'sysadmin'
GO
7) You are done.Exit out of it .8) restart you SQL Server service without -m parameter.
You are done .Login again to SQL Server and reset the SA password .Save the SA password for the rainy day .
Source

Related

Taking ownership for SQL Server Management Studio

I'm new to SQL Server 2008. I just installed SQL Server Express. I'm having trouble creating a new database, and I think I don't have permission.
I login like this, please see this screenshot:
Then I tried to create a new database and I got this:
I tried to search for some solution and this what I've got:
http://blogs.msdn.com/b/sqlexpress/archive/2010/02/23/how-to-take-ownership-of-your-local-sql-server-2008-express.aspx
But I can't download the script and the page says:
An error occurred while processing your request.
Please help. Kind regards
I resolved my problem with the following steps:
Set the instance of the SQL Service to single-user mode:
Open SQL Server Configuration Manager. Double click SQL Server Services.
Stop all SQL Server services
Right click SQL service and click Properties, in the Advanced tab, look for 'Startup Parameters'
Insert '-m;' at the beginning of the Startup Parameters value
Start the SQL service
Open SQL Server Management Studio and login with Windows authentication, you can now add user or change password of different users.
Hope this helps!
Try logging in with the sa account and grant permissions to your Windows account.
If you do not know the sa password use sqlcmd and execute the following commands:
Use Master
Go
ALTER LOGIN [sa] WITH PASSWORD=N'NewPassword'
Go
Login with the sa account and GRANT permission to the account.
USE Master;
GRANT CREATE DATABASE TO Jommel;

Creation of database in SQL Server 2008

I am unable to create a database in SQL Server 2008. This is the message that I recieve every time:
TITLE: Microsoft SQL Server Management Studio
------------------------------
Create failed for Database 'university'. (Microsoft.SqlServer.Smo)
------------------------------
ADDITIONAL INFORMATION:
An exception occurred while executing a Transact-SQL statement or batch. (Microsoft.SqlServer.ConnectionInfo)
------------------------------
CREATE DATABASE permission denied in database 'master'. (Microsoft SQL Server, Error: 262)
For help, click: http://go.microsoft.com/fwlink?ProdName=Microsoft+SQL+Server&ProdVer=10.00.1600&EvtSrc=MSSQLServer&EvtID=262&LinkId=20476
What should be done?
This is related to the permission of the logged in user.
Solution 1)
Click on Start -> Click in Microsoft SQL Server 2005
Now right click on SQL Server Management Studio
Click on Run as administrator
Solution2)
check with select user_name() if you are not logged in as guest.
Add a domain account as sysadmin to SQL Express with SQLCMD
Start a command shell elevated
type SQLCMD –S (local)\sqlexpress
CREATE LOGIN [your domain account] FROM WINDOWS;
check if the login is created successfully
SELECT NAME FROM SYS.SERVER_PRINCIPALS
Grant sysadmin rights
SP_ADDSRVROLEMEMBER ‘darth\vader’, ‘sysadmin’
Reference : http://blogs.msdn.com/b/dparys/archive/2009/09/17/create-database-permission-denied-in-database-master-my-fix.aspx
You need to give the account your using permission to create databases.
You may need to login using the sa account and perform a GRANT on the user account.
GRANT CREATE DATABASE TO YourAccount;
http://msdn.microsoft.com/en-us/library/ms178569.aspx

sql studio express 2005 lost sa password issue

I have a problem, I'm using windows 7 with MS SQL Server Management Studio Express 2005 although I've lost my administrator/ sa password. Is there a command I can use to reset the password, without the old one?
I can login using windows authenication.
many thanks,
James
Login to the SQL Server computer as the Administrator of that computer. Open Query Analyzer and connect to SQL Server using Windows NT authentication. Run sp_password as shown below to reset the sa password:
sp_password #new = 'will_never_forget_again', #loginame = 'sa'
EDIT:
This is unexpected, as you were able to get in to detach a db, so you must
have some privileges. The message you got:
... is a response to the sp_password command. So when you say you tried to
reconnect with 'sa', can you tell us how you did that?
Also, when using the SQLCMD tool, you have to type GO to execute a command:
SP_PASSWORD #NEW = 'my_password', #loginame = 'sa'
GO
Then you need to exit before you try to reconnect.
Try getting in again, and seeing who you are. So after connecting:
C:\sqlcmd -E -d master
Please run this:
SELECT suser_sname(), user_name()
GO
Also, run this after you obtain your user name and enter your new password per my original answer:
ALTER LOGIN sa ENABLE
GO
In the object explorer, go to Security-> Logins and click on preferences for the "sa"-account. There you can reset the password
Because you can login using your windows authentication then it's pretty easy.
what you need is just
login to your management studio,
on the tree view, expand "security" folder, and also expand the
"Logins" folder,
right click on "sa"
change the password. done

SQL Server Script to create a new user

I want to write a script to create a admin user ( with abcd password ) in SQL Server Express.
Also I want to assign this user admin full rights.
Based on your question, I think that you may be a bit confused about the difference between a User and a Login. A Login is an account on the SQL Server as a whole - someone who is able to log in to the server and who has a password. A User is a Login with access to a specific database.
Creating a Login is easy and must (obviously) be done before creating a User account for the login in a specific database:
CREATE LOGIN NewAdminName WITH PASSWORD = 'ABCD'
GO
Here is how you create a User with db_owner privileges using the Login you just declared:
Use YourDatabase;
GO
IF NOT EXISTS (SELECT * FROM sys.database_principals WHERE name = N'NewAdminName')
BEGIN
CREATE USER [NewAdminName] FOR LOGIN [NewAdminName]
EXEC sp_addrolemember N'db_owner', N'NewAdminName'
END;
GO
Now, Logins are a bit more fluid than I make it seem above. For example, a Login account is automatically created (in most SQL Server installations) for the Windows Administrator account when the database is installed. In most situations, I just use that when I am administering a database (it has all privileges).
However, if you are going to be accessing the SQL Server from an application, then you will want to set the server up for "Mixed Mode" (both Windows and SQL logins) and create a Login as shown above. You'll then "GRANT" priviliges to that SQL Login based on what is needed for your app. See here for more information.
UPDATE: Aaron points out the use of the sp_addsrvrolemember to assign a prepared role to your login account. This is a good idea - faster and easier than manually granting privileges. If you google it you'll see plenty of links. However, you must still understand the distinction between a login and a user.
Full admin rights for the whole server, or a specific database? I think the others answered for a database, but for the server:
USE [master];
GO
CREATE LOGIN MyNewAdminUser
WITH PASSWORD = N'abcd',
CHECK_POLICY = OFF,
CHECK_EXPIRATION = OFF;
GO
EXEC sp_addsrvrolemember
#loginame = N'MyNewAdminUser',
#rolename = N'sysadmin';
You may need to leave off the CHECK_ parameters depending on what version of SQL Server Express you are using (it is almost always useful to include this information in your question).
You can use:
CREATE LOGIN <login name> WITH PASSWORD = '<password>' ; GO
To create the login (See here for more details).
Then you may need to use:
CREATE USER user_name
To create the user associated with the login for the specific database you want to grant them access too.
(See here for details)
You can also use:
GRANT permission [ ,...n ] ON SCHEMA :: schema_name
To set up the permissions for the schema's that you assigned the users to.
(See here for details)
Two other commands you might find useful are ALTER USER and ALTER LOGIN.
If you want to create a generic script you can do it with an Execute statement with a Replace with your username and database name
Declare #userName as varchar(50);
Declare #defaultDataBaseName as varchar(50);
Declare #LoginCreationScript as varchar(max);
Declare #UserCreationScript as varchar(max);
Declare #TempUserCreationScript as varchar(max);
set #defaultDataBaseName = 'data1';
set #userName = 'domain\userName';
set #LoginCreationScript ='CREATE LOGIN [{userName}]
FROM WINDOWS
WITH DEFAULT_DATABASE ={dataBaseName}'
set #UserCreationScript ='
USE {dataBaseName}
CREATE User [{userName}] for LOGIN [{userName}];
EXEC sp_addrolemember ''db_datareader'', ''{userName}'';
EXEC sp_addrolemember ''db_datawriter'', ''{userName}'';
Grant Execute on Schema :: dbo TO [{userName}];'
/*Login creation*/
set #LoginCreationScript=Replace(Replace(#LoginCreationScript, '{userName}', #userName), '{dataBaseName}', #defaultDataBaseName)
set #UserCreationScript =Replace(#UserCreationScript, '{userName}', #userName)
Execute(#LoginCreationScript)
/*User creation and role assignment*/
set #TempUserCreationScript =Replace(#UserCreationScript, '{dataBaseName}', #defaultDataBaseName)
Execute(#TempUserCreationScript)
set #TempUserCreationScript =Replace(#UserCreationScript, '{dataBaseName}', 'db2')
Execute(#TempUserCreationScript)
set #TempUserCreationScript =Replace(#UserCreationScript, '{dataBaseName}', 'db3')
Execute(#TempUserCreationScript)
CREATE LOGIN AdminLOGIN WITH PASSWORD = 'pass'
GO
Use MyDatabase;
GO
IF NOT EXISTS (SELECT * FROM sys.database_principals WHERE name = N'AdminLOGIN')
BEGIN
CREATE USER [AdminLOGIN] FOR LOGIN [AdminLOGIN]
EXEC sp_addrolemember N'db_owner', N'AdminLOGIN'
EXEC master..sp_addsrvrolemember #loginame = N'adminlogin', #rolename = N'sysadmin'
END;
GO
this full help you for network using:
1- Right-click on SQL Server instance at root of Object Explorer, click on Properties
Select Security from the left pane.
2- Select the SQL Server and Windows Authentication mode radio button, and click OK.
3- Right-click on the SQL Server instance, select Restart (alternatively, open up Services and restart the SQL Server service).
4- Close sql server application and reopen it
5- open 'SQL Server Configuration Manager' and tcp enabled for network
6-Double-click the TCP/IP protocol, go to the IP Addresses tab and scroll down to the IPAll section.
7-Specify the 1433 in the TCP Port field (or another port if 1433 is used by another MSSQL Server) and press the OK
8-Open in Sql Server: Security And Login And Right Click on Login Name And Select Peroperties And Select Server Roles And
Checked The Sysadmin And Bulkadmin then Ok.
9-firewall: Open cmd as administrator and type:
netsh firewall set portopening protocol = TCP port = 1433 name = SQLPort mode = ENABLE scope = SUBNET profile = CURRENT
This past week I installed Microsoft SQL Server 2014 Developer Edition on my dev box, and immediately ran into a problem I had never seen before.
I’ve installed various versions of SQL Server countless times, and it is usually a painless procedure. Install the server, run the Management Console, it’s that simple. However, after completing this installation, when I tried to log in to the server using SSMS, I got an error like the one below:
SQL Server login error 18456
“Login failed for user… (Microsoft SQL Server, Error: 18456)”
I’m used to seeing this error if I typed the wrong password when logging in – but that’s only if I’m using mixed mode (Windows and SQL Authentication). In this case, the server was set up with Windows Authentication only, and the user account was my own. I’m still not sure why it didn’t add my user to the SYSADMIN role during setup; perhaps I missed a step and forgot to add it. At any rate, not all hope was lost.
The way to fix this, if you cannot log on with any other account to SQL Server, is to add your network login through a command line interface. For this to work, you need to be an Administrator on Windows for the PC that you’re logged onto.
Stop the MSSQL service.
Open a Command Prompt using Run As Administrator.
Change to the folder that holds the SQL Server EXE file; the default for SQL Server 2014 is “C:\Program Files\Microsoft SQL Server\MSSQL12.MSSQLSERVER\MSSQL\Binn”.
Run the following command: “sqlservr.exe –m”. This will start SQL Server in single-user mode.
While leaving this Command Prompt open, open another one, repeating steps 2 and 3.
In the second Command Prompt window, run “SQLCMD –S Server_Name\Instance_Name”
In this window, run the following lines, pressing Enter after each one:
1
CREATE LOGIN [domainName\loginName] FROM WINDOWS
2
GO
3
SP_ADDSRVROLEMEMBER 'LOGIN_NAME','SYSADMIN'
4
GO
Use CTRL+C to end both processes in the Command Prompt windows; you will be prompted to press Y to end the SQL Server process.
Restart the MSSQL service.
That’s it! You should now be able to log in using your network login.

Stored Procedure: Linked server login failure

I'm getting "Msg 18456, Level 14, State 1, Line 1
Login failed for user 'NT AUTHORITY\ANONYMOUS LOGON'." from when trying to execute the code below. I've change all of the critical information, but you should get the idea.
Are some of my parameter incorrect? The local sql admin username is correct and the remote username and password is correct, but it still keeps telling me that the login failed. Any ideas?
Overall, are there other changes I need to make? Can I insert data this way?
Both DBs are sql server 2005. One is local, one is offsite and accessible via a secure vpn tunnel. I have no trouble acessing the offsite DB using SSMS using the username and password i've been provided(those i've been using in my SP).
-- establish the linked server and login.
EXEC sp_addlinkedserver #server=SERVER1,#srvproduct='',#provider='SQLNCLI', #datasrc='SERVER IP ADDRESS'
EXEC sp_addlinkedsrvlogin SERVER1, 'false', 'LOCAL SQL ADMIN USERNAME', 'REMOTE USERNAME', 'REMOTE PASSWORD'
insert into [SERVER1].DATABASE.dbo.INSERTTABLE(....) select fields from localtable
-- drop the linked server and login
EXEC Sp_DropServer SERVER1, 'droplogins'
I suspect the target server is set to "Windows Authentication" only.
When you try and connect as a SQL login (specified in sp_addlinkedsrvlogin), it tries to interpret the credentials as windows and fails
This error normally occurs when #useself = 'true' for sp_addlinkedsrvlogin and the calling SQL Server is not configured for delegation. The server (not SQL server) can not pass through the Windows credentials.
1) use named parameters for sp_addlinkedsrvlogin for explicitness.
2) use "go" between statements (don't execute the above as a single batch).
3) set "local sql admin username" to null - to rule that out
4) just to note, it appears that remote username/password (if specified) need to be sql-server logins, not NT Network Logins
Start with those...!
Unsure of the exact cause folks. I just moved on a created a Linked Server through the SSMS GUI.