select two databaseshttp://forums.asp.net/t/1771498.aspx/1?select+two+databasesWed, 22 Feb 2012 12:17:07 -050017714984840768http://forums.asp.net/p/1771498/4840768.aspx/1?select+two+databasesselect two databases <p>I have the following Select statement which uses two different databases, one is in my app_data folder, the other is somewhere else, the path is in the string I call dbpath.</p> <p>It works when I run it on my computer, it works when I put it on the internet, <strong>it does not work</strong> when I put it on the server where it has to run. &nbsp;(Error in From clause) What could the problem be?</p> <p>Dim sqlUnion As String = &quot;SELECT Kreditor_ID as Kredi_ID,Kreditor as Kredi FROM &quot; &amp; cs2 &amp; &quot; INNER JOIN &quot; &amp; cs3 &amp; &quot; ON tblVertrag.ID=tblVertragGattung.VertragID WHERE GattungID=? ORDER BY Kreditor_ID UNION SELECT Kredi_ID,Kredi FROM Projek WHERE Lager_ID=?&quot;</p> <p>Dim cs2 As String = &quot;~/App_Data/Visieren.mdb..tblVertrag&quot;<br> Dim cs3 As String = &quot;~/App_Data/Visieren.mdb..tblVertragGattung&quot;<br> cs2 = HttpContext.Current.Server.MapPath(cs2)<br> cs3 = HttpContext.Current.Server.MapPath(cs3)</p> <p>Using cn As New OleDbConnection(&quot;Provider=Microsoft.Jet.OLEDB.4.0;Data Source=&quot; &amp; dbpath)<br> Using dbcomm As New OleDbCommand(sqlUnion, cn)<br> dbcomm.Parameters.AddWithValue(&quot;Lager_ID&quot;, GattungID)<br> dbcomm.Parameters.AddWithValue(&quot;GattungID&quot;, GattungID)<br> cn.Open()<br> dbread = dbcomm.ExecuteReader</p> <p>dt2.Load(dbread)<br> dbread.Close()<br> cn.Close()</p> <p>End Using</p> <p>End Using</p> 2012-02-20T09:10:17-05:004840852http://forums.asp.net/p/1771498/4840852.aspx/1?Re+select+two+databasesRe: select two databases <p>Try this:</p> <p>Dim sqlUnion As String = String.Format(&quot;SELECT Kreditor_ID as Kredi_ID,Kreditor as Kredi FROM tblVertrag IN '{0}' INNER JOIN tblVertragGattung IN '{0}' ON tblVertrag.ID=tblVertragGattung.VertragID WHERE GattungID=? ORDER BY Kreditor_ID UNION SELECT Kredi_ID,Kredi FROM Projek WHERE Lager_ID=?&quot;, Server.MapPath(&quot;~/App_Data/Visieren.mdb&quot;))</p> 2012-02-20T09:57:11-05:004840919http://forums.asp.net/p/1771498/4840919.aspx/1?Re+select+two+databasesRe: select two databases <p></p> <blockquote><span class="icon-blockquote"></span> <h4>hans_v</h4> <p></p> <p>Try this:</p> <p>Dim sqlUnion As String = String.Format(&quot;SELECT Kreditor_ID as Kredi_ID,Kreditor as Kredi FROM tblVertrag IN '{0}' INNER JOIN tblVertragGattung IN '{0}' ON tblVertrag.ID=tblVertragGattung.VertragID WHERE GattungID=? ORDER BY Kreditor_ID UNION SELECT Kredi_ID,Kredi FROM Projek WHERE Lager_ID=?&quot;, Server.MapPath(&quot;~/App_Data/Visieren.mdb&quot;))</p> <p></p> </blockquote> <p></p> <p></p> <p>This gives me Error even when running on my computer - Syntax Error in From &nbsp;Clause</p> 2012-02-20T10:24:18-05:004840968http://forums.asp.net/p/1771498/4840968.aspx/1?Re+select+two+databasesRe: select two databases <p>First we need to identify where the problem occurs. Can you try to execute the following SQL statements?:</p> <p>Dim sqlUnion As String = String.Format(&quot;SELECT&nbsp;* FROM tblVertrag IN '{0}'&quot;, Server.MapPath(&quot;~/App_Data/Visieren.mdb&quot;))</p> <p>Dim sqlUnion As String = String.Format(&quot;SELECT * FROM tblVertragGattung IN '{0}'&quot;, Server.MapPath(&quot;~/App_Data/Visieren.mdb&quot;))</p> <p>Dim sqlUnion As String = &quot;SELECT * FROM Projek&quot;</p> 2012-02-20T10:43:26-05:004841140http://forums.asp.net/p/1771498/4841140.aspx/1?Re+select+two+databasesRe: select two databases <p>all of those 3 statements work</p> 2012-02-20T12:05:54-05:004841722http://forums.asp.net/p/1771498/4841722.aspx/1?Re+select+two+databasesRe: select two databases <p>Dim sqlUnion As String = String.Format(&quot;SELECT Kreditor_ID as Kredi_ID,Kreditor as Kredi FROM tblVertrag&nbsp;INNER JOIN tblVertragGattung&nbsp;ON tblVertrag.ID=tblVertragGattung.VertragID IN '{0}'&nbsp;WHERE GattungID=?&nbsp;UNION SELECT Kredi_ID,Kredi FROM Projek WHERE Lager_ID=? ORDER BY Kredi_ID &quot;, Server.MapPath(&quot;~/App_Data/Visieren.mdb&quot;))</p> 2012-02-20T18:39:11-05:004842506http://forums.asp.net/p/1771498/4842506.aspx/1?Re+select+two+databasesRe: select two databases <p>Thank you, this works. &nbsp;The only thing is, it doesn't select only distinct values - doesn't Union by default select only distinct values?</p> 2012-02-21T08:16:38-05:004842951http://forums.asp.net/p/1771498/4842951.aspx/1?Re+select+two+databasesRe: select two databases <p></p> <blockquote><span class="icon-blockquote"></span> <h4>pulsmartin</h4> The only thing is, it doesn't select only distinct values </blockquote> <p></p> <p>Dim sqlUnion As String = String.Format(&quot;SELECT DISTINCT Kredi_ID FROM (SELECT Kreditor_ID as Kredi_ID,Kreditor as Kredi FROM tblVertrag INNER JOIN tblVertragGattung ON tblVertrag.ID=tblVertragGattung.VertragID IN '{0}' WHERE GattungID=? UNION SELECT Kredi_ID,Kredi FROM Projek WHERE Lager_ID=?) ORDER BY Kredi_ID &quot;, Server.MapPath(&quot;~/App_Data/Visieren.mdb&quot;))</p> 2012-02-21T11:08:55-05:004844483http://forums.asp.net/p/1771498/4844483.aspx/1?Re+select+two+databasesRe: select two databases <p>What I meant is that if the same Kredi_ID exists in both tables, I want it to show only once. &nbsp;Could it be that it shows it to me twice because even though Kredi_ID is the same, Kredi is not?</p> 2012-02-22T07:06:43-05:004844676http://forums.asp.net/p/1771498/4844676.aspx/1?Re+select+two+databasesRe: select two databases <p></p> <blockquote><span class="icon-blockquote"></span> <h4>pulsmartin</h4> What I meant is that if the same Kredi_ID exists in both tables, I want it to show only once. </blockquote> <p></p> <p>I understand, and I think my last solution will do just that....</p> 2012-02-22T08:22:52-05:004844745http://forums.asp.net/p/1771498/4844745.aspx/1?Re+select+two+databasesRe: select two databases <p>It still returns two rows if it exists in both tables(I added the bold Kredi to your select statement)</p> <p>Dim sqlUnion As String = String.Format(&quot;SELECT DISTINCT Kredi_ID,<strong>Kredi</strong> FROM (SELECT Kreditor_ID as Kredi_ID,Kreditor as Kredi FROM tblVertrag INNER JOIN tblVertragGattung ON tblVertrag.ID=tblVertragGattung.VertragID IN '{0}' WHERE GattungID=? UNION SELECT Kredi_ID,Kredi FROM Projek WHERE Lager_ID=?) ORDER BY Kredi_ID &quot;, Server.MapPath(&quot;~/App_Data/Visieren.mdb&quot;))</p> 2012-02-22T08:50:00-05:004844887http://forums.asp.net/p/1771498/4844887.aspx/1?Re+select+two+databasesRe: select two databases <p>You mean your statement returns 2 records with the same kredi_ID AND Kredi ????</p> 2012-02-22T10:01:10-05:004844926http://forums.asp.net/p/1771498/4844926.aspx/1?Re+select+two+databasesRe: select two databases <p></p> <blockquote><span class="icon-blockquote"></span> <h4>pulsmartin</h4> <p></p> <p>What I meant is that if the same Kredi_ID exists in both tables, I want it to show only once. &nbsp;Could it be that it shows it to me twice because even though Kredi_ID is the same, Kredi is not?</p> <p></p> </blockquote> <p></p> <p></p> <p>If both columns are the same then it's not shown twice. &nbsp;But if kredi_ID is same and Kredi different then it's shown twice. How do I go about that?</p> 2012-02-22T10:15:58-05:004845161http://forums.asp.net/p/1771498/4845161.aspx/1?Re+select+two+databasesRe: select two databases <p>OK here is what I found searching on the internet ( now that I know where the problem lies) , seems to be working. &nbsp;Is this the correct way of doing it?</p> <p>Dim sqlUnion As String = String.Format(&quot;SELECT Kredi_ID, Max(Kredi) as Kredi FROM (SELECT Kreditor_ID as Kredi_ID,Kreditor as Kredi FROM tblVertrag INNER JOIN tblVertragGattung ON tblVertrag.ID=tblVertragGattung.VertragID IN '{0}' WHERE GattungID=? UNION SELECT Kredi_ID, Kredi FROM Projek WHERE Lager_ID=?) GROUP BY Kredi_ID ORDER BY 2&quot;, HttpContext.Current.Server.MapPath(&quot;~/App_Data/Visieren.mdb&quot;))</p> 2012-02-22T12:03:14-05:004845176http://forums.asp.net/p/1771498/4845176.aspx/1?Re+select+two+databasesRe: select two databases <p></p> <blockquote><span class="icon-blockquote"></span> <h4>pulsmartin</h4> If both columns are the same then it's not shown twice. </blockquote> <p></p> <p>That is exactly what DISTINCT is all about. It does show only unique rows.</p> <p></p> <blockquote><span class="icon-blockquote"></span> <h4>pulsmartin</h4> But if kredi_ID is same and Kredi different then it's shown twice. How do I go about that? <p></p> <p>Then you need to decide which of the 2 (or more) values of Kredi you want to show?</p> </blockquote> 2012-02-22T12:09:06-05:004845178http://forums.asp.net/p/1771498/4845178.aspx/1?Re+select+two+databasesRe: select two databases <p></p> <blockquote><span class="icon-blockquote"></span> <h4>pulsmartin</h4> <p></p> <p>OK here is what I found searching on the internet ( now that I know where the problem lies) , seems to be working. &nbsp;Is this the correct way of doing it?</p> <p>Dim sqlUnion As String = String.Format(&quot;SELECT Kredi_ID, Max(Kredi) as Kredi FROM (SELECT Kreditor_ID as Kredi_ID,Kreditor as Kredi FROM tblVertrag INNER JOIN tblVertragGattung ON tblVertrag.ID=tblVertragGattung.VertragID IN '{0}' WHERE GattungID=? UNION SELECT Kredi_ID, Kredi FROM Projek WHERE Lager_ID=?) GROUP BY Kredi_ID ORDER BY 2&quot;, HttpContext.Current.Server.MapPath(&quot;~/App_Data/Visieren.mdb&quot;))</p> <p></p> </blockquote> <p></p> <p>Yep, that looks good.... This will only show each Kredi_ID only once, and if there are multiple values of Kredi, it will show the maximum value....</p> 2012-02-22T12:10:24-05:004845189http://forums.asp.net/p/1771498/4845189.aspx/1?Re+select+two+databasesRe: select two databases <p>Thanks for sticking with me until the end</p> 2012-02-22T12:17:07-05:00