0

I am using sql server R2. I have a stored procedure in which I am passing two paramaters as @username varchar(100) and @password varchar(100).

Now, when I create user from my application first time, the password will be NULL.

I want to make a query which return record for matching username and password. The problem is code is passing empty string to stored procedure. I want something that consider or convert empty string to NULL automatically. I already have solution for this using if condition but I want to use single query, not if condition.

EDIT :

Its a simple stored procedure :

CREATE PROCEDURE [dbo].[Pro_TblP]
(
 @userName varchar(100),
 @password varchar(100) 
)
AS
BEGIN
    IF (@password ='')
        BEGIN
            SELECT * FROM TblP 
            WHERE username = @userName AND password IS NULL AND IsDeleted = 0
        END

    ELSE 
        BEGIN
            SELECT * FROM TblP 
            WHERE username = @userName AND password = @password AND IsDeleted = 0
    END
END

GO

I want to combine the query in single representation. Don't want the if condition.

1
  • We can help if you show us your code. What does it look like? Commented Mar 11, 2013 at 5:08

4 Answers 4

1
CREATE PROCEDURE [dbo].[Pro_TblP]
(
 @userName varchar(100),
 @password varchar(100) 
)
AS
BEGIN
   SELECT * FROM TblP 
   WHERE username = @userName AND COALESCE(password,'') = @password AND IsDeleted = 0

END

GO

SQL Fiddle

Sign up to request clarification or add additional context in comments.

1 Comment

@Dev : Check Fiddle and tell me where are you facing the problem?
0

Try this

alter PROCEDURE [dbo].[Pro_TblP]
(
 @userName varchar(100),
 @password varchar(100)='' 
)
AS
BEGIN
   BEGIN
      SELECT * FROM TblP 
      WHERE username = @userName AND IsNull(password,'')=@password AND IsDeleted = 0
   END
END

2 Comments

Where are you comparing password with parameter @password in above stored procedure you have given?
@Dev: Updated it, was a typo when copy from my query analyser
0

Try below code : when @password is empty string OR null it will return all values. when you pass a string in @password variable it will then filter it.

CREATE PROCEDURE [dbo].[Pro_TblP]
(
 @userName varchar(100),
 @password varchar(100) 
)
AS
BEGIN
     SELECT * FROM TblP 
         WHERE username = @userName AND IsDeleted = 0
         And (password = @password OR  isnull(@password,'') = '')           

END

Comments

0
SELECT *
FROM TblP 
WHERE username = @userName AND 
      IsDeleted = 0 AND
      (
        password = @password OR
        password IS NULL AND @password = ''
      )

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.