I want to create a new Database in SQL Server using Oracle SQL Developer IDE.
When I am writing a query to create a new DB with user and login details it says user and db is successfully created but I can't see the user in the DB Created.
Below is the command which I am using:
create database subodh_test;
use subodh_test;
CREATE LOGIN test_subodh2
WITH PASSWORD = 'testing';
create user test_subodh2;
Also, Using Microsoft SQL Server Management Studio I am running below command that is executed perfectly:
USE master
DECLARE #DatabaseName sysname
DECLARE #Login sysname
DECLARE #Password sysname
DECLARE #SQLStatement nvarchar(4000)
SET #DatabaseName = N'testing'
SET #Login = N'testing'
SET #Password = N'test123'
SET #SQLStatement = N'ALTER DATABASE ' + #DatabaseName + N' SET
RESTRICTED_USER WITH ROLLBACK IMMEDIATE'
EXEC(#SQLStatement)
SET #SQLStatement = N'DROP DATABASE ' + #DatabaseName
EXEC(#SQLStatement)
EXEC sp_droplogin #Login
SET #SQLStatement = N'CREATE DATABASE ' + #DatabaseName
EXEC(#SQLStatement)
SET #SQLStatement = N'ALTER DATABASE ' + #DatabaseName + ' SET
ANSI_NULL_DEFAULT ON'
EXEC(#SQLStatement)
EXEC sp_addlogin #Login, #Password, #DatabaseName
EXEC sp_addsrvrolemember #Login, 'sysadmin'
SET #SQLStatement = N'USE ' + #DatabaseName +
N' EXEC sp_adduser #Login' +
N' EXEC sp_addrolemember ''db_owner'', #Login'
EXEC sp_executesql #SQLStatement,
N'#Login sysname',
#Login = #Login
Go
Related
We are moving to RDS and one of our apps needs access to tempdb and I am trying to figure out the best way to create a startup job that works with RDS. Currently we are able to create a stored proc that sets up the necessary permissions in the master database and use the EXEC sp_procoption 'AddPermissionsToTempDb', 'startup', 'true' command to set it to start at boot.
In RDS however we are not able to create stored procs in the master database. I tried creating the stored proc in a user-owned db but when I then try to create the startup job with EXEC sp_procoption 'mydb.dbo.AddPermissionsToTempDb', 'startup', 'true' it says it can't find the stored procedure or I do not have permission... Is there another way to accomplish this on RDS?
Was able to find a solution based on Jeroen Mostert's comment so credit goes to them. Here is the full query I used to create the startup job to grant permissions to a list of users to create, control and execute stored procedures on tempdb on an AWS RDS SQL Server instance:
USE msdb
go
declare #job_name varchar(50)
set #job_name = 'AddTempDBPermissionsOnStartup'
exec dbo.sp_delete_job #job_name = #job_name
declare #sql varchar(max)
select #sql = '
Declare #Users Table (username varchar(100) )
insert #Users(username) values (''[user1]''),
(''[user2]''),
(''[user3]'')
use tempdb
CREATE ROLE sp_executor GRANT EXECUTE TO sp_executor
CREATE ROLE sp_manipulator
GRANT CREATE PROCEDURE TO sp_manipulator
GRANT CONTROL TO sp_manipulator
DECLARE #username as NVARCHAR(100);
DECLARE User_Cursor CURSOR FOR
SELECT * from #Users
OPEN User_Cursor;
FETCH NEXT FROM User_Cursor INTO #username;
WHILE ##FETCH_STATUS = 0
BEGIN
PRINT #username
IF EXISTS(SELECT * FROM [tempdb].sys.database_principals WHERE type_desc = ''SQL_USER'' AND name = #username)
PRINT '' - user already exists''
ELSE
BEGIN
PRINT '' - creating user''
DECLARE #Sql VARCHAR(MAX)
SET #Sql =
''USE Tempdb'' + char(13) +
''CREATE USER '' + #username + '' FOR LOGIN '' + #username + char(13) +
''EXEC sp_addrolemember sp_executor, '' + #username + char(13) +
''EXEC sp_addrolemember sp_manipulator, '' + #username
EXEC (#Sql)
END
FETCH NEXT FROM User_Cursor INTO #username;
END;
CLOSE User_Cursor;
DEALLOCATE User_Cursor;
GO
'
--Add a job
EXEC dbo.sp_add_job
#job_name = #job_name ;
--Add a job step to run the command
EXEC sp_add_jobstep
#job_name = #job_name,
#step_name = N'job step',
#subsystem = N'TSQL',
#command = #sql
--Schedule the job to run at startup
exec sp_add_jobschedule #job_name = #job_name,
#name = 'RunAtStartSchedule',
#freq_type=64
--Add the job to the SQL Server Server
EXEC dbo.sp_add_jobserver
#job_name = #job_name
I have a question.
This code works:
USE [myDB]
GRANT CONNECT TO [user]
This other code won't (although its execution is done without errors):
DECLARE #nameDB AS VARCHAR (max)= 'myDB'
DECLARE #sql AS VARCHAR(max)
SELECT #sql = 'USE [' + #nameDB + ']'
PRINT #sql
EXEC sp_sqlexec #sql
GRANT CONNECT TO [user]
I need to use the latter (or similar) because I have many databases that need to be in read-only mode for certain user.
Any suggestions?
Try this:
DECLARE #nameDB AS sysname = 'myDB'
DECLARE #sql AS VARCHAR(max)
SELECT #sql = 'USE [' + #nameDB + ']; GRANT CONNECT TO [user];'
PRINT #sql
EXEC sp_sqlexec #sql
I'm creating a procedure to add users logins to a specific database. I call this procedure inside a trigger when a user is inserted in my database (I want to have different logins for each user in my site and in the access to the database).
Here's my trigger:
ALTER TRIGGER [dbo].[sys_users_insert]
ON [dbo].[sys_users]
AFTER INSERT
AS
BEGIN
DECLARE #USERNAME varchar(MAX)
SELECT #USERNAME = INSERTED.username
FROM INSERTED
EXEC CreateUser #USERNAME
END
The CreateUser is my procedure to create the logins in the database. The procedure is working. When I call it from a query editor:
EXEC CreateUser 'patricia.santos'
The user is created with success. But when I insert a user in the table and trigger runs the user isn't created nor is inserted in the table. And I found out that if I insert a user without dots in the username everything goes ok. So I believe is something with the dot but I would like to know if there's some workaround to accept usernames with dots.
UPDATE
Here's my procedure for creating logins in my database:
USE mydatabase
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER PROCEDURE [dbo].[CreateUser]
#USERNAME AS VARCHAR(MAX) = ''
AS
BEGIN
declare #sql nvarchar(MAX)
If NOT EXISTS(select loginname from master.dbo.syslogins where name = #USERNAME)
BEGIN
set #sql = 'CREATE LOGIN ' + #USERNAME +
' WITH
PASSWORD = ''something'',
CHECK_POLICY = OFF'
exec(#sql)
set #sql = 'ALTER SERVER ROLE sysadmin ADD MEMBER ' + #username
exec(#sql)
IF NOT EXISTS (SELECT * FROM sys.database_principals WHERE name = #USERNAME)
BEGIN
-- Creates a database user for the login created above.
set #sql = 'CREATE USER [' + #USERNAME + '] FOR LOGIN [' + #USERNAME + ']'
exec(#sql)
EXEC sp_addrolemember 'db_owner', #USERNAME
END
END
END
Thank you :)
I think the issue may be with your CreateUser procedure. Ensure that the “CREATE USER” clause and ALTER SERVER ROLE clause wraps the user name in square brackets. Please see updated procedure below:
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER PROCEDURE [dbo].[CreateUser]
#USERNAME AS VARCHAR(MAX) = ''
AS
BEGIN
declare #sql nvarchar(MAX)
If NOT EXISTS(select loginname from master.dbo.syslogins where name = #USERNAME)
BEGIN
set #sql = 'CREATE LOGIN [' + #USERNAME + '] ' +
' WITH
PASSWORD = ''something'',
CHECK_POLICY = OFF'
exec(#sql)
set #sql = 'ALTER SERVER ROLE sysadmin ADD MEMBER [' + #username + ']'
exec(#sql)
IF NOT EXISTS (SELECT * FROM sys.database_principals WHERE name = #USERNAME)
BEGIN
-- Creates a database user for the login created above.
set #sql = 'CREATE USER [' + #USERNAME + '] FOR LOGIN [' + #USERNAME + ']'
exec(#sql)
EXEC sp_addrolemember 'db_owner', #USERNAME
END
END
END
Let me know if this helps at all.
I have many databases that on my SQL Server box that start with a prefix zzz.
Is there a way to do a DROP DATABASE (or some other method) that will remove and delete the data files? If a connection is opened, I want it closed.
Basically I just want them gone.
Generate a drop script, copy/paste & run:
exec master.sys.sp_msforeachdb 'if ''?'' like ''ZZZ%'' print ''drop database [?]'''
Or drop directly in the SQL string if your brave.
use master;
go
-- this will drop all dbs that start with t5....
declare #strsql varchar(500)
declare #curname sysname
select #curname = name from sys.databases
where name like 't5%'
while( ##rowcount> 0)
begin
set #strsql ='ALTER DATABASE ' +#curname +' SET OFFLINE WITH ROLLBACK IMMEDIATE'
exec (#strsql)
set #strsql ='drop database '+#curname
exec (#strsql)
select #curname = name from sys.databases
where name like 't5%'
end
You could write a dynamic SQL to do this:
use master
go
declare #dbnames nvarchar(max)
declare #statement nvarchar(max)
declare #closeconnection nvarchar(max)
set #dbnames = ''
set #statement = ''
select #dbnames = #dbnames + ',[' + name + ']' from sys.databases where name like 'zzz%'
if len(#dbnames) = 0
begin
print 'no databases to drop'
end
else
BEGIN
SET #closeconnection = 'alter database ' + substring(#dbnames, 2, len(#dbnames))
+ ' SET SINGLE_USER WITH ROLLBACK IMMEDIATE'
set #statement = 'drop database ' + substring(#dbnames, 2, len(#dbnames))
print #statement
EXEC sp_executesql #closeconnection;
exec sp_executesql #statement;
end
Normally, the syntax to close all active connections to a database is:
--set it to single user to disable any other connections
ALTER DATABASE YourDatabase SET SINGLE_USER WITH ROLLBACK IMMEDIATE
--do your stuff here
--set it back to multiple users
ALTER DATABASE YourDatabase SET MULTI_USER
Alternatively, you could also generate a dynamic select list that populates your drop database statement along with close connection statements like this:
USE master;
Go
SELECT 'DROP DATABASE '+ name,
'alter database ' + name + ' SET SINGLE_USER WITH ROLLBACK IMMEDIATE'
FROM sys.databases WHERE name like 'zzz%';
GO
Courtesy: #SeriousM and OFH
I modified benjamin's script a bit so you only have to declare the prefix once.
use master;
declare #dbPrefix varchar(10)
set #dbPrefix = 'zzz_%';
declare #strsql varchar(500)
declare #curname sysname
select #curname = name from sys.databases
where name like #dbPrefix
while( ##rowcount> 0)
begin
set #strsql ='ALTER DATABASE ' +#curname +' SET SINGLE_USER WITH ROLLBACK IMMEDIATE'
exec (#strsql)
set #strsql ='drop database '+#curname
exec (#strsql)
select #curname = name from sys.databases
where name like #dbPrefix
end
I am using Linked server For transferring data using MSDTC
Alter Proc [dbo].[usp_Select_TransferingDatasFromServerCheckingforExample]
#RserverName varchar(100), ----- Server Name
#RUserid Varchar(100), ----- server user id
#RPass Varchar(100), ----- Server Password
#DbName varchar(100) ----- Server database
As
Set nocount on
Set Xact_abort on
Declare #user varchar(100)
Declare #userID varchar(100)
Declare #Db Varchar(100)
Declare #Lserver varchar(100)
Select #Lserver = ##servername
Select #userID = suser_name()
select #User=user
Exec('if exists(Select 1 From [Master].[' + #user + '].[sysservers] where srvname = ''' +
#RserverName + ''') begin Exec sp_droplinkedsrvlogin ''' + #RserverName + ''',''' + #userID +
''' exec sp_dropserver ''' + #RserverName + ''' end ')
declare #ColumnList varchar(max)
set #ColumnList = null
Select #ColumnList = case when #ColumnList is not null then #ColumnList + ',' + quotename(name) else quotename(name) end
From syscolumns where Id = object_id('Crnot') order by colid
Set identity_Insert Crnot On
exec ('Insert Into ['+ #RserverName + '].'+ #DbName + '.'+ #user +'.Crnot ('+ #ColumnList +') Select '+ #ColumnList +' from Crnot ')
Set identity_Insert Crnot Off
Exec sp_droplinkedsrvlogin #RserverName,#userID
Exec sp_dropserver #RserverName
when executing this qry i get the error "No transaction Active"
Check your MS DTC configuration (cut and paste from a doc, not checked recently):
Start, Run, dcomcnfg.exe
In the Component Services window, expand Component Services... Computers...My Computer.
Right-click My Computer, Properties.
Click Security Configuration on the MSDTC tab.
Click to select the Network DTC Access check box.
Set both the Allow Inbound and Allow Outbound check boxes
Under the Transaction Manager Communication group, click to select the No Authentication Required option.
Verify that the DTC Logon Account name is set to NT AUTHORITY\NetworkService.
Click Ok etc
In your code, Set identity_Insert Crnot only applies to local objects.
It should be part of the dynamic SQL INSERT