I am having a scenerio like i am having two table with the name tbl1 and tb2
Select * from tbl1 t1 inner join tb2 t2 on t1.Id = t2.id
The above sample query here i need to achive is that i need to perform this inner join based on the condition. (i.e) I will pass input parameter IsJoin as true means, i can perform inner join else not. I am not preferring dynamic query. Kindly let me know if there is any other way to achieve this.
Thanks in advance
There's just one way to do that then - using IF and ELSE statements
IF <YOUR CONDITION>
BEGIN
SELECT *
FROM tbl1 t1
INNER JOIN tb2 t2
ON t1.Id = t2.id;
END
ELSE
BEGIN
SELECT *
FROM tbl1 t1;
END
If it's just few conditions, then it's fine to do this using multiple statements, however if it's tons of joins based on conditions - it's easier to maintain Dynamic SQL. And it becomes much more readable.
Try this way:
declare #IsJoin numeric(1,0)
select #IsJoin = 1
Select * from tbl1 t1
left join tb2 t2 on t1.Id = t2.id
and #IsJoin = 1
If you need to preserve all columns of t1 and t2 whether join or not
DECLARE #IsJoin bit = 1 -- 1 for true; 0 for false
SELECT
*
FROM
tbl1 t1
LEFT OUTER JOIN tbl2 t2 on t1.Id = t2.id and #IsJoin = 1
WHERE
(#IsJoin = 0 OR t2.id is not null) -- Keep the inner join behavior
Related
I have an sql update statement:
update table1 set col1='val' where id in (select t2.id
from table2 t2
inner join table1 t1 on t1.id = t2.id
where t1.col2='val2' and t2.col3='val3');
Is there a better way to write it? I am thinking somehow not to use join since I have the table in the update construction.
Maybe:
update table1 set col1='val' where id in (select t2.id
from table2 t2
where t2.col3='val3')
and t1.col2='val2';
Is it better the last query?
Maybe with exists() ?
UPDATE table1 s
SET s.col1='val'
WHERE EXISTS(SELECT 1 FROM table2 t
WHERE s.id = t.id AND someCondition)
Initially, I have a query like below, doing a join on 1=1. (It's simply doing a cross join, which selects all rows from the first table and all rows from the second table and shows as a cartesian product, i.e. with all possibilities.)
SELECT * FROM Table1 t1
JOIN Table2 t2 ON 1=1
Problem: Optimize this query in such a way, it will show only the records for a particular ID and if we don't have an ID or have a NULL in the ID then it will show the result same as previously(1=1). So I wrote the script below.
Declare #T2id as int;
Set #T2id = 123;
SELECT * FROM Table1 t1
JOIN Table2 t2 ON
-- left side of join on statement
CASE
WHEN #T2id Is NULL
THEN 1
ELSE
t2.Id
END
=
-- right side of join on statement
CASE
WHEN #T2id Is NULL
THEN 1
ELSE
#T2id
END
Can anyone confirm, is it good or we can have a better approach than this?
I think your way of presenting a cross-join is something I haven't seen before.
My view is it's simpler to read and understand if you just:
SELECT *
FROM Table1 t1, Table2 t2
As for the question, assuming SQL Server (you didn't tag the RDBMS, but I guess from your variable declaration) you might consider:
IF ISNULL(#T2id,1) = 1
SELECT *
FROM Table1 t1, Table2 t2;
ELSE
SELECT *
FROM Table1 t1
INNER JOIN Table2 t2 ON t1.id = t2.id
WHERE t2.id = #T2id;
This is somewhat of a followon to SQL JOIN where to place the WHERE condition?
I would prefer to use the USING clause on the join, but am not yet able to put the field value condition with the join.
SELECT 1
FROM table1 t1
JOIN table2 t2 USING(id) AND t2.field = 0;
ORA-00933: SQL command not properly ended
Is it possible to have USING and another condition as part of the JOIN clause?
You can use:
SELECT 1
FROM table1 t1
JOIN table2 t2 USING(id)
WHERE t2.field = 0;
Using USING(id) is like using ON t1.id = t2.id except that in the JOIN result instead of two columns t1.id & t2.id there is only one id column.
For INNER JOIN USING with a condition followed by an OUTER JOIN you need a subquery to keep the WHERE with the USING:
SELECT ...
FROM (SELECT id, ...
FROM table1 t1
JOIN table2 t2 USING(id)
WHERE t2.field = 0) s
LEFT JOIN ...;
For an OUTER JOIN USING with a condition you need a subselect:
SELECT ...
FROM table1 t1
LEFT JOIN (SELECT *
FROM table2 t2
WHERE t2.field = 0) t2
USING (id);
See this re ON/WHERE with JOIN. See this re ON/WHERE when mixing INNER & OUTER JOINs.
I have this SQL...
UPDATE table1 t1
SET (t1.wert) =
(select t2.bezeichnung from table2 t2
where t1.id = t2.cpbezeichnung)
where t1.id = t2.cpbezeichnung
... which I cant run because it says me that it doesnt know t2.cpbezeichnung in line #5.
How can I fix it?
Table with alias t2 is not defined for UPDATE query, so it's clearly not known at line 5. Table t2 is defined only inside subquery on lines 3 and 4.
What exactly are you trying to achieve with condition on line 5?
If do you want prevent setting NULL into t1.wert for rows where there is no appropriate record in table t2, then you need to replace condition on line 5
UPDATE table1 t1
SET (t1.wert) =
(select t2.bezeichnung from table2 t2 where t1.id = t2.cpbezeichnung)
where t1.id IN (SELECT t2.cpbezeichnung from table2)
This will set values in t1.wert only for records where t1.id exists in t2.cpbezeichnung.
The t2 alias (along with table2) is only visible in the subquery. My guess is that you want
UPDATE table1 t1
SET t1.wert = (select t2.bezeichnung
from table2 t2
where t1.id = t2.cpbezeichnung)
where exists (select 1
from table2 t2
where t1.id = t2.cpbezeichnung)
which updates every row where there is a match between the two tables. If that's not what you want, posting a test case would be helpful.
When using correlated subqueries you cannot use an alias from the inner query in the outer query.
The outer query knows nothing about the inner query except its results.
Ref
You will have to use a Inner join on the two tables.
Something like
UPDATE table1 t1 INNERJOIN table t2 ON your_table_condition SET t1.wert = (select t2.bezeichnung from t2 where t1.id = t2.cpbezeichnung) where t1.id = t2.cpbezeichnung
I'm using a select statement inside a stored procedure. All I need to do is based on the value of a parameter I've to use either RIGHT JOIN or INNER JOIN. Please anyone there help me to achieve this. Many Thanks in Advance..
SELECT FLD1, FLD2
FROM TBL1 C (NOLOCK)
CASE
WHEN #SHOW = 156 THEN INNER
ELSE RIGHT JOIN TBL2 IC (NOLOCK) ON C.FLD3 = IC.FLD4
END
Is it correct? What would be the right way of doing this?
You could use IF statements:
DECLARE #Param VARCHAR(10)
SET #Param = 'RIGHT'
IF (#Param = 'RIGHT')
SELECT t1.Something
FROM Table1 t1
RIGHT JOIN Table2 t2 ON t1.ID = t2.ID
WHERE ...
ELSE IF (#Param = 'INNER')
SELECT t1.Something
FROM Table1 t1
INNER JOIN Table2 t2 ON t1.ID = t2.ID
WHERE ...
Or could be done as one SELECT (not necessarily best performance, be sure to test), something like:
SELECT t1.Something
FROM Table1 t1
RIGHT OUTER JOIN Table2 t2 ON t1.ID = t2.ID
WHERE (#Param = 'RIGHT') OR (#Param = 'INNER' AND t1.ID IS NOT NULL)
Theres more than enough information on the case statement out there, this should get you started.