Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
Discussion Groups
DB Engine
SQL ServerMSDESQL Server CE
Services
Analysis (Data Mining)Analysis (OLAP)DTSIntegration ServicesNotification ServicesReporting Services
Programming
CLRConnectivitySQLXML
Other Technologies
ClusteringEnglish QueryFull-Text SearchReplicationService Broker
General
Data WarehousingPerformanceSecuritySetupSQL Server ToolsOther SQL Server Topics
DirectoryUser Groups
Related Topics
MS AccessOther DB ProductsMS Server Products.NET DevelopmentVB DevelopmentJava DevelopmentMore Topics ...

SQL Server Forum / DB Engine / SQL Server / July 2008

Tip: Looking for answers? Try searching our database.

Problem with the cursor and dbname

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
tolcis - 02 Jul 2008 17:12 GMT
Hi,
I have the following  cursor is running on my server and revoking
access for all users.

However, two of my databases are named: MY Mas and MY San. There is a
space in the name (don't ask - it was named before I got here).  We
can't change the name of the database but when I run the code below it
give me the error message:

Could not locate entry in sysdatabases for database 'MY'.  The code
never takes the full db name like MY Mas  - it only takes the first
part (MY).  How can I modify the code below so it takes the above two
databases and runs the cursor on them as well?

Thank you,

DECLARE @dbname varchar(30)
declare @username varchar (100)
DECLARE @viewactionmsg varchar (1000)
DECLARE dbnames_cursor CURSOR FOR SELECT name
FROM master..sysdatabases where name not IN
('tempdb','model')

OPEN dbnames_cursor
FETCH NEXT FROM dbnames_cursor into @dbname
/* Do all databases, checking for 'EOF' on Cursor. */
WHILE (@@FETCH_STATUS <> -1)
BEGIN

execute ('DECLARE drop_users CURSOR FOR
    SELECT u.[name] from ['+ @dbname + ']..sysusers u ' + 'where u.sid is
not NULL and u.[name] not in (''dbo'', ''sa'',''guest'')')

    OPEN drop_users

    FETCH NEXT FROM drop_users INTO @username

    WHILE (@@FETCH_STATUS <> -1)
    BEGIN
    IF (@@FETCH_STATUS <> -2)
    Begin
        --PRINT @username
        SELECT @viewactionmsg = 'USE '+ @dbname + ' EXEC sp_revokedbaccess
[' + @username +']'
        exec (@viewactionmsg)
    End

    FETCH NEXT FROM drop_users
    INTO @username
    End  --

--    CLOSE drop_users
    DEALLOCATE drop_users
Fetch next from dbnames_cursor into @dbname
end
--CLOSE dbnames_cursor
DEALLOCATE dbnames_cursor
go
Alex Kuznetsov - 02 Jul 2008 17:24 GMT
> Hi,
> I have the following  cursor is running on my server and revoking
[quoted text clipped - 54 lines]
> DEALLOCATE dbnames_cursor
> go

use square brackets:

               SELECT @viewactionmsg = 'USE ['+ @dbname + '] EXEC
sp_revokedbaccess
tolcis - 02 Jul 2008 17:32 GMT
> > Hi,
> > I have the following  cursor is running on my server and revoking
[quoted text clipped - 59 lines]
>                 SELECT @viewactionmsg = 'USE ['+ @dbname + '] EXEC
> sp_revokedbaccess

Thank you, that did it.
Plamen Ratchev - 02 Jul 2008 17:33 GMT
You can use QUOTENAME:

SELECT @viewactionmsg = 'USE '+ QUOTENAME(@dbname) + ...

HTH,

Plamen Ratchev
http://www.SQLStudio.com
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2009 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.