I have dynamic SQL statement generated using criteria. This statement involves multiple joins using many tables. Requirement is to get list of all the tables used in this statement.
For example:
SELECT
table1.a, table2.b, ...
FROM
table1
INNER JOIN
table2 ON table1.col1 = table2.col1
LEFT OUTER JOIN
table3 ON table2.col3 = table3.col1
I want to get list of tables used in the above query dynamically like:
table1,table2,table3.
I am using C# and SQL Server. I think parsing the string and finding individual tables will be complex. Is there any way we can get the list of Tables used in a query from SQL Server itself?
Related
I have looked at and tried many of the similarly named questions to no avail. I am programming in C# and need to build a query that joins two tables from two different SQL Servers.
Currently I have the query build with an older copy of the table on the same database. It is defined as follows:
SELECT DISTINCT
tblWCPriorities.WONum, tblWCPriorities.OverallPriority, tblWCPriorities.LegendPriority, LTRIM(RTRIM(tblJDEWOData.PartNum)) AS PartNum, tblJDEWOData.OpSeqNum, tblJDEWOData.QtyReceived,
LTRIM(RTRIM(tblJDEWOData.CellName)) AS WorkCenterName, tblJDEWOData.QtyAtOperation, tblWCPriorities.StockDate, tblWCPPartType.PartTypeName
FROM tblWCPriorities INNER JOIN
tblJDEWOData ON tblWCPriorities.WONum = tblJDEWOData.WONum LEFT OUTER JOIN
tblWCPPartType ON tblWCPriorities.PartTypeID = tblWCPPartType.PartTypeID
ORDER BY tblWCPriorities.OverallPriority, tblWCPriorities.WONum, tblJDEWOData.OpSeqNum
How do I combine the two?
I have a database with multiple tables and some stored procedures that query if to get specific data from various tables in order to display desired information.
Find one example of a stored procedure bellow:
SELECT DISTINCT
ROW_NUMBER() OVER (ORDER BY dbo.[table1].ReportedDate DESC) AS rowNumber,
dbo.[table1].[id],
dbo.[table1].caseReference,
dbo.[table2].organisationName AS customerName,
dbo.[table3].assetRegistration,
dbo.[table4].surname
FROM dbo.[table1] WITH (NOLOCK)
LEFT JOIN dbo.[table2] with (NOLOCK)
ON dbo.[table2].JobId = dbo.[table1].[id]
LEFT JOIN dbo.[table3] WITH (NOLOCK)
ON dbo.[table3].id = dbo.[table2].[JobServiceId]
LEFT JOIN dbo.[table4] WITH (NOLOCK)
ON dbo.[table4].[jobID] = dbo.[table1].[id]
WHERE (table1.caseReference LIKE #caseReference+'%')
I want to move from using such stored procedures to a more code based approach using entity framework. How can I recreate a query like the one above using Linq query over the dbContext classes mapped to the database?
I am mostly having problems in figuring out how to choose the data I want to be returned from each table and how to put it all together.
you can do it easly Linq query or lambda expressions over the dbContext classes mapped to the database.
check this it would be help you
Entity Framework Join 3 Tables
How to join multiple tables?
I am programming an Excel add-in in C# where I process data contained in different DataTable objects. I would like to provide a function to perform SQL queries on the data, with the ability to reference data from other tables in where and sort by clauses (for example, using a join).
An example of such a query would be
SELECT name
FROM Table1
WHERE id = Table2.id AND Table2.age > 18
The problem with this is that a DataTable doesn't know of the existance of the other DataTables, so (for so far I know) there are no such methods in the class. Also, I cannot use something like LINQ, since the query will be written by the users of the add-in in excel.
Would it be a good solution to copy the data to an in-memory database, where each DataTable is mapped to a table? How would this work performance-wise? Is there a simpler solution?
In terms of SQL query you are missing a table reference in selecting the tables, corrected query will look like
SELECT name
FROM Table1, Table2
WHERE Table1.id = Table2.id AND Table2.age > 18
Use Table1.name if there is same named attribute in Table2.
However using only WHERE condition in Joins without specifying the joining attribute is not recommended read this question. Use JOIN.
SELECT Table1.name
FROM Table1 INNER JOIN Table2 ON Table1.id = Table2.id WHERE Table2.age > 18
I would like to know how to efficiently count (SQL server side) the amount of distinct count of results for a specific range of a related entity that has a many to many relationship.
This is the current situation in entity Framework:
Table1 1<------->∞ Table2
Table2 ∞<------->∞ Table4
Table 2 and Table 4 have a many to many relationship and are linked with Table3 in SQL.
What I want is the distinct count of table4 results related to a specific range of Table1.
In LinQ to SQL the query is this:
(from dc in Table1
join vc in Table2 on dc.Table1Id equals vc.Table2Id
join vcac in Table3 on vc.Table2Id equals vcac.Table3Id
join ac in Table4 on vcac.Table3Id equals ac.Table4Id
where dc.Table1Id > 200000
group ac by ac.Table4Id into nieuw
select new { acid= nieuw.Key}).Count()
This lets SQL server return the count directly.
Because the extra table for the many to many relation ( Table3) is gone, I have had problems converting this query to L2E in query syntax. ( since I cannot join table 4 with table 2 with an inner join).
I have this in chained syntax, however, is this efficient ( does this fetch the whole list, or does it let SQLserver do the count, as I'm not sure this is an efficient way to select, Table 2 contains about 30.000 entries, I don't want it to fetch this result just to count it):
context.Table4.Where(a => a.Table2.Any(v => v.Table1Id > 200000)).Select(a => aTable4Id).Distinct().Count();
How would I go converting the Linq2SQL query into L2E in the query syntax ? Or is the chained syntax fine in this situation ?
The .Select() method uses deferred execution, meaning it won't actually run the query until you need the results. At that point in the chain it still exists only as a query definition. Then you modify with .Distinct() before getting .Count() - which does query the database using a SQL GROUP BY statement. So you should be good.
I want to select rows from multiple tables using subsonic. For one table I can use Query object, but I don't know how I can add more than one tables to query.
You neet to join them, much like you would do in SQL.
If you have a foreign key relationship in the schema, Subsonic is smart enough to figure the joins directly:
DataSet DS = DB.Select().From<Table1>().InnerJoin<Table2>().ExecuteDataSet();
If you don't have a FKI between the tables, you need to manually specify the columns from each table to create the join:
DataSet DS = DB.Select().From<Table1>().InnerJoin(Table1.FKIColumn,Table2.IDColumn).ExecuteDataSet();
Similarly you can create the Left/Right Outer joins,etc.
Remeber you can only join them on simple FKI constraints. For example I found no easy way to do "INNER JOIN Table2 on Table1.FKI = Table2.ID and Table2.CreateDate>Table1.CreateDate" directly from SubSonic.
And a big downside to using SubSonic multiple table joins is that you will run into troubles if you have identically named columns in both tables.