I have a stored procedure for a select query and there are left join and inner join commands in the query. I'm using the script below for dependencies of the stored procedure.
select
referenced_database_name,
referenced_entity_name,
referenced_minor_name
from
sys.dm_sql_referenced_entities ('dbo.sp_test', 'OBJECT')
How can I get the column name which used for joining tables in this SP? Or is there any simple way to get/parse column names from this script:
select column1, column2
from tableA
inner join tableB on tableB.id = tableA.id
I need tableB.id and tableA.id keywords from the script
I've tried parse that script with substring, left etc functions but they're insecure for me. There are lots of piece of select query using union all.
I'v solved my problem parsing the query. At first, i left a delimiter (--*) for splitting the section which contains joins then used that code for parsing the query (i know its not professional but it has worked for me) :
select distinct
referenced_database_name,
referenced_entity_name,
referenced_minor_name,
SUBSTRING(c.value,CHARINDEX('on',value)+3,len(value)) split
,IIF(SUBSTRING(c.value,CHARINDEX('on',value)+3,len(value)) like '%'+CONCAT(referenced_entity_name,'.',referenced_minor_name)+'%',1,0) as clm
from sys.dm_sql_referenced_entities ('dbo.sp_test', 'OBJECT') a
left join sys.sysdepends b on OBJECT_NAME(depid) = a.referenced_entity_name
outer apply string_split(REPLACE(OBJECT_DEFINITION(id),'--*','/'),'/') c
where OBJECT_NAME(id) = 'sp_test' and c.value like '%join%' and referenced_minor_name is not null
Related
When I write an exist query in ORACLE JOIN syntax like this, it works fine as expected.
Select * from TableA
where not exists (Select 1 from TableB where TableB.Id = TableA.TableBForeignKeyId)
When i write it as ANSI JOIN syntax, it doesn't work.
Select * from TableA
where not exists (Select 1
from (TableA
INNER JOIN TableB
on TableA.TableBForeignKeyId = TableB.Id))
On the ANSI JOIN version, TableA behave not like the upper querys TableA, because of that, the complete query returns no rows.
Not: There is only one row on TableA that has no reference on TableB.
This query should return one row of TableA.
Why ANSI JOIN behave like this?
The syntax you have written in the first query is good: i.e.
Select * from TableA
where not exists (Select 1 from TableB where TableB.Id = TableA.TableBForeignKeyId)
In the second query, TableA of the inner query will not refer to the outer TableA and hence you are not getting the desired result.
Why you are not happy with the first query?
If you are looking for some other way of writing the query then you can use the following query:
Select * from TableA
where TableA.TableBForeignKeyId not in (Select TableB.Id
from TableB )
Cheers!!
The two queries are not similar at all.
The first is a correlated subquery. The WHERE condition refers to the outer query.
The second is an uncorrelated subquery. There is no connection to the outer query. In this case, the subquery returns a single value, independent of the outer query. If it returns no rows, then the overall query returns no rows. If it returns one or more rows, then the overall query returns multiple rows.
Both are valid, correct syntax. You should use the one you want, which would normally be the correlated subquery.
i want to join two tables in hive based on the partial match. So far, i tried with the below SQL query:
select * from tableA a join tableB b on a.id like '%'+b.id+'%';
and instr but nothing working, is there a way?
JOIN in Hive only support equation conditions. Plus, you should use CONCAT for your string concatenation.
This is one solution instead.
select *
from tableA a, tableB b
where a.id like concat('%',b.id,'%')
I have a table which contains strings which is needed to be searched [like below image]
TableA
After that, I have a table which stores data [like below image]
TableB
And what the search function does is to select from TableB only if the record contains the string in TableA [i.e. The expected result should be like below image(TableC)]
TableC
I've tried using the SQL below, but the SQL had some error while trying to run [Incorrect syntax near 'select'] , also, the SQL is a bit complicated, is there any way to make the SQL simplier?
select * from TableB a
where exists
(select colA from TableB b
where b.colA = a.ColA and Contains (b.ColA, select searchCol from TableA))
Please try:
select
a.*
From tblA a inner join tblB b
on b.Col like '%'+a.Col+'%'
SQL Fiddle Demo
One of these should work according to me
SELECT b.colA
FROM TableB b join TableA a
WHERE colA like '%' + a.ColA + '%';
SELECT b.colA
FROM TableB b join TableA a
WHERE INSTR(b.colA, '{' + a.colA + '}') > 0;
SELECT b.* FROM tblA a,tblB b
WHERE PATINDEX('%'+a.Col+'%',b.Col)>0
SQL FIDDLE DEMO
I have searched for performance difference between PATINDEX and LIKE but not got a conclusive answer , some say PATINDEX is faster when indexing can't be used which is when pattern to be searched is enclosed within wildcard character eg '%'+a.Col+'%' while other post mention they can similar performance .
Perhaps some SO SQL Wizard can look in crystal ball and show us the light
I have a table called Table1.
The table contains columns like:
The sourceID (ID of the join)
The sourceTable (the table to be joined table, this is variable)
My query:
SELECT T.Category, J.Tariff
FROM Table1 as T
INNER JOIN T.SourceTable J ---- This needs to be changed
ON T.sourceID = J.id
Is this possible?
Basically the source table can be different for every row
I'd say that's generally a bad idea. Even if possible, it basically prevents the optimiser from doing much (which can cause the query to slow down a lot).
I suggest changing your database design to make what you're trying to do easier (think having a base table which the tables you want to join on have a 1-to-1 mapping to) rather than trying to get this to work.
You can use Union, and filter by the column with the table pointer. Something like this:
SELECT T.Category, J.Tariff
FROM Table1 as T
INNER JOIN T.SourceTable1 J ON T.sourceID = J.id
where T.sourceTable = '1'
union
SELECT T.Category, J.Tariff
FROM Table1 as T
INNER JOIN T.SourceTable2 J ON T.sourceID = J.id
where T.sourceTable = '2'
Table names cannot be dynamic in an SQL Query. You have to find a different way to do this. Use a Stored Procedure and/or EXEC().
In principle it's possible using left joins, but only if you have a fixed set of lookup tables (otherwise, you'll need to build your SQL statement dynamically):
SELECT T.Category, coalesce(source1.tariff, source2.tariff) as tariff
FROM Table1 as T
LEFT OUTER JOIN T.source1
ON T.sourceID = source1.id and t.sourcetable = 'source1'
LEFT OUTER JOIN T.source2
ON T.sourceID = source2.id and t.sourcetable = 'source2'
but as mentioned in the other answers, that's usually a sign that your database design is flawed.
Try something like this:
1) Create a VIEW with all source tables:
CREATE VIEW source_tables AS
(SELECT 'table_1' as table_name,
id,
value
FROM table_1
UNION ALL
SELECT 'table_2' as table_name,
id,
value
FROM table_2
UNION ALL
..........
SELECT 'table_n' as table_name,
id,
value
FROM table_n);
2) Use the VIEW to get the values from source table:
SELECT T.category, S.value
FROM base_table as T
JOIN source_tables S ON T.sourceID = S.id AND T.SourceTable = S.table_name
Joins are usually used to fetch data from 2 tables using a common factor from either tables
Is it possible to use a join statement using a table and results of another SQL statement and if it is what is the syntax
Sure, this is called a derived table
such as:
select a.column, b.column
from
table1 a
join (select statement) b
on b.column = a.column
keep in mind that it will run the select for the derived table in entirety, so it can be helpful if you only select things you need.
EDIT: I've found that I rarely need to use this technique unless I am joining on some aggregated queries.... so I would carefully consider your design here.
For example, thus far most demonstrations in this thread have not required the use of a derived table.
It depends on what the other statement is, but one of the techniques you can use is common table expressions - this may not be available on your particular SQL platform.
In the case of SQL Server, if the other statement is a stored procedure, you may have to insert the results into a temporary table and join to that.
It's also possible in SQL Server (and some other platforms) to have table-valued functions which can be joined just like a view or table.
select *
from TableA a
inner join (select x from TableB) b
on a.x = b.x
Select c.CustomerCode, c.CustomerName, sq.AccountBalance
From Customers c
Join (
Select CustomerCode, AccountBalance
From Balances
)sq on c.CustomerCode = sq.CustomerCode
Sure, as an example:
SELECT *
FROM Employees E
INNER JOIN
(
SELECT EmployeeID, COUNT(EmployeeID) as ComplaintCount
FROM Complaints
GROUP BY EmployeeID
) C ON E.EmployeeID = C.EmployeeID
WHERE C.ComplaintCount > 3
It is. But what specifically are you looking to do?
That can be done with either a sub-select, a view or a temp table... More information would help us answer this question better, including which SQL software, and an example of what you'd like to do.
Try this:
SELECT T1.col1, t2.col2 FROM Table1 t1 INNER JOIN
(SELECT col1, col2, col3 FROM Table 2) t2 ON t1.col1 = t2.col1