sp_grantlogin Error Hanlde When NT username not valid

Posted on 2004-11-27
Last Modified: 2008-02-01
If I'm creating (sp) that Adding NT user to the Data base I should do like this:

exec sp_grantlogin 'Servername\username'

But suppose the user name is not inside the NT security names then the following error will be displayed :

MSG 15401 : Windows NT user or group 'Servername\username' not found check the name again.

How I can handle this error when The User name is not found in the NT security users , I mean if I do like this :

exec sp_grantlogin 'Servername\username'
if @@error <> 0

I did it like this but it didn't works , any body can help me how to handle it?
Question by:mubama0n
    1 Comment
    LVL 16

    Accepted Solution

    sp_grantlogin returns 0 for success or 1 for failure. You could try this:

    DECLARE @rc int

    EXEC @rc = sp_grantlogin 'Servername\username'
    IF @rc = 0 -- success
    ELSE -- failure

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Threat Intelligence Starter Resources

    Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

    In this article—a derivative of my blog post (—I will explore a few different perspectives on which week today's date falls within using Microsoft SQL Server. First, to frame this stu…
    Slowly Changing Dimension Transformation component in data task flow is very useful for us to manage and control how data changes in SSIS.
    Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
    Via a live example, show how to extract information from SQL Server on Database, Connection and Server properties

    737 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

    19 Experts available now in Live!

    Get 1:1 Help Now