correct sytax for sp_msforeachdb SQL Server 2005

Posted on 2011-04-27
Last Modified: 2013-11-05
I am trying to use the sp_msforeachdb stored procedure to execute the following commands and I cannot get the sysntax correct.  can anyone help?

When I issue this:
sp_msforeachdb 'EXEC dbo.sp_changedbowner @loginame = N'DOMAIN\DomainUser', @map = false'

Open in new window

I get this:
Msg 102, Level 15, State 1, Line 1
Incorrect syntax near 'DOMAIN'.

and when I issue this:
sp_msforeachdb 'EXEC sp_change_users_login 'Auto_fix', 'DOMAIN\DomainUser''

Open in new window

i get this:
Msg 102, Level 15, State 1, Line 1
Incorrect syntax near 'Auto_fix'.
Question by:itsonlyme4
    LVL 6

    Accepted Solution

    you need to escape your quotes
    and use the database wildcard to have the query run in the right environment
    issue this:

     sp_msforeachdb 'EXEC ?.dbo.sp_change_users_login ''Auto_fix'', ''DOMAIN\DomainUser'''

    sp_msforeachdb 'EXEC ?.dbo.sp_changedbowner @loginame = N''DOMAIN\DomainUser'', @map = false'

    Author Comment

    Awesome!  Thank you!

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    How to run any project with ease

    Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
    - Combine task lists, docs, spreadsheets, and chat in one
    - View and edit from mobile/offline
    - Cut down on emails

    by Mark Wills Attending one of Rob Farley's seminars the other day, I heard the phrase "The Accidental DBA" and fell in love with it. It got me thinking about the plight of the newcomer to SQL Server...  So if you are the accidental DBA, or, simp…
    Use this article to create a batch file to backup a Microsoft SQL Server database to a Windows folder.  The folder can be on the local hard drive or on a network share.  This batch file will query the SQL server to get the current date & time and wi…
    Migrating to Microsoft Office 365 is becoming increasingly popular for organizations both large and small. If you have made the leap to Microsoft’s cloud platform, you know that you will need to create a corporate email signature for your Office 365…
    Hi everyone! This is Experts Exchange customer support.  This quick video will show you how to change your primary email address.  If you have any questions, then please Write a Comment below!

    761 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    8 Experts available now in Live!

    Get 1:1 Help Now