SQL select statement with WHERE clause based on name - sql

I have a table TBL_TEST which has a column NAME with 5 rows, and those rows have the values 'Balkhab', 'Gosfandi', 'Sancharak', 'Aqcha'
I want to write a query with a WHERE clause that when the value in where class match with one of the values in NAME column in TBL_TEST then it should show only that value but when it does not match then it should show all the values in the table, below is select statement that is not working as expect.
SELECT NAME
FROM TBL_TEST
WHERE NAME = 'Balkhab' OR NAME != 'Balkhab'

I suspect you want something like this:
SELECT NAME
FROM TBL_TEST
WHERE NAME = 'Balkhab'
UNION ALL
SELECT NAME
FROM TBL_TEST
WHERE NOT EXISTS (SELECT 1 FROM TBL_TEST WHERE NAME = 'Balkhab');

IF Exists(select 'X' FROM TBL_TEST WHERE NAME = 'Balkhab')
SELECT NAME from TBL_TEST WHERE NAME = 'Balkhab'
ELSE
SELECT NAME FROM TBL_TEST

You can try this one below
select *
from table where not exists (select name from table where name = 'Aqcha') or
name ='Aqcha'

You can write a stored procedure for it with one parameter.
Create procedure(#Name varchar(max))
As
begin
If(Isnull(#name, 0))
Begin
If exists (select 1 from TBL_TEST xx where xx.name = #name)
Begin
Select *
From TBL_TEST
Where name = #name
End
Else
Begin
Select *
From TBL_TEST
End
End
Else
Begin
Select * from TBL_TEST
End
End
Pass your name as parameter to the stored procedure to get require result
stored procedure are the best option as you can avoid network traffic, less bandwidth as you can pass only require parameter and get required result

Related

Automation Anywhere SQL results

I am trying to capture if my SQL Query have 0 rows or multiple rows. If it has 0 rows then I will insert, if 1 will perform an update, if > 1 will perform additional analysis.
Is there a way I can see if my query resulted in x results or no results in automation anywhere?
Any assistance will be appreciated.
You can make use of if exists and if not exists and check if rows exists or not, or even if there are multiple before doing the insert.
Here is a simple example using if not exists where if the row doesn't exist on dbo.Table it will insert a row. If it already exists then the ID will be logged to an Error table.
declare #InsertID int = 5, #Name nvarchar(max) = 'some name'
if ((select count(1) from dbo.Table where ID = #InsertID) > 1) -- detect error; more than one record for an id
begin
insert into dbo.Error (ErrorID, ErrorDate)
select #InsertID, getdate()
end
else if not exists (select 1 from dbo.Table where ID = #InsertID) -- no record exists for ID, insert it
begin
insert into dbo.Table (ID, Name)
select #InsertID, #Name
else if exists (select 1 from dbo.Table where ID = #InsertID) -- update the single record
begin
update dbo.Table set Name = #Name where ID = #InsertID
end
A2019 returns the results of a SQL Query as a table...
You could have an if statement right after your query which checks to see if the row count of the returned table is > 0 then take action accordingly.

Conditional SQL statement - conditioning on column to be returned

I'm trying to make a conditional select statement, and the parameter I'm conditioning on is the column I want to return. Meaning - if something about a column in a table is true, I want to return that column; if not, I want to return the other column.
Ex:
IF table.obj_param_A = ?
BEGIN
SELECT obj_param_A FROM table
END
ELSE
BEGIN
SELECT obj_param_B FROM table
END
I know this doesn't work, but it's the general idea of what I want. Any idea how to structure this properly?
If you do not concern the column name returned, you can use CASE
SELECT
CASE
WHEN obj_param_A = ? THEN obj_param_A
ELSE obj_param_B
END AS value
FROM table
Below is an example of how you can use the table information about a specific column in order to select from specific columns.
Drop Table dbo.Test
Create Table dbo.Test (iVersion int, iVersion2 smallmoney)
Insert Into dbo.Test
Select 1, 1.12
Declare #Test as nvarchar(200)
Select #Test = Column_Name from information_schema.columns where Table_Schema = 'dbo' and table_name = 'test' and Ordinal_Position = 1
--IF (#Test = 'iVersion') --Equal To iVersion
IF (#Test != 'iVersion') ---Not Equal To iVersion
BEGIN
SELECT iVersion FROM dbo.Test
END
ELSE
BEGIN
SELECT iVersion2 FROM dbo.Test
END
This should return the data from iVersion2 since the Name of the First column is "iVersion" and the If Statement returns iVersion only if it is not the first column

How to use case statement inside an SQL select Query

I need to get the output of a selected query depending on certain conditions
Means if(id=uid)
then I need the below query
select * from table1 where id=5;
else
I need the below one
select * from table1 where id=10
I know i can use if condition for this. But my query is very long one so when I use if else then it would look like
if(#id=#uid)
begin
select * from table1 where id=5;// query 1
end
else
select * from table1 where id=10;//query 2
but here I need to replace the entire query once again for a single check. I hope I can do something like this:
declare #id int=4;
declare #uid=10;
select * from table1 where
case
when #id=#uid
then
id=5
else
id=10;
end
Updation
I need one more condition too
in this case id=5 and uid=10
then if(id=uid)
then
select * from table1 where id=5
and
if(id!=uid)
then
select * from table1
something like this
You can use the case expression to return the value id should be equal to:
SELECT *
FROM table1
WHERE id = CASE WHEN #id = #uid THEN 5 ELSE 10 END;
EDIT:
The updated requirement in the question is to return all rows when #id != #uid. This can be done by comparing id to id:
SELECT *
FROM table1
WHERE id = CASE WHEN #id = #uid THEN 5 ELSE id END;
Alternatively, with this updated requirement, a simple or expression might be simpler to use:
SELECT *
FROM table1
WHERE #id = #uid OR id = 5;
SELECT * FROM table1
WHERE (id=5 AND #id=#uid) OR (id=10 AND #id<>#uid)
SELECT
*
FROM
table1
WHERE
(
#id = #uid
AND
id =5
)
OR
(
not #id = #uid
AND
id=10
)

Give values from sub select and re-enter back in query

My dbo.Report table has a column called Name. I need to somehow select the Name column in my sub select. How can I get the Name values from the sub select as well? Once I have those I need to be able to run another select query such as this:
SELECT * FROM MyOtherTable WHERE Name = #pName
where #pName being a newly created variable with values from the sub-select possibly?? I'm not sure how that works. Or something like this:
SELECT * FROM MyOtherTable WHERE Name IN (the values from my sub select go here)
PROC:
SELECT ListingInfo,
COALESCE((SELECT SUM(ListingViews)
FROM dbo.Report
WHERE (ID = #pID)
AND (DateEntered BETWEEN DATEADD(MONTH, 0, #pFromDate) AND DATEADD(MONTH, 1, #pToDate)-1)
GROUP BY ID), 0) AS 'Views'
FROM dbo.Reporting r
INNER JOIN dbo.Listings l ON (r.ID = l.ID)
WHERE (r.ID = #pID)
AND l.TypeCode = 20
You can use a table variable to store your names.
DECLARE #Names TABLE
(
Name varchar(250),
SomeInt int
)
INSERT INTO #Names (Name, SomeInt)
SELECT Name, sum(ListingViews)
FROM WhateverTable
GROUP BY Name
SELECT * FROM OtherTable WHERE Name IN (SELECT Name FROM #Names)
You can go on to use #Names as if it were any other table, and if you use it in a stored procedure, it will automatically handle the clean up of the table variable when it ends.
Something like this?
Or what else do you need?
Didn't understand it at all, sorry.
SELECT *
FROM MyOtherTable
WHERE Name IN (SELECT name FROM Table WHERE Name = #pName)
Edit:
Something like this?
SELECT COUNT(*), name
FROM MyOtherTable O, Table T
WHERE O.Name = T.NAME
AND T.NAME = #pName
group by name

IF condition in view in SQL Server

Is it possible to have a if condition in VIEWS
eg
CREATE VIEW
as
DECLARE #Count int
SET #Count=-1
select #Count=EmpID from EmployeeDetails where ID=200
IF #Count=-1
BEGIN
SELECT * FROM TEAM1
END
ELSE
BEGIN
SELECT * FROM TEAM1
END
You could try something sneaky with a UNION :
SELECT {fieldlist}
FROM Table1
WHERE EXISTS(SELECT EmpID FROM EmployeeDetails WHERE ID = 200)
UNION ALL
SELECT {fieldlist}
FROM Table2
WHERE NOT EXISTS(SELECT EmpID FROM EmployeeDetails WHERE ID = 200)
This method would require both SELECT statements to return the same set of fields, although their sources might be different.
Views only allow select statements as stated in here
if you need to do if on column values you can use a
SELECT
CASE WHEN COLUMN1 = 1 THEN COLUMNX ELSE COLUMNY END
FROM TABLE1
if your need exceeds this you should create a select from a table valued function instead of a view.
What you need is a simple Procedure
CREATE PROCEDURE DOSOMETHING
(
#ID INT
)
AS
BEGIN
IF #ID > 100
SELECT 1 AS ID,'ME' AS NAME, GETDATE() AS VARIABLEDATECOL, NEWID() AS VARIABLEGUID
ELSE
SELECT 2 AS ID, 'YOU' AS NAME
END
No I don't believe this is possible.
You could use a stored procedure instead to achieve this functionality.
simply use a udf (User defined Function)
Here you can use IF, ELSE, WHILE etc.
But when you are manipulating data (INSERT, UPDATE, DELETE) then you have to use Stored Procedures because udf's aren't able to do that