Showing posts with label role. Show all posts
Showing posts with label role. Show all posts

Wednesday, March 28, 2012

Script Won't Run in Job But Runs in Query Analyzer

I have a script that is basically a cursor that uses
sp_addrolemember to add all logins on the server to the
db_datareader role in every database. I use a variable
for each login. The script is in a job and scheduled to
run daily. I receive the following error: Msg 913, Sev
16: Could not find database ID %. Database may not be
activated yet or may be in transition. The problem is
that the EXACT same script will run in Query Analyzer with
NO errors. Why wouldn't the script give the same error in
Query Analyzer?
Thanks.I would have to see the script in order to (possibly) suggest anything...
:-)
Tibor Karaszi, SQL Server MVP
Archive at:
http://groups.google.com/groups?oi=...ublic.sqlserver
"Brandon Tolleson" <anonymous@.discussions.microsoft.com> wrote in message
news:617901c3e5d7$b9b63000$a401280a@.phx.gbl...
quote:

> I have a script that is basically a cursor that uses
> sp_addrolemember to add all logins on the server to the
> db_datareader role in every database. I use a variable
> for each login. The script is in a job and scheduled to
> run daily. I receive the following error: Msg 913, Sev
> 16: Could not find database ID %. Database may not be
> activated yet or may be in transition. The problem is
> that the EXACT same script will run in Query Analyzer with
> NO errors. Why wouldn't the script give the same error in
> Query Analyzer?
> Thanks.
|||DECLARE @.lname varchar(100)
DECLARE @.uname varchar(100)
Declare @.mystr varchar(1000)
Declare @.dbname varchar(100)
DECLARE MacroVal CURSOR FOR Select name from syslogins
where name like 'MyDomain\
%' order by name
OPEN MacroVal
FETCH NEXT FROM MacroVal INTO @.lname
WHILE(@.@.FETCH_STATUS <> -1)
BEGIN
IF (@.@.FETCH_STATUS <> -2)
BEGIN
Set @.uname = substring(@.lname,charindex('',@.lname)
+1,50)
DECLARE DBName CURSOR FOR Select name from
sysdatabases order by name
OPEN DBName
FETCH NEXT FROM DBName INTO @.dbname
WHILE(@.@.FETCH_STATUS <> -1)
BEGIN
IF (@.@.FETCH_STATUS <> -2)
BEGIN
Set @.mystr = 'USE '+@.dbname+' If not
exists (select * from sysusers where SUSER_SNAME(sid)
= ''' +@.lname+''') Exec
sp_grantdbaccess '''+@.lname+''', '''+@.uname+''''
if @.uname not like 'student%'
BEGIN
Print (@.mystr)
Exec (@.mystr)
Set @.mystr = 'USE '+@.dbname+' If
exists (select * from sysusers where SUSER_SNAME(sid)
= '''+@.lname+''' and name <> ''dbo'') BEGIN EXEC
sp_addrolemember ''db_datareader'', '''+@.uname+''' EXEC
sp_addrolemember''db_backupoperator'', '''+@.uname+''' END'
Print (@.mystr)
Exec (@.mystr)
END
END
FETCH NEXT FROM DBName INTO @.dbname
END
CLOSE DBName
DEALLOCATE DBName
END
FETCH NEXT FROM MacroVal INTO @.lname
END
CLOSE MacroVal
DEALLOCATE MacroVal
GO
quote:

>--Original Message--
>I would have to see the script in order to (possibly)

suggest anything...
quote:

>:-)
>--
>Tibor Karaszi, SQL Server MVP
>Archive at:
>http://groups.google.com/groups?

oi=djq&as_ugroup=microsoft.public.sqlserver
quote:

>
>"Brandon Tolleson" <anonymous@.discussions.microsoft.com>

wrote in message
quote:

>news:617901c3e5d7$b9b63000$a401280a@.phx.gbl...
with[QUOTE]
in[QUOTE]
>
>.
>
|||There doesn't seem to be a problem with the script per se. I just ran it on
my SQL Server and the
outcome was success. Did you specify an output file for the jobstep? This ca
n sometimes give more
info about the problem. In my output file, I only had messages such as:
USE master If not exists (select * from sysusers where SUSER_SNAME(sid)
= 'TIBORK\Tibor') Exec
sp_grantdbaccess 'TIBORK\Tibor', 'Tibor' [SQLSTATE 01000]
Granted database access to 'TIBORK\Tibor'. [SQLSTATE 01000]
You might want to go though the job in Agent so you didn't get any unfortuna
te linebreaks anywhere.
Although, the error message you posted didn't indicate that this is the prob
lem.
My guess is that some database isn't accessible when you run the script. Thi
s is what the error
message say. You might want to search KB for the error message. Or possibly
open a case with MS
support.
Tibor Karaszi, SQL Server MVP
Archive at: http://groups.google.com/groups?oi=...ls
erver
"Brandon Tolleson" <anonymous@.discussions.microsoft.com> wrote in message
news:617901c3e5d7$b9b63000$a401280a@.phx.gbl...
quote:

> I have a script that is basically a cursor that uses
> sp_addrolemember to add all logins on the server to the
> db_datareader role in every database. I use a variable
> for each login. The script is in a job and scheduled to
> run daily. I receive the following error: Msg 913, Sev
> 16: Could not find database ID %. Database may not be
> activated yet or may be in transition. The problem is
> that the EXACT same script will run in Query Analyzer with
> NO errors. Why wouldn't the script give the same error in
> Query Analyzer?
> Thanks.
|||I restarted the server and the job ran successfully. ?
Go figure.
quote:

>--Original Message--
>There doesn't seem to be a problem with the script per

se. I just ran it on my SQL Server and the
quote:

>outcome was success. Did you specify an output file for

the jobstep? This can sometimes give more
quote:

>info about the problem. In my output file, I only had

messages such as:
quote:

>USE master If not exists (select * from sysusers where

SUSER_SNAME(sid) = 'TIBORK\Tibor') Exec
quote:

>sp_grantdbaccess 'TIBORK\Tibor', 'Tibor' [SQLSTATE 01000]
>Granted database access to 'TIBORK\Tibor'. [SQLSTATE

01000]
quote:

>You might want to go though the job in Agent so you

didn't get any unfortunate linebreaks anywhere.
quote:

>Although, the error message you posted didn't indicate

that this is the problem.
quote:

>My guess is that some database isn't accessible when you

run the script. This is what the error
quote:

>message say. You might want to search KB for the error

message. Or possibly open a case with MS
quote:

>support.
>--
>Tibor Karaszi, SQL Server MVP
>Archive at: http://groups.google.com/groups?

oi=djq&as_ugroup=microsoft.public.sqlserver
quote:

>
>"Brandon Tolleson" <anonymous@.discussions.microsoft.com>

wrote in message
quote:

>news:617901c3e5d7$b9b63000$a401280a@.phx.gbl...
with[QUOTE]
in[QUOTE]
>
>.
>

Script Won't Run in Job But Runs in Query Analyzer

I have a script that is basically a cursor that uses
sp_addrolemember to add all logins on the server to the
db_datareader role in every database. I use a variable
for each login. The script is in a job and scheduled to
run daily. I receive the following error: Msg 913, Sev
16: Could not find database ID %. Database may not be
activated yet or may be in transition. The problem is
that the EXACT same script will run in Query Analyzer with
NO errors. Why wouldn't the script give the same error in
Query Analyzer?
Thanks.I would have to see the script in order to (possibly) suggest anything...
:-)
--
Tibor Karaszi, SQL Server MVP
Archive at:
http://groups.google.com/groups?oi=djq&as_ugroup=microsoft.public.sqlserver
"Brandon Tolleson" <anonymous@.discussions.microsoft.com> wrote in message
news:617901c3e5d7$b9b63000$a401280a@.phx.gbl...
> I have a script that is basically a cursor that uses
> sp_addrolemember to add all logins on the server to the
> db_datareader role in every database. I use a variable
> for each login. The script is in a job and scheduled to
> run daily. I receive the following error: Msg 913, Sev
> 16: Could not find database ID %. Database may not be
> activated yet or may be in transition. The problem is
> that the EXACT same script will run in Query Analyzer with
> NO errors. Why wouldn't the script give the same error in
> Query Analyzer?
> Thanks.|||DECLARE @.lname varchar(100)
DECLARE @.uname varchar(100)
Declare @.mystr varchar(1000)
Declare @.dbname varchar(100)
DECLARE MacroVal CURSOR FOR Select name from syslogins
where name like 'MyDomain\
%' order by name
OPEN MacroVal
FETCH NEXT FROM MacroVal INTO @.lname
WHILE(@.@.FETCH_STATUS <> -1)
BEGIN
IF (@.@.FETCH_STATUS <> -2)
BEGIN
Set @.uname = substring(@.lname,charindex('\',@.lname)
+1,50)
DECLARE DBName CURSOR FOR Select name from
sysdatabases order by name
OPEN DBName
FETCH NEXT FROM DBName INTO @.dbname
WHILE(@.@.FETCH_STATUS <> -1)
BEGIN
IF (@.@.FETCH_STATUS <> -2)
BEGIN
Set @.mystr = 'USE '+@.dbname+' If not
exists (select * from sysusers where SUSER_SNAME(sid)
= ''' +@.lname+''') Exec
sp_grantdbaccess '''+@.lname+''', '''+@.uname+''''
if @.uname not like 'student%'
BEGIN
Print (@.mystr)
Exec (@.mystr)
Set @.mystr = 'USE '+@.dbname+' If
exists (select * from sysusers where SUSER_SNAME(sid)
= '''+@.lname+''' and name <> ''dbo'') BEGIN EXEC
sp_addrolemember ''db_datareader'', '''+@.uname+''' EXEC
sp_addrolemember''db_backupoperator'', '''+@.uname+''' END'
Print (@.mystr)
Exec (@.mystr)
END
END
FETCH NEXT FROM DBName INTO @.dbname
END
CLOSE DBName
DEALLOCATE DBName
END
FETCH NEXT FROM MacroVal INTO @.lname
END
CLOSE MacroVal
DEALLOCATE MacroVal
GO
>--Original Message--
>I would have to see the script in order to (possibly)
suggest anything...
>:-)
>--
>Tibor Karaszi, SQL Server MVP
>Archive at:
>http://groups.google.com/groups?
oi=djq&as_ugroup=microsoft.public.sqlserver
>
>"Brandon Tolleson" <anonymous@.discussions.microsoft.com>
wrote in message
>news:617901c3e5d7$b9b63000$a401280a@.phx.gbl...
>> I have a script that is basically a cursor that uses
>> sp_addrolemember to add all logins on the server to the
>> db_datareader role in every database. I use a variable
>> for each login. The script is in a job and scheduled to
>> run daily. I receive the following error: Msg 913, Sev
>> 16: Could not find database ID %. Database may not be
>> activated yet or may be in transition. The problem is
>> that the EXACT same script will run in Query Analyzer
with
>> NO errors. Why wouldn't the script give the same error
in
>> Query Analyzer?
>> Thanks.
>
>.
>|||There doesn't seem to be a problem with the script per se. I just ran it on my SQL Server and the
outcome was success. Did you specify an output file for the jobstep? This can sometimes give more
info about the problem. In my output file, I only had messages such as:
USE master If not exists (select * from sysusers where SUSER_SNAME(sid) = 'TIBORK\Tibor') Exec
sp_grantdbaccess 'TIBORK\Tibor', 'Tibor' [SQLSTATE 01000]
Granted database access to 'TIBORK\Tibor'. [SQLSTATE 01000]
You might want to go though the job in Agent so you didn't get any unfortunate linebreaks anywhere.
Although, the error message you posted didn't indicate that this is the problem.
My guess is that some database isn't accessible when you run the script. This is what the error
message say. You might want to search KB for the error message. Or possibly open a case with MS
support.
--
Tibor Karaszi, SQL Server MVP
Archive at: http://groups.google.com/groups?oi=djq&as_ugroup=microsoft.public.sqlserver
"Brandon Tolleson" <anonymous@.discussions.microsoft.com> wrote in message
news:617901c3e5d7$b9b63000$a401280a@.phx.gbl...
> I have a script that is basically a cursor that uses
> sp_addrolemember to add all logins on the server to the
> db_datareader role in every database. I use a variable
> for each login. The script is in a job and scheduled to
> run daily. I receive the following error: Msg 913, Sev
> 16: Could not find database ID %. Database may not be
> activated yet or may be in transition. The problem is
> that the EXACT same script will run in Query Analyzer with
> NO errors. Why wouldn't the script give the same error in
> Query Analyzer?
> Thanks.|||I restarted the server and the job ran successfully. ?
Go figure.
>--Original Message--
>There doesn't seem to be a problem with the script per
se. I just ran it on my SQL Server and the
>outcome was success. Did you specify an output file for
the jobstep? This can sometimes give more
>info about the problem. In my output file, I only had
messages such as:
>USE master If not exists (select * from sysusers where
SUSER_SNAME(sid) = 'TIBORK\Tibor') Exec
>sp_grantdbaccess 'TIBORK\Tibor', 'Tibor' [SQLSTATE 01000]
>Granted database access to 'TIBORK\Tibor'. [SQLSTATE
01000]
>You might want to go though the job in Agent so you
didn't get any unfortunate linebreaks anywhere.
>Although, the error message you posted didn't indicate
that this is the problem.
>My guess is that some database isn't accessible when you
run the script. This is what the error
>message say. You might want to search KB for the error
message. Or possibly open a case with MS
>support.
>--
>Tibor Karaszi, SQL Server MVP
>Archive at: http://groups.google.com/groups?
oi=djq&as_ugroup=microsoft.public.sqlserver
>
>"Brandon Tolleson" <anonymous@.discussions.microsoft.com>
wrote in message
>news:617901c3e5d7$b9b63000$a401280a@.phx.gbl...
>> I have a script that is basically a cursor that uses
>> sp_addrolemember to add all logins on the server to the
>> db_datareader role in every database. I use a variable
>> for each login. The script is in a job and scheduled to
>> run daily. I receive the following error: Msg 913, Sev
>> 16: Could not find database ID %. Database may not be
>> activated yet or may be in transition. The problem is
>> that the EXACT same script will run in Query Analyzer
with
>> NO errors. Why wouldn't the script give the same error
in
>> Query Analyzer?
>> Thanks.
>
>.
>

Script User Roles

I have created a user role in my SQL 7 database. I would like to copy this
role to 8 other servers. I created the role using Enterprise Manager. Can
you advise how to query this out to SQL Script so that I could just run the
script on the other servers to create the role and then assign users as
required.In Enterprise Manager you can script database and select option to script
roles and permissions also. Then find in the generated script related T-SQL
by role name (starting from sp_addrole and below).
Martin Rakhmanov
"dtatham" <dtatham@.discussions.microsoft.com> wrote in message
news:79CC672A-B46A-4F2C-9CC1-F7DAE8772DB8@.microsoft.com...
>I have created a user role in my SQL 7 database. I would like to copy this
> role to 8 other servers. I created the role using Enterprise Manager.
> Can
> you advise how to query this out to SQL Script so that I could just run
> the
> script on the other servers to create the role and then assign users as
> required.
>

Monday, March 26, 2012

Script to list Users with SA Role on the server

I am wanting to get a list of all the users on a SERVER who has the
System Administrator role assigned to their login. I was wondering if
there is a script or table(s) that I could refer to instead of looking
at all the individual login names. We are using SQL Server 2000.
Any help on this regard will be greatly appreciated.
ThanksYou can use the Stored Procedure sp_helpsrvrolemember to view the members of
a fixed server role.
ie.
EXEC sp_helpsrvrolemember 'sysadmin'
- Peter Ward
WARDY IT Solutions
"shub" wrote:

> I am wanting to get a list of all the users on a SERVER who has the
> System Administrator role assigned to their login. I was wondering if
> there is a script or table(s) that I could refer to instead of looking
> at all the individual login names. We are using SQL Server 2000.
> Any help on this regard will be greatly appreciated.
> Thanks
>

Script to list Users with SA Role on the server

I am wanting to get a list of all the users on a SERVER who has the
System Administrator role assigned to their login. I was wondering if
there is a script or table(s) that I could refer to instead of looking
at all the individual login names. We are using SQL Server 2000.
Any help on this regard will be greatly appreciated.
Thanks
You can use the Stored Procedure sp_helpsrvrolemember to view the members of
a fixed server role.
ie.
EXEC sp_helpsrvrolemember 'sysadmin'
- Peter Ward
WARDY IT Solutions
"shub" wrote:

> I am wanting to get a list of all the users on a SERVER who has the
> System Administrator role assigned to their login. I was wondering if
> there is a script or table(s) that I could refer to instead of looking
> at all the individual login names. We are using SQL Server 2000.
> Any help on this regard will be greatly appreciated.
> Thanks
>

Script to list Users with SA Role on the server

I am wanting to get a list of all the users on a SERVER who has the
System Administrator role assigned to their login. I was wondering if
there is a script or table(s) that I could refer to instead of looking
at all the individual login names. We are using SQL Server 2000.
Any help on this regard will be greatly appreciated.
ThanksYou can use the Stored Procedure sp_helpsrvrolemember to view the members of
a fixed server role.
ie.
EXEC sp_helpsrvrolemember 'sysadmin'
- Peter Ward
WARDY IT Solutions
"shub" wrote:
> I am wanting to get a list of all the users on a SERVER who has the
> System Administrator role assigned to their login. I was wondering if
> there is a script or table(s) that I could refer to instead of looking
> at all the individual login names. We are using SQL Server 2000.
> Any help on this regard will be greatly appreciated.
> Thanks
>

script to grant permision to role

Hi,
is there any script (qucik way) to grant all Select, Update, Insert,
Delete to a role which i call it "WebAccess"
I would like not to spend too much time creating a role called "WebAccess"
and go to that role and check on every Update, Select, Insert, and Delete fo
r
each object
Thanks
EdEd,
Try adding the role "WebAccess" to the fixed databse roles db_datareader and
db_datawriter.
EXEC sp_addrolemember 'db_datareader', 'WebAccess'
EXEC sp_addrolemember 'db_datawriter', 'WebAccess'
AMB
"Ed" wrote:

> Hi,
> is there any script (qucik way) to grant all Select, Update, Insert,
> Delete to a role which i call it "WebAccess"
> I would like not to spend too much time creating a role called "WebAcces
s"
> and go to that role and check on every Update, Select, Insert, and Delete
for
> each object
> Thanks
> Ed|||Do Datareader and DataWrite have rights to Select, Insert, Update, Delete an
d
Exec the stored procedure and UDF?
One more question, do i need to grant permission all stored procedure
starting with dt_xxxx
Thanks again
Ed
"Alejandro Mesa" wrote:
> Ed,
> Try adding the role "WebAccess" to the fixed databse roles db_datareader a
nd
> db_datawriter.
> EXEC sp_addrolemember 'db_datareader', 'WebAccess'
> EXEC sp_addrolemember 'db_datawriter', 'WebAccess'
>
> AMB
>
> "Ed" wrote:
>|||Ed,

> Do Datareader and DataWrite have rights to Select, Insert, Update, Delete
and
> Exec the stored procedure and UDF?
db_datareader - Can select all data from any user table in the database.
db - datawriter - Can modify any data in any user table in the database.
For udfs and sps you have to grant EXECUTE (sp and udf) and REFERENCES (udf)
.
AMB
"Ed" wrote:
> Do Datareader and DataWrite have rights to Select, Insert, Update, Delete
and
> Exec the stored procedure and UDF?
> One more question, do i need to grant permission all stored procedure
> starting with dt_xxxx
> Thanks again
> Ed
>
> "Alejandro Mesa" wrote:
>|||is that necessary to grant permission to stored procedure starting with dt_x
xxx
Ed
"Alejandro Mesa" wrote:
> Ed,
>
> db_datareader - Can select all data from any user table in the database.
> db - datawriter - Can modify any data in any user table in the database.
> For udfs and sps you have to grant EXECUTE (sp and udf) and REFERENCES (ud
f) .
>
> AMB
> "Ed" wrote:
>|||No.
AMB
"Ed" wrote:
> is that necessary to grant permission to stored procedure starting with dt
_xxxx
> Ed
> "Alejandro Mesa" wrote:
>

Friday, March 23, 2012

Script to copy permissions for all objects given to a user or a role

How would I, using a sql script, copy permissions assigned to a user
or a role in one or more databases to another user or a role in their
respective databases?

Help appreciatedgudia97@.yahoo.com (gudia) wrote in message news:<2130f7ff.0407071438.275b76f3@.posting.google.com>...
> How would I, using a sql script, copy permissions assigned to a user
> or a role in one or more databases to another user or a role in their
> respective databases?
> Help appreciated

First of all, I would avoid granting permissions to users, since this
becomes difficult to manage - if you always use roles, then things are
much easier. Even if you have only one user in a role today, at least
you won't have any extra work when you need to add a second one. Also,
you should keep a permissions script for each role, so you know which
permissions are correct, and you can run it for multiple roles.

To solve your issue right now, you could add one role to another, if
that's appropriate. If not, then you can reverse-engineer a
permissions script using a query like this:

select
case protecttype
when 205 then 'grant '
when 206 then 'revoke '
end +
case action
when 26 then ' references '
when 193 then ' select '
when 195 then ' insert '
when 196 then ' delete '
when 197 then ' update '
when 224 then ' execute '
end +
' on ' + object_name(id) + ' to TargetRole '
+ case when protecttype = 204 then ' with grant option' else '' end
from
sysprotects
where
uid = user_id('SourceRole')

See sysprotects in Books Online for more information. Note that this
query doesn't handle SELECT or UPDATE permissions on explicit columns,
but only where the permissions are on the whole table. It also does
not handle statement permissions (CREATE TABLE etc.), but you can
easily write a similar query for that.

Simon|||Simon:

Thanks. This will help as a starting point. I do use roles as oppossed
to users for permissioning.

*** Sent via Devdex http://www.devdex.com ***
Don't just participate in USENET...get rewarded for it!