R
Reg
I have some simple scripts to create databases and add users/roles. The
scripts run just fine in Management Studio. When I run them programmitcally
they execute without any errors, however nothing happens; neither the users
nor the databases are created.
Here's an example for adding a user and setting role to sysadmin:
-- Get Major Version of Sql Server
-- 8 = Sql Server 2000
-- 9 = Sql Server 2005
DECLARE @version nvarchar(128)
select @version = LEFT(CONVERT(nvarchar(128),
SERVERPROPERTY('productversion')), 1)
-- Store traffic user and password
DECLARE @cmduser varchar(128)
DECLARE @cmdpass varchar(128)
select @cmduser = 'myuser'
select @cmdpass = 'mypassword'
DECLARE @userid int
-- Create user if doesn't exist
if @version = '9'
begin
--if not exists (select * from [master].[sys].[syslogins] where name =
@cmduser)
IF NOT EXISTS (SELECT * FROM sys.server_principals WHERE name = @cmduser)
begin
EXEC sp_addlogin @cmduser,@cmdpass
end
end
if @version = '8'
begin
-- Is there a proper way to do this in SQL Server 2000?
if not exists (select * from [master].[dbo].[sysxlogins] where name =
@cmduser)
begin
EXEC sp_addlogin @cmduser,@cmdpass
end
end
-- Give user system admin privileges
--EXEC master..sp_addsrvrolemember @cmduser, 'sysadmin'
EXEC master..sp_addsrvrolemember @loginame = @cmduser, @rolename = 'sysadmin'
scripts run just fine in Management Studio. When I run them programmitcally
they execute without any errors, however nothing happens; neither the users
nor the databases are created.
Here's an example for adding a user and setting role to sysadmin:
-- Get Major Version of Sql Server
-- 8 = Sql Server 2000
-- 9 = Sql Server 2005
DECLARE @version nvarchar(128)
select @version = LEFT(CONVERT(nvarchar(128),
SERVERPROPERTY('productversion')), 1)
-- Store traffic user and password
DECLARE @cmduser varchar(128)
DECLARE @cmdpass varchar(128)
select @cmduser = 'myuser'
select @cmdpass = 'mypassword'
DECLARE @userid int
-- Create user if doesn't exist
if @version = '9'
begin
--if not exists (select * from [master].[sys].[syslogins] where name =
@cmduser)
IF NOT EXISTS (SELECT * FROM sys.server_principals WHERE name = @cmduser)
begin
EXEC sp_addlogin @cmduser,@cmdpass
end
end
if @version = '8'
begin
-- Is there a proper way to do this in SQL Server 2000?
if not exists (select * from [master].[dbo].[sysxlogins] where name =
@cmduser)
begin
EXEC sp_addlogin @cmduser,@cmdpass
end
end
-- Give user system admin privileges
--EXEC master..sp_addsrvrolemember @cmduser, 'sysadmin'
EXEC master..sp_addsrvrolemember @loginame = @cmduser, @rolename = 'sysadmin'