Is it possible to get all the tables and views used inside a user defined function? Is there any defined procedure to get that by passing the udf name?
create function sample_function
return table
as
return
select *
from table1 t1
join table2 t2 on t2.id = t1.id
I need to get table1 and table2 when i pass sample_function to any procedure.
Any Ideas would help.
Hope this helps.
SELECT DISTINCT referenced_entity_name FROM sys.dm_sql_referenced_entities('dbo.fn_sample','OBJECT')
EDIT:
Use the below query, to get only the referenced tables
SELECT DISTINCT referenced_entity_name
FROM sys.dm_sql_referenced_entities('dbo.fn_sample','OBJECT') ro
INNER JOIN sysobjects so ON (ro.referenced_id = so.id)
WHERE so.xtype = 'U'
Related
I have scenario where I have to find the sum of multiple columns from table2 and I have the value in where clause from another table1.
I wrote mySql query for the same as follow. I need to write it in the jooq.
select (sum(t2.column1)+sum(t2.column2)+sum(t2.column3)) as total_amount
from db.table1 t1, db.table2 t2
where t1.column1 = ‘value1’ and t1.column2 = t2.column4;
As a general rule of thumb, all functions are available from org.jooq.impl.DSL by the same name, and all operators are available from org.jooq.Field by a name that reflects the way the operator is pronounced. In your case, use:
DSL.sum(Field)
Field.plus(Field)
Specifically, assuming this static import:
import static org.jooq.impl.DSL.*;
Write:
Table1 t1 = TABLE1.as("t1");
Table2 t2 = TABLE2.as("t2");
ctx.select(sum(t2.COLUMN1).plus(sum(t2.COLUMN2)).plus(t2.COLUMN3)).as("total_amount"))
.from(t1, t2)
.where(t1.COLUMN1.eq("value1"))
.and(t1.COLUMN2.eq(t2.COLUMN4))
.fetch();
I've got a table T1 with 2 foreign keys targeting the same table T2.
So, my SQL statement is like that :
SELECT *
FROM T1
INNER JOIN T2 AAA ON AAA.ID = T1.ID1
INNER JOIN T2 BBB ON BBB.ID = T1.ID2
(this sql works)
Then, I use $info = $exec->fetch(PDO::FETCH_ASSOC);
I'd like, at end, use the $info->AAA.name and $info->BBB.name,
but I cannot find how to set the table name into the reading. And I'd
like to avoid to change my SQL by adding dozen of ... AS ...
Any idea?
UPDATE1
I have searched through PDO documentation and found directive PDO::ATTR_FETCH_TABLE_NAMES. Docs say that it makes driver to return table names before column names. Table names will be separated from column names by '.'(dot).You could try to use this directive to solve your problem, but perhaps it will not work without aliases..
Please try this on: $PDO->setAttribute(PDO::ATTR_FETCH_TABLE_NAMES, true);
UPDATE2
I have found following question and answer from the asking person, who have used this directive to solve his problem, similar to yourth.
SELECT result with table names for columns
This question dublicates several other questions asked earlier:
PDO: fetchAll() with duplicate column name on JOIN
PDO Auto-aliasing when fetching?
So, the first answer was: "NO, you cannot do this without setting aliases for each column."
Workaround is to use PDO::FETCH_NUM, which will return you a numbered array of fetched columns. But I would not recomend it, because if you will change your tables' structure, your PHP script may become broken.
Anyway, it is still a good practise to select certain columns and set aliases instead of all *. Like this:
SELECT T1.ID1 as ID1, T1.ID2 as ID2, T1.name as T1_name, AAA.name as AAA_name, BBB.ID as BBB_name
FROM
T1
INNER JOIN
T2 AAA
ON AAA.ID = T1.ID1
INNER JOIN
T2 BBB
ON BBB.ID = T1.ID2
I am running into some trouble with the following circumstances:
I have a query that creates two temp tables, and the following select to join them together--
SELECT * FROM result
INNER JOIN result2 ON result2.packetDetailsId = result.packetDetailsId
I am then trying to create another column from concatenating a few of the resulting fields and then use that to reference/query against another table. Is there a way to accomplish this in one query? Should I get away from the temp tables?
Thank you again in advance.
update: If I try to alias the combination of the two temp tables I get an error message stating [Err] 1060 - Duplicate column name 'packetDetailsId'
select * from (
SELECT * FROM result
INNER JOIN result2 ON result2.packetDetailsId = result.packetDetailsId) as myalias
Another Update: I almost have it working as one query but I get the result "(BLOB)" in the column I concoctenated:
select packet_details.packetDetailsId,products.productId,Credit,AccountNum,OrderStat, CONCAT(products.productId,Credit,'_',OrderStat) as consol from (
select packetDetailsId, GROUP_CONCAT(Credit) AS Credit, GROUP_CONCAT(AccountNum) AS AccountNum, GROUP_CONCAT(OrderStat) AS OrderStat FROM
( SELECT pd_extrafields.packetDetailsId,
CASE WHEN pd_extrafields.ex_title LIKE ('%Credit%')
THEN pd_extrafields.ex_value ELSE NULL END as Credit,
CASE WHEN pd_extrafields.ex_title LIKE ('%Account%')
THEN pd_extrafields.ex_value ELSE NULL END as AccountNum,
CASE WHEN pd_extrafields.ex_title LIKE ('%Existing%')
THEN pd_extrafields.ex_value ELSE NULL END as OrderStat
FROM pd_extrafields )AS TempTab GROUP BY packetDetailsId ) as alias2
INNER JOIN packet_details ON alias2.packetDetailsId = packet_details.packetDetailsId
INNER JOIN sales ON packet_details.packetDetailsId = sales.packetDetailsId
INNER JOIN sold_products ON sales.saleId = sold_products.saleId
INNER JOIN products ON sold_products.productId = products.productId
If I understand correctly, you already have the temporary tables created and you need to "concatenate" the results, using from ... inner join ...
The only possible restriction you may have is that you can only reference your temporary tables once in your from clause; besides that, there are no other restrictions (I frequently use temporary tables as intermediate steps in the creation of my final result).
Tips
Let's say your temp tables are temp_result1 and temp_result2. Both tables have a field packedDetailsId, on which the join will be performed. Remember to create the appropriate indexes on each table; at the very least you need to index packedDetailsId on both tables:
alter table temp_result1
add index PDI(packedDetailsId);
alter table temp_result2
add index PDI(packedDetailsId);
Now, just execute a query with the desired join and concatenation. If concat returns BLOB, then cast the result as char (of course, I'm assuming you need a text string):
select r1.*, r2.*, cast(concat(r1.field1, ',', r2.field2) as char) as data_concat
from temp_result1 as r1
inner join temp_result2 as r2 on r1.packedDetailsId = r2.packedDetailsId;
I see your problem is that GROUP_CONCAT is returning BLOB values... It's normal (MySQL doesn't know a priori how to return the values, so it returns binary data); just use the cast function.
Hope this helps you
so, if the result2 and result are both temp tables, you will have to include the # if local temp table and ## if global temp table
so your statements should be :
SELECT * FROM #result
INNER JOIN #result2 ON #result2.packetDetailsId = #result.packetDetailsId
My Bad. This is only applicable for MS SQL
Is it possible to achieve next thing without using views, but just one single query? I have two tables:
TableA->TanbleB (1-many) ON TableA.Id = TableB.TableAId
I need to update one field in Table A (TableA.Field1) for records in TableA that satisfy condition on one field in tableA (WHERE TableA.Field2=SomeValue)
.
TableA.Field1 will be updated from TableB with value that is last inserted (last inserted value in related records to TableA).
I will put an example:
UPDATE TableA a SET Field1 = (SELECT TOP 1 b.Feild1 * b.Field2 FROM TableB b WHERE b.TableAId = a.id) WHERE field2 = 1
I know Above example doesn't work, but I have many ways tried using INNER JOIN and failed. I had an idea to use something like this:
UPDATE TableA INNDER JOIN ( SELECT ... FROM TABLE B) ON TABLEA.Id= TableB.TableAId SET ....
But the 2ns query should return 1 record for each DISTINCT TableAId, but only the last inserted.
I hope I am making some sense here.
Thanks in advance.
Here is some SQL that will do what you want
UPDATE T1 INNER JOIN T2 ON T1.ID = T2.T1ID SET T1.F2 = [T2].[F2]*[T2].[F3] WHERE (((T1.F1)="ABC") AND ((T2.ID)=DMax("[ID]","[T2]","[T1ID]=" & [T1].[ID])));
This predicated on T1.ID being the primary key for T1 and T2.T1ID being a index field in T2
One of the flaws in Access is that you can't run an "UPDATE" query based on a "SELECT" query, it will usually give the error:
Operation must use an updateable query
The only way around is as you say to create a view of the "SELECT" query and then inner join this on your table, Access is then working with a static recordset and can handle the "UPDATE" query ok
Alternatively you could write a VBA procedure to step through line by line with the Recordset.
Best of luck : )
UPDATE:
SELECT b.TableAId, b.Feild1 * b.Field2 INTO tblView FROM TableB As b WHERE b.field2 = 1
I am buidling an app in CakePHP. I have 2 models:
- Project
- User
The Project model has various belongsTo relations to the user model, one for the creator, one for the last editor and one for the manager. This works fine.
Then I add a virtual field to the User model, called 'name', which is CONCAT(first_name, ' ', last_name). It combines the first name and last name into a general name field, which is used througout the app.
After this, I get SQL errors saying that the first_name column is ambiguous. This is because in the query, the alias for Creator, Manager, etc is not used in the CONCAT field.
Any ideas on how to avoid this?
Showing the exact queries should help resolve this problem. But if you are joining 2 tables, and they both have a column with the same name. you have to reference the column with TableName.ColumnName, like this.
Select Table1.Column1 AS someColumn, Table2.Column1 AS SomeOtherColumn
FROM Table1
INNER JOIN Table2
ON Table1.ID = Table2.Table1ID
WHERE Table1.ID = 3
You can shorten this up by giving your tables aliases, As follows.
Select T1.Column1 AS someColumn, T2.Column1 AS SomeOtherColumn
FROM Table1 AS T1
INNER JOIN Table2 AS T2
ON T1.ID = T2.Table1ID
WHERE T1.ID = 3
I found the solution: http://book.cakephp.org/view/1632/Virtual-fields-and-model-aliases
Try specifying what table the first_name column is from. Something like this:
CONCAT(table1.first_name,'',table1.last_name)