I have been struck while writing a query to pick a string . What I'm posting the query is sample data
Declare @tbl table (tblname varchar(20),Query VARCHAR(MAX))
Insert into @tbl (tblname,Query) values ('Employee','select EmpId,
(Select top 1 Dept_ID from Stg.Dept
where Deptid = Deptid) Dept_ID,
(Select top 1 Dept_ID from Stg.Sub_dept
where Deptid = D.Deptid) SubDept_ID
from stg.Employee E
left join stg.Dept D
ON D.EMPID = E.EmpID
WHERE EMPID = (Select EMPID from stg.dept where Deptid = Deptid)')
Query :
select tblname,SUBSTRING(LTRIM(SUBSTRING(Query, CHARINDEX('FROM', Query)+4, LEN(Query))),
CHARINDEX('.', LTRIM(SUBSTRING(Query, CHARINDEX('FROM', Query)+4, LEN(Query))))+1,
CHARINDEX(' ',
SUBSTRING(LTRIM(SUBSTRING(Query, CHARINDEX('FROM', Query)+4, LEN(Query))),
CHARINDEX('.', LTRIM(SUBSTRING(Query, CHARINDEX('FROM', Query)+4, LEN(Query))))+1, LEN(Query)))-1) from
@tbl
Result :
tblname Req_tbl
Employee Dept
Actually this query is picking the stg.Dept table name from sub query in that query column. What I exactly want is to pick up stg.Employee table which is the main table.
Output :
tblname Req_tbl
Employee Employee
Can you please suggest on this