To create a stored procedure

  • I have a Table which have two columns 1. UserName 2.Password.

    Now I need to create stored procedure with two parameters 1.@username nvarchar(255) 2. Password nvarchar(255)

    When I execute the stored procedures. If userId and password match then it should print login successful and if it doesnot match it should print login unsucessfull.

    Please help me out.

    Thanks in advance.

  • That's pretty basic. What have you tried? What problem are you facing?

    If this isn't homework, I'd be really concerned if you're handling passwords on plain text.

    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
  • create procedure spgetuserdetails

    (

    @userid nvarchar(255),

    @Password nvarchar(255)

    )

    as

    begin

    declare @count int

    select @count = count(*) from UsersTable where UserId = @userid and [Password] = @Password

    if(@count = 0)

    begin

    Print 'Not Successful'

    end

    else

    begin

    print 'Login successfull'

    end

    end

    I tried this. Is this right?

  • swarun999 (12/5/2016)


    create procedure spgetuserdetails

    (

    @userid nvarchar(255),

    @Password nvarchar(255)

    )

    as

    begin

    declare @count int

    select @count = count(*) from UsersTable where UserId = @userid and [Password] = @Password

    if(@count = 0)

    begin

    Print 'Not Successful'

    end

    else

    begin

    print 'Login successfull'

    end

    end

    I tried this. Is this right?

    First note the comments about storing passwords in plain text. Don't. So I am assuming that this is not a real-world example.

    Second, successful has only one 'l'.

    Third, a more efficient way of achieving your logic uses EXISTS:

    IF NOT EXISTS

    (

    SELECT 1

    FROM dbo.UsersTable

    WHERE

    UserId = @userid AND

    Password = @Password

    )

    BEGIN

    PRINT 'Not Successful';

    END;

    ELSE

    BEGIN

    PRINT 'Login Successful';

    END;

    The absence of evidence is not evidence of absence
    - Martin Rees
    The absence of consumable DDL, sample data and desired results is, however, evidence of the absence of my response
    - Phil Parkin

  • swarun999 (12/5/2016)


    create procedure spgetuserdetails

    (

    @userid nvarchar(255),

    @Password nvarchar(255)

    )

    as

    begin

    declare @count int

    select @count = count(*) from UsersTable where UserId = @userid and [Password] = @Password

    if(@count = 0)

    begin

    Print 'Not Successful'

    end

    else

    begin

    print 'Login successfull'

    end

    end

    I tried this. Is this right?

    You tried it or you wrote it? If you try it, you'll find out if it's right or not.

    I suggest that you try Phil's option. You won't need a variable and the query is less expensive.

    Remember, ALWAYS test.

    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
  • Yes, I will do. Thanks for your help

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

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