Wrox Programmer Forums
Go Back   Wrox Programmer Forums > SQL Server > SQL Server 2000 > SQL Server 2000
|
SQL Server 2000 General discussion of Microsoft SQL Server -- for topics that don't fit in one of the more specific SQL Server forums. version 2000 only. There's a new forum for SQL Server 2005.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the SQL Server 2000 section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old September 29th, 2003, 04:31 AM
Registered User
 
Join Date: Sep 2003
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default Modifying user permissions through SQL

Hi,

Could anyone help me with the following?

I am writing an SQL script to create a new database on an existing MS SQL Server (2000). To this end the master database on the server is accessed with the user 'sa' to obtain sysadmin rights. The database is then created according to the script included below. Next, I want to create a new login for SQL Server authentication, that should finally be the only user (besides 'sa') that can access the database. This could be done by:

sp_addlogin 'appname', 'pwd', 'dbname'

Works so far, but here I get stuck. When connecting to the SQL server using 'uid=appname;pwd=pwd;', the user apperently does not have access rights for the database 'dbname' yet. My question: How (through SQL) can you set the permission properties for the user 'appname' such that it gets exclusive full rights for the database?

With best regards,
Sander



F.Y.I. Here is the script used to create the database

CREATE DATABASE [dbname] ON (NAME = N'dbname_dat', FILENAME = N'C:\Program Files\Microsoft SQL Server\MSSQL\data\dbname.mdf' , SIZE = 4, FILEGROWTH = 10%) LOG ON (NAME = N'dbname_log', FILENAME = N'C:\Program Files\Microsoft SQL Server\MSSQL\data\dbname.ldf' , SIZE = 2, FILEGROWTH = 10%)
COLLATE SQL_Latin1_General_CP1_CI_AS
GO

exec sp_dboption N'dbname' , N'autoclose', N'false'
exec sp_dboption N'dbname' , N'bulkcopy', N'true'
exec sp_dboption N'dbname' , N'trunc. log', N'true'
exec sp_dboption N'dbname' , N'torn page detection', N'true'
exec sp_dboption N'dbname' , N'read only', N'false'
exec sp_dboption N'dbname' , N'dbo use', N'false'
exec sp_dboption N'dbname' , N'single', N'false'
exec sp_dboption N'dbname' , N'autoshrink', N'false'
exec sp_dboption N'dbname' , N'ANSI null default', N'false'
exec sp_dboption N'dbname' , N'recursive triggers', N'false'
exec sp_dboption N'dbname' , N'ANSI nulls', N'false'
exec sp_dboption N'dbname' , N'concat null yields null', N'false'
exec sp_dboption N'dbname' , N'cursor close on commit', N'false'
exec sp_dboption N'dbname' , N'default to local cursor', N'false'
exec sp_dboption N'dbname' , N'quoted identifier', N'false'
exec sp_dboption N'dbname' , N'ANSI warnings', N'false'
exec sp_dboption N'dbname' , N'auto create statistics', N'true'
exec sp_dboption N'dbname' , N'auto update statistics', N'true'
GO

USE dbname
GO

CREATE TABLE Accounts (
    AccountID tinyint IDENTITY (0, 1) NOT NULL ,
    Naam nvarchar (20) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL ,
..... etc
 
Old September 29th, 2003, 04:45 AM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 1,212
Thanks: 0
Thanked 1 Time in 1 Post
Default

As you have seen sp_addlogin just gives the account a logon to the db server. You need sp_grantdbaccess to get into specific databases. Also you will probably need sp_addrolemember to control the access rights within each db. Alternatively you can give specific object level permissions using the T-SQL GRANT or DENY statements.

hth
Phil
 
Old October 1st, 2003, 03:59 AM
Registered User
 
Join Date: Sep 2003
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thanks,

Using sp_grantdbaccess and sp_addrolemember it was easy to provide db_owner privileges for the user. Here's the script that works for me:

--Create a SQL Server login
USE master
EXEC sp_addlogin 'appname','pwd', 'dbname'
USE dbname

--Remove guest access from the database, if necessary
IF exists (SELECT * FROM sysusers WHERE name='guest' AND status<>0)
EXEC sp_revokedbaccess 'guest'

--Grant database access for the new login with db_owner privileges
IF NOT exists (SELECT * FROM sysusers WHERE name='MiceBase')
EXEC sp_grantdbaccess 'MiceBase'
EXEC sp_addrolemember db_owner,'MiceBase'






Similar Threads
Thread Thread Starter Forum Replies Last Post
Permissions on DB without user-level security nbourre Access VBA 4 June 7th, 2008 08:09 AM
Setting SQL Server 2000 access permissions / Syste snejsnej SQL Server 2000 1 May 18th, 2006 08:10 AM
User permissions on mapped network drive affinity Classic ASP Professional 0 January 12th, 2006 12:22 PM
Run DTS with a windows user, not a SQL user Kalli SQL Server DTS 1 September 15th, 2005 11:53 PM
Modifying User Access Permissions to Folders (VB) gakhar Pro VB 6 0 April 14th, 2004 01:20 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.