﻿<?xml version='1.0' encoding='UTF-8'?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/"><channel><title>SQLServerCentral / SQL Server 2008 / T-SQL (SS2K8)  / How to combine two tables when one table is in different database and this table will determine the value to be shown on the first table. / Latest Posts</title><generator>InstantForum.NET v2.9.0</generator><description>SQLServerCentral</description><link>http://www.sqlservercentral.com/Forums/</link><webMaster>notifications@sqlservercentral.com</webMaster><lastBuildDate>Tue, 21 May 2013 18:22:54 GMT</lastBuildDate><ttl>20</ttl><item><title>RE: How to combine two tables when one table is in different database and this table will determine the value to be shown on the first table.</title><link>http://www.sqlservercentral.com/Forums/Topic1406814-392-1.aspx</link><description>Hello [b]Geomorian[/b].  That was really cool.  I am thinking it has be another technique, like for example, using a variable ([font="Courier New"][b]BIT[/b][/font] type) to set to either 1 or 0, then "convert" it to "YES" or "NO". ([i]I guess I am just making my life too hard, silly me.[/i]).  But for the record, is this possible?  If yes, can someone give a little example?  Well anyway, this is straight to the point, and it is awesome! :cool:Thank you [b]Geomorian[/b] and [b]Sean Lange[/b]. :-)Warm regards,Mark Squall</description><pubDate>Thu, 17 Jan 2013 07:19:02 GMT</pubDate><dc:creator>marksquall</dc:creator></item><item><title>RE: How to combine two tables when one table is in different database and this table will determine the value to be shown on the first table.</title><link>http://www.sqlservercentral.com/Forums/Topic1406814-392-1.aspx</link><description>[quote]marksquall (1/14/2013Just one last thing, how would I add an additional column called [font="Courier New"][b]CameFromArchive[/b][/font] so that the database administrator would determine what [font="Courier New"][b]CtrlNo[/b][/font] came from the archive table (0-NO 1-YES)?[/quote]I think you can use a case just like the one that was allready included in the query posted by Sean, like so :[code="plain"]SELECT        A.ConsInvNumber AS [Consolidated Invoice No],       A.InvTrackNo AS [Invoice TrackNo],       B.SaleOrderNo AS [Sales OrderNo],       B.OrderLine AS [Sales Order LineNo],       case when A.CtrlNo = 0 then osa.CtrlNo else a.CtrlNo end as CtrlNo,	      [b] case when OSA.CtrlNo is NULL then 'No' else 'Yes' end as CameFromArchive[/b]FROM DBMain.dbo.ConsInvoiceDetail AS A  INNER JOIN     DBMain.dbo.IndInvDetail AS B ON A.InvTrackNo = B.InvTrackNoLeft Join (select top 1 CtrlNo from DBArchive.dbo.OrderSummaryArchive where SalesOrderNo = B.SaleOrderNo) osaWHERE A.ConsInvNumber=980029768 [/code]EDIT: forgot a comma</description><pubDate>Tue, 15 Jan 2013 03:19:14 GMT</pubDate><dc:creator>Geomorian</dc:creator></item><item><title>RE: How to combine two tables when one table is in different database and this table will determine the value to be shown on the first table.</title><link>http://www.sqlservercentral.com/Forums/Topic1406814-392-1.aspx</link><description>Hello [b]Sean[/b].  Thank you for the sample query, it works just fine (with some minor modifications).  Awesome! :-)I apologize to you (and to the readers) for making my aliases so short, actually I am just in a "hurry" that is why I used short aliases. In my query here, I (really) used full table abbreviation. ([i]silly me[/i]). :crazy: :w00t: :-DJust one last thing, how would I add an additional column called [font="Courier New"][b]CameFromArchive[/b][/font] so that the database administrator would determine what [font="Courier New"][b]CtrlNo[/b][/font] came from the archive table (0-NO 1-YES)?Thank you so much and more power!Respectfully Yours,Mark Squall</description><pubDate>Mon, 14 Jan 2013 17:13:59 GMT</pubDate><dc:creator>marksquall</dc:creator></item><item><title>RE: How to combine two tables when one table is in different database and this table will determine the value to be shown on the first table.</title><link>http://www.sqlservercentral.com/Forums/Topic1406814-392-1.aspx</link><description>This is somewhat of a shot in the dark because we don't have ddl or sample data to work with.[code]SELECT        A.ConsInvNumber AS [Consolidated Invoice No],       A.InvTrackNo AS [Invoice TrackNo],       B.SaleOrderNo AS [Sales OrderNo],       B.OrderLine AS [Sales Order LineNo],       case when A.CtrlNo = 0 then osa.CtrlNo else a.CtrlNo end as CtrlNo	   FROM DBMain.dbo.ConsInvoiceDetail AS A  INNER JOIN     DBMain.dbo.IndInvDetail AS B ON A.InvTrackNo = B.InvTrackNoLeft Join (select top 1 CtrlNo from DBArchive.dbo.OrderSummaryArchive where SalesOrderNo = B.SaleOrderNo) osaWHERE A.ConsInvNumber=980029768[/code]I would suggest that when aliasing your tables you don't use A, B, C. Instead use some sort of abbreviation of the table name. It makes debugging a lot easier and anybody looking at your code doesn't have to scratch out their eyeballs trying to figure out which table certain columns come from.</description><pubDate>Mon, 14 Jan 2013 12:33:04 GMT</pubDate><dc:creator>Sean Lange</dc:creator></item><item><title>How to combine two tables when one table is in different database and this table will determine the value to be shown on the first table.</title><link>http://www.sqlservercentral.com/Forums/Topic1406814-392-1.aspx</link><description>Dear members,Hello, a pleasant day to everyone.I would want to ask help about this query that I need for reporting.Let’s for example I have a database named [font="Courier New"][color="#0000ff"]DBMain[/color][/font], and I want to combine two tables by means of their key named [font="Courier New"][color="#0000ff"]InvTrackNo[/color][/font], eventually here is the query:[code="sql"]SELECT        A.ConsInvNumber AS [Consolidated Invoice No],       A.InvTrackNo AS [Invoice TrackNo],       B.SaleOrderNo AS [Sales OrderNo],       B.OrderLine AS [Sales Order LineNo],       A.CtrlNo	   FROM DBMain.dbo.ConsInvoiceDetail AS A  INNER JOIN     DBMain.dbo.IndInvDetail AS B ON A.InvTrackNo = B.InvTrackNoWHERE A.ConsInvNumber=980029768[/code]And here is the output of my main query:[code="plain"]Consolidated InvoiceNo	    Invoice TrackNo	Sales OrderNo	   Sales Order LineNo	CtrlNo980029768	            0901164982	    	162194751		 100		407633405980029768		    0901164982	     	162194751		 200		407633405980029768		    0901164982	     	162194751		100		407633405980029768		    0901164982	    	162194751		200		407633405980029768		    0901192336	    	161891209	       40		0980029768		    0901192337		161891209		100		0980029768		    0901192338		161891209		160		0980029768	            0901204388		162044081		100		406488527980029768		    0901204389		162044081		200		406488589[/code]I noticed that some Control Number ([font="Courier New"]A.CtrlNo[/font]) are zeros, my superior said I should look for the archive table and somehow “combine” them to this first query.I tried to query first the archive table named [font="Courier New"][color="#0000ff"]OrderSummaryArchive[/color][/font] that is located in a different database named [font="Courier New"][color="#0000ff"]DBArchive[/color][/font]:[code="sql"]SELECT CtrlNo,          SalesOrderNo,          SalesOrderLineNo FROM OrderSummaryArchive WHERE SalesOrderNo = 161891209[/code]And here is the output of my second query:[code="other"]CtrlNo	  SalesOrderNo    SalesOrderLineNo405290666   161891209	       20[highlight="#FFFF00"]405290666   161891209	       40[/highlight]405290669   161891209          60405290669   161891209          80[highlight="#FFFF00"]405290669   161891209	      100[/highlight]405290674   161891209         120405290674   161891209	      140[highlight="#FFFF00"]405290674   161891209	      160[/highlight][/code]As you can see, the highlighted result will complete my first query, I just do not how how to combine them if the second table is in another database, and how to check the appropriate [font="Courier New"]CtrlNo[/font] for each record.My expected result would be:[code="other"]ConsInvoiceNo      InvoiceTrackNo	Sales OrderNo	   Sales Order LineNo	CtrlNo     CameFromArchive980029768	   0901164982	    162194751		       100		407633405        0980029768	   0901164982	    162194751		       200		407633405        0980029768	   0901164982	    162194751		       100		407633405        0980029768	   0901164982	    162194751		       200		407633405        0980029768	   0901192336	    161891209		       40		405290666        1980029768	   0901192337	    161891209		       100		405290669        1980029768	   0901192338	    161891209		       160		405290674        1980029768	   0901204388       162044081		       100		406488527        0980029768	   0901204389	    162044081		       200		406488589        0[/code]The report should include an additional column called [font="Courier New"]CameFromArchive[/font] so that the database administrator would determine what [font="Courier New"]CtrlNo[/font] came from the archive table (0-NO 1-YES).I hope someone could help me fix my first query.  Thank you.Respectfully Yours,Mark Squall</description><pubDate>Mon, 14 Jan 2013 09:36:50 GMT</pubDate><dc:creator>marksquall</dc:creator></item></channel></rss>