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?
Related
I have a query with multiple joined tables, this is a simple version, I have many such with further levels of joins:
SELECT ApplicationField.OID, ApplicationField.FieldName, ApplicationField.Label, ApplicationField.DataType, ApplicationField.Length, ApplicationField.FieldType, ApplicationField.DefaultValue, ApplicationField.DataPrefix,
ApplicationField.DisplaySequence, ApplicationField.TabNumber, ApplicationField.Required, ApplicationField.Hidden, ApplicationField.PasswordField, ApplicationField.Loopback, ApplicationField.ApplicationVersion,
ApplicationField.OptimisticLockField, FieldQuery.QueryID, FieldQuery.ApplicationField, FieldQuery.ExecuteSequence, FieldQuery.MaxRows, FieldQuery.ReturnField, FieldQuery.QueryType
FROM ApplicationField LEFT OUTER JOIN
FieldQuery ON ApplicationField.OID = FieldQuery.ApplicationField
WHERE(ApplicationField.ApplicationVersion = #versionOID)
ORDER BY ApplicationField.TabNumber, ApplicationField.DisplaySequence
How can I use a Datareader or DataAdapter using a single SQl statement that returns multiple related tables, keeping the tables separate in a dataset, so that I get separate tables for ApplicationFields and related FieldQueries? There may be multiple FieldQueries per Application Field, one to many relationship.
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 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?
I have a set of (not very well normalised or relational) tables named
PLAN,
GROUP,
PRODUCT
CLIENT
Most have linkage i.e.
PLAN -> CLIENT on clno
GROUP to PRODUCT on PRODCD
However, the linkage between PLAN and GROUP is tricky. A plan has 2 field of interest GRPNO and PRODCD.
What I want to do is if GRPNO != 0 then join GROUP on GRPNO. However if GRPNO = 0 then I want to join GROUP on PRODCD.
The frustrating thing is that the fileds I want to return in my queries are the same across the board I just need to be able to vary the join, or join the same table twice.
The best I can come up with is 2 queries and merge them using datasets, or possibly using a union.
Is there a nifty way to do this in one select?
I should point out I am access Foxpro over ODBC to do this.
Thank you!
You can do:
JOIN GROUP AS G ON
(PL.GRPNO = 0 AND G.PRODCD = PL.PRODCD) OR
(PL.GRPNO !=0 AND G.GRPNO = PL.GRPNO)
However it would surprise me if this is faster than using UNION ALL.
I keep tables on different .sdf files because it's easy to manage them, ie; back up only changed db file, etc, plus in future db size might bigger and there is -4GB limit-
I need to join the tables and this will be my first -possibly LINQ- attempt. I know there are tons of examples/documents but a simple example would be nice to start.
This is the query for MS SQL Server:
SELECT personID, personPin, personName, seenTime
FROM db1.personList
LEFT JOIN db2.personAttendances on personID = seenPersonID
ORDER BY seenTime DESC
I think LINQ will be the way to go as you're querying across 2 different contexts. LINQ joins are quite easy: http://msdn.microsoft.com/en-gb/vcsharp/ee908647
Something like:
var q = from c in db1Context.personList
join p in db2Context.personAttendances on c.personID equals p.seenPersonID
select new { Category = c, p.ProductName };
I don't think SqlCE supports linking at the Db (SQL) level.
That means you'll have to use Linq-to-Objects. The example query has no WHERE clause so you can simply load the entire tables into Lists. But when the datasets get bigger that may not be acceptable.