Nested quotes help

  • how can i get the [ ] characters around the username? nested quotes is probably my least favorite thing right now. i'm trying to generate output that I can cut/paste and run.

    dropping all orphaned users on a server:

    DECLARE @command varchar(1000)

    SELECT @command = 'USE [?]; select ''DROP USER ''' + ', u.name from master..syslogins l right join sysusers u on l.sid = u.sid where l.sid is null and issqlrole <> 1 and isapprole <> 1 and (u.name <> ''INFORMATION_SCHEMA'' and u.name <> ''guest'' and u.name <> ''system_function_schema'' and u.name <> ''sys'' and u.name <> ''dbo'')'

    EXEC sp_MSforeachdb @command

    thanks!

  • additionally, getting rid of the column name and --------- in the output so that i only have the drop statements would be a bonus.

  • This can get you a copy-paste-exec code. Be careful with it.

    DECLARE @command varchar(1000) ;

    SELECT @command = 'SELECT ''USE [?]'';

    select ''DROP USER '' + QUOTENAME( u.name)

    from master..syslogins l

    right join [?]..sysusers u on l.sid = u.sid

    where l.sid is null

    and issqlrole <> 1

    and isapprole <> 1

    and u.name <> ''INFORMATION_SCHEMA''

    and u.name <> ''guest''

    and u.name <> ''system_function_schema''

    and u.name <> ''sys''

    and u.name <> ''dbo'''

    EXEC sp_MSforeachdb @command;

    Be careful with it.

    Luis C.
    General Disclaimer:
    Are you seriously taking the advice and code from someone from the internet without testing it? Do you at least understand it? Or can it easily kill your server?

    How to post data/code on a forum to get the best help: Option 1 / Option 2
  • Check out SET QUOTED_IDENTIFIER OFF. Now you no longer have to worry about how dang many single quotes you need to get your dynamic sql to be parsable! 😎

    Best,
    Kevin G. Boles
    SQL Server Consultant
    SQL MVP 2007-2012
    TheSQLGuru on googles mail service

  • sweet! thanks guys.

Viewing 5 posts - 1 through 4 (of 4 total)

You must be logged in to reply to this topic. Login to reply