SQL query to retrieve data based on either emailid or userid - sql

I have to write a query for a login page where the user can enter either emailid(varchar datatype) or userid(int datatype). How do I write a query for this without knowing the datatype of the input?

CREATE PROCEDURE my_stored_procedure
#emailId nvarchar(50) = NULL,
#userId int = NULL
AS
SET NOCOUNT ON;
IF (#emailId IS NOT NULL) AND (LEN(#emailId) > 0)
SELECT * FROM MYTABLE WHERE EmailId = #emailId
ELSE
SELECT * FROM MYTABLE WHERE UserId = #UserId
GO

Your input parameter should be of varchar type.
Then in your procedure use ISNUMERIC function to check for number. Have two queries based on that with IF statement in case if it is int or varchar.
DECLARE #userInput VARCHAR(50)
IF ISNUMERIC(#userInput) = 1
BEGIN
-- Your query with userid
END
ELSE
BEGIN
-- Your query with emailid
END

Try this query it may help you.
DECLARE #emailid varchar(250),#userid bigint
set #emailid='A'
SET #userid=3
select * from yourtable
where case when #emailid='A' THEN 'A' ELSE emailid END=#emailid
AND case when #userid=0 THEN 0 ELSE userid END=#userid
Note: You should pass 'A' as default value for email_id and 0 for userid or else you can pass your required input

Related

Finding MAX value of data type Varchar

Firstly I have a table tblSample with ID as column of datatype INT. To auto generate ID for every transaction, I created a stored procedure:
DECLARE #Id INT
SELECT #Id = MAX(Id)
FROM tblSample
IF #Id IS NULL
BEGIN
SELECT 0 as Id
END
ELSE
SELECT MAX(Id) as ID FROM tblSample
Here as you observe if ID has no rows MAX(Id)=0 then return 0 or else return MAX(ID) value so that next insertion will be greater than max(ID).
That's fine but now I had column ID with datatype VARCHAR I have to do similar operation how can I that?
The code looks fine so it should work with VARCHAR also but my suggestion is to use storage variable also with same datatype so it won't get conflicted anywhere in the operation:
DECLARE #Id VARCHAR(10)
I think you could use MAX(CAST(varcharcolumn AS Int))
DECLARE #Id INT
SELECT #Id=MAX(Id) FROM tblSample
IF #Id IS NULL
BEGIN
SELECT 'Your_VarCharValue' + CAST(0 AS VARCHAR) as Id
END
ELSE
SELECT 'Your_VarCharValue' + CAST(MAX(Id) AS VARCHAR) as ID FROM tblSample

How to condition my select based on boolean parameter

I'm creating a stored procedure that should return a list of users based on a boolean flag #status as follows:
If status is not informed, the query must return all users
If 'status' is passed as true, the query must return only users where the column LastAccess is not null.
If status is passed as false, the query must return users which column LastAccess is null.
This column is of type datetime.
I'm ok dealing the first use case, with #status is null in the last line of the following code:
CREATE PROCEDURE selectUsersByStatus (
#userId int,
#status bit
)
AS
SELECT * from users
WHERE users.id = #userId
and (#status is null or CASE #status WHEN 1 THEN (users.LastAccess is not null) ELSE (users.LastAccess is null))
However the rest of the line obviously doesn't work. How to proceed?
Thanks
Try simple:
CREATE PROCEDURE selectUsersByStatus (
#userId int,
#status bit
)
AS
SELECT * from users
WHERE users.id = #userId
and (#status is null OR (#status = 1 AND users.LastAccess IS NOT NULL) OR (#status = 0 AND users.LastAccess IS NULL))

IF ELSE condition in SQL select

I want to do a if-else condition statement in SQL Server but am not sure how.
Inside the stored procedure I have the following parameters:
#MarketId nvarchar (10),
#RegionId nvarchar (10)
And the following statement:
select * from Interaction I
where
(#MarketId = 0 ) OR (I.MarketId = (SELECT Id FROM Market WHERE ExternalId = #MarketId))
What I want to do is to check the value of #MarketId
if #MarketId = 0
then I want the where condition for I.MarketId to get its Ids from elsewhere like
(SELECT ID FROM Market WHERE ExternalId = #RegionId)
otherwise, if its 1, then I just want to leave it as is and get the Id from #MarketId instead of #RegionId..
How should I go about this?
Thanks!
This should work:
SELECT *
FROM Interaction I
WHERE ( #MarketID = 0
AND EXISTS (SELECT 1 FROM Market
WHERE ExternalId = #RegionId AND Id = I.MarketID)
OR I.MarketID = #MarketID

SQL Server - check input parameter for null or zero

I have a stored procedure for sql server 2008 like this:
create procedure test_proc
#someval int,
#id int
as
update some_table
set some_column = ISNULL(#someval, some_column)
where id = #id
go
If the parameter #someval is NULL, this SP will just use the existing value in some_column.
Now I want to change this behaviour such that if value for #someval is 0, a NULL is stored in some_column otherwise it behave just the way it is doing now.
So I am looking for something like:
if #someval == 0
set some_column = NULL
else
set some_column = ISNULL(#someval, some_column)
I don't have the option to create a varchar #sql variable and call sq_executesql on it (at least that is the last thing I want to do). Any suggestions on how to go about doing this?
You can do this using the CASE expression. Something like this:
update some_table
set some_column = CASE WHEN #someval = 0 THEN NULL
WHEN #someval IS NULL THEN somcolumn
ELSE #someval -- the default is null if you didn't
-- specified one
END
where id = #id
something like this?
create procedure test_proc
#someval int,
#id int
as
update some_table
set some_column = CASE
WHEN #someval = 0 THEN NULL
ELSE ISNULL(#someval, some_column) END
where id = #id
go
I think it's a really bad idea - I'd suggest that if someone wants to store a NULL, they really shouldn't have to pass some other magical value to cause it to happen. However, let's show how it can be done:
update some_table
set some_column = CASE WHEN #someVal = 0 THEN NULL ELSE ISNULL(#someval, some_column) END
where id = #id
Given the simplicity of the stored procedure in your question, of course, the whole matter can be cleared up by not calling the stored procedure if you don't want to alter some_column. I'd imagine that your real procedure is more complex. Instead, what I'd do is have:
create procedure test_proc
#someval int,
#someval_specified bit,
#id int
as
update some_table
set some_column = CASE WHEN #someval_specified = 1 THEN #someval ELSE some_column END
where id = #id
And now NULL means NULL, 0 means 0, etc.

Get a parameter from one select clause instead of two

I want to get a parameter from a select clause in one time, instead of two selects.
Currently, I'm doing this :
SELECT
Idx_Pro,
Name,
Mail,
IsValidateUser
FROM
MyTable
WHERE
[Mail] = #Mail
AND
[Password] = #password
If (##ROWCOUNT > 0)
SET #Id_output = (SELECT Id_User
FROM MyTable
WHERE [Mail] = #Mail
AND [Password] = #password
)
I tried this :
DECLARE #Id_output int
SELECT
[#Id_output] = Idx_Pro,
...
FROM MyTable
...
But I can't get it ...
Is it even possible to get only one column in a variable ? (My select returns only one row)
thanks,
TonyFlow
Are you looking to both return the 4 column result and assign to the variable in one operation? If so that isn't possible. You could assign all the column values to variables or parameters then select those to return the result set though.
DECLARE #Idx_Pro INT,
#Name VARCHAR(50),
#IsValidateUser BIT
SELECT #Idx_Pro = Idx_Pro,
#Name = Name,
#IsValidateUser = IsValidateUser
FROM MyTable
WHERE [Mail] = #Mail
AND [Password] = #password
/*No intermediate statement can be placed here as it will reset ##ROWCOUNT*/
SELECT #Idx_Pro AS Idx_Pro,
#Name AS Name,
#Mail AS Mail,
#IsValidateUser AS IsValidateUser
WHERE ##ROWCOUNT > 0