Inner Join On Two Separate Databases

May 30, 2008

I need to do a inner join on tables from two separate databases.  I understand that you can do this by using this type of syntax:

select a.col1, b.col2
from db1.dbo.tab1 a, db2.dbo.tab2 b
where a.col1 = b.col2

however, how do I reference the two databases in the following code?

Thanks,

Tim

 

Function GetConnectionString() As StringDim ConnectionString As String = ConfigurationManager.ConnectionStrings("MainWeb").ConnectionString

Return ConnectionString

End Function

 Using conn As New SqlConnection(GetConnectionString())

conn.Open()

Dim sql As String

sql = "SELECT CaskInfo.CaskID, CoCInfo.CoCName, AmendmentInfo.AmendmentName FROM CaskInfo INNER JOIN CoCInfo ON CoCInfo.CoCID = CaskInfo.CoC INNER JOIN AmendmentInfo ON AmendmentInfo.AmendmentID = CaskInfo.Amendment WHERE "For i = 0 To UBound(words)

If i > 0 Then sql = sql + " OR "

sql = sql + "(CoCInfo.CoCName + ' ' + AmendmentInfo.AmendmentName) LIKE '%" + words(i) + "%'"

Next

' lblResults.text = sql' Exit Sub

Dim com As SqlCommand = New SqlCommand(sql, conn)

Dim result As SqlDataReader = com.ExecuteReader()

Dim SearchResults As StringWhile result.Read()

SearchResults = SearchResults + result.GetInt32(0).ToString + " " + result.GetString(1) + " " + result.GetString(2) + "<br>"

End While

result.Close()

lblResults.text = SearchResults

conn.Close()

End Using

View 5 Replies


ADVERTISEMENT

Select From 2 Separate Databases

Jul 21, 2000

How do you select data from 2 separate databases residing on 2 servers?

Is there a way?

Angel

View 1 Replies View Related

Databases In Separate Folders?

Mar 15, 2006

In Enterprise Manager, is there a way to group Databases into Separate folders?

View 6 Replies View Related

How To Move Two Separate Databases Into One?

Nov 23, 2005

I have two databases with multiple tables. Same tables same fileds.Both databases contain records, but they should not match each other.(I will run a report for matches before this and delete any data thatis not current to ensure this is the case)How do I go about moving data from one of the databases to the other inorder to create a single database with all the data? Unfortunately mySQL is limited and I cannot image how I would work around the uniqueids. Append or update maybe?Thanks in advanceAndrew

View 1 Replies View Related

Selecting Count Of Records Among Two Separate Databases

Jan 11, 2007

Hey all,
I want to run a query that returns the count of records returned by two other queries. Having much trouble with this... I'm sure it's just a triviality. Thanks in advance...

View 2 Replies View Related

Separate Databases For High/low Transaction Volumes?

Jun 23, 2006

I have an existing database with approx 500,000 rows and accessed by afew hundred users per day creating approx 1,000 new records per dayplus typical reporting - relatively low volume stuff for SQL Server.I'm about to add a process that will be importing data daily fromlegacy databases and summarizing it for reporting purposes, integratingit with the existing database. This volume of data will be considerablyhigher, perhaps 100,000+ rows per day, which will be deleted once ithas been summarized and the results written to some intermediatetables.Is there any concern about mixing different levels of volume within onedatabase? As I'll be creating lots of rows daily and then deleting themI was wondering about fragmentation, transaction logging etc. andwhether having this processing in a separate database from the mainapplication would be 'better'.

View 3 Replies View Related

How To Access 2 Databases On Separate Servers From Within The Same Query

Apr 10, 2008

Hi,

Im trying to access data from a database on another server in a SQL 2005 query.


use Bury2k29.ServiceDeskForms
select .......


but I get the message


could not locate entry in sysdatabases for database 'Burky2k29'. No entry found with that name. Make sure that the name is entered correctly.


Bury2k29 is the name of the server, and ServiceDeskForms is the database I want to access.

When I open a blank query and enter only the code to access that database it runs fine.

Any ideas?

View 4 Replies View Related

Update Query Joining Tables From Separate Databases

Apr 17, 2008



In database DB1, I have table DB1.dbo.Suppliers1. This table has an ID column of type INT named SUPPL1_ID

In database DB2, I have table DB2.dbo.Suppliers2. This table has an ID column of type INT named SUPPL2_ID
I would like to update DB2.dbo.Suppliers2 based on values from DB1.dbo.Suppliers1 joining on SUPPL1_ID = SUPPL2_ID.

How can I do this in SSIS?

Assumptions:


linked servers are not an option, as I want the SSIS package to be portable and not dependent on server environments.
TIA.

-El Salsero

View 5 Replies View Related

Join Two Separate Select Statements Into One

May 15, 2008

I have two seperate tables that I need to get the data into one select statement. Here is the two separate select statements that I have created.

Select AGEGROUP, [YEAR], SUM(CASES) as Cases FROM tbHosp a
LEFT JOIN tbAGEGROUP b on a.AgeGroupID = b.AgeGroupID
LEFT JOIN tbYEARHOSP f on f.YEARID = a.YEARID
LEFT JOIN tbSEX g on g.SEXID = a.SEXID
where a.AgeGroupID in (1,2,3)
and a.YEARID in (1,2,3)
Group BY AgeGroup, [YEAR]
ORDER BY AgeGroup, [YEAR]

Select AGEGROUP, [YEAR], SUM(POPULATION) as [Population] FROM tbPopulation a
LEFT JOIN tbAGEGROUP b on a.AgeGroupID = b.AgeGroupID
LEFT JOIN tbYEARHOSP f on f.YEARID = a.YEARID
LEFT JOIN tbSEX g on g.SEXID = a.SEXID
where a.AgeGroupID in (1,2,3)
and a.YEARID in (1,2,3)
Group BY AgeGroup, [YEAR]
ORDER BY AgeGroup, [YEAR]

Results - First Select with tbHosp

AgeGroup YEAR CASES
<1 2001 9
<1 2002 32
<1 2003 10
1-4 2001 13
1-4 2002 11
1-4 2003 23
5-9 2001 13
5-9 2002 14
5-9 2003 34

Second Select with tbPopulation
AgeGroup YEAR POPULATION
<1 2001 40686
<1 2002 39768
<1 2003 40438
1-4 2001 174346
1-4 2002 170191
1-4 2003 167223
5-9 2001 247071
5-9 2002 242548
5-9 2003 237816


What I am trying to do is get one result set with Population and Cases together like the following

AgeGroup YEAR CASES POPULATION
<1 2001 9 40686
<1 2002 32 39768
<1 2003 10 40438
1-4 2001 13 174346
1-4 2002 11 170191
1-4 2003 23 167223
5-9 2001 13 247071
5-9 2002 14 242548
5-9 2003 34 237816

Any help would be greatly appreciated.

Thanks!

View 12 Replies View Related

Data Access :: Combining Tables Of 2 Separate Databases For ODBC Use

Apr 29, 2015

Currently we have one customer database containing various tables. As part of requirements for a new client, we need to manage their data in a totally separate database. The tables and structure are exactly the same but we would be loading data into a separate database.

I am looking for a way to combine tables with the same name in each database when I run queries, rather than having to query each database separately. Currently we actually have many queries set up in MS Access which use an ODBC link to query the data off SQL server. I am aware it is possible to apply a UNION SELECT in Access from 2 separate ODBC connections, but this is extremely slow.So my initial question is - is there a way to provide access to the tables from both databases over the same ODBC link? If this cannot be done over ODBC I guess we can consider more "modern" methods, but ideally we want to keep this in MS Access as that is where our existing queries are based. I was hoping that some kind of view can be treated as an ODBC connection.I mentioned ideally we want to keep the reporting queries in MS Access.

View 6 Replies View Related

SQL Server Admin 2014 :: Separate Transaction Log Files For Multiple Databases?

May 15, 2015

We have multiple databases on a single instance in an OLTP environment. I have my data files on a separate SAN LUN from my transaction log files (and a few NDFs split out onto additional LUNs). I was wondering if there is a performance benefit to putting each LDF file on its own LUN? Or at least my few busiest LDFs?

We are currently on 2012, but I'm having to put together specs for a 2014 installation and need to answer this question without having an environment in which I can benchmark different setups. I just want to hear whether or not others have done this (why or why not?).

View 3 Replies View Related

Generate A Separate Txt File For Each Account In A Table, Need To Join Tables To Get Details, And Specify Output File Name?

May 16, 2008

Hey,



I'm looking for a good way to rewrite my T-SQL code with 'bcp' to SSIS package, any help would be greatly appreciated?



I have table1 contain account numbers and output-filename for each account,

I need to join table2 and table3 to get data for each account in table1, and then export as a txt file.



Here is my code using bcp (bulk copy)

DECLARE @RowCnt int,
@TotalRows int,
@AccountNumber char(11),
@sql varchar(8000),
@date char(10),
@ArchPath varchar(500)

SET @RowCnt = 1
SET @date = CONVERT(CHAR(10),GETDATE(),110)
SET @ArchPath = '\D$EDATAWorkFoldersSendSendData'
SELECT @TotalRows = count(*) FROM table1
--select @ArchPath

WHILE (@RowCnt <= @TotalRows)
BEGIN
SELECT @AccountNumber = AccountNumber, @output_filename FROM table1 WHERE Identity_Number = @RowCnt
--PRINT @AccountNumber --test
SELECT @sql = N'bcp "SELECT h.HeaderText, d.RECORD FROM table2 d INNER JOIN table3 h ON d.HeaderID = h.HeaderID WHERE d.ccountNumber = '''
+ @AccountNumber+'''" queryout "'+@ArchPath+ @output_filename + '.txt" -T -c'
--PRINT @sql
EXEC master..xp_cmdshell @sql
SELECT @RowCnt = @RowCnt + 1
END

View 7 Replies View Related

Add 2 Separate Columns From Separate Tables Using Trigger

Feb 15, 2012

I am trying to add 2 separate columns from separate tables i.e column1 should be added to column 2 when inserted and I want to use a trigger but i don't know the syntax to use...

View 14 Replies View Related

Display Output On Separate Separate Line

Feb 10, 2007

How can i format my query so that each piece of data appears on a new separate line? Is there a command for a new line feed? does not work.

thanks.

For example:

a: data
b: data
c: data

a: data
b: data
c: data

View 6 Replies View Related

Join 2 Tables From 2 Different Databases

Feb 6, 2007

Hi All,
 Is it possible to join 2 tables from 2 different databases in Stored Procedure.
Thanks in advance.

View 2 Replies View Related

How Can I Join Two Tables From Two Different Databases?

Aug 3, 2007

hi,
i have one database ASPNETDB.MDF created by default when adding a user to my site, and MyData.mds - my database...i want to join the aspnet_Users table with another table created by me (in myData.mds), how can i do that? is hard if i should re-write all the data from myData into the ASPNETDB,i even writed both connectionStrings in the web.config but still with no succes...
is there any trick in the SQL statment? please help me
thank you

View 10 Replies View Related

Join Table From Different Databases?

Apr 16, 2008

I would like to join table in different databases with a primary key and query it. How to do ?

View 3 Replies View Related

SELECT ... JOIN On Two Databases In VWD (?)

May 26, 2006

My environment:XP Home, VWD, SQLEXPRESS.A purely local setting, no network, no remote servers.
I try to do a JOIN query between tables in the membership ASPNETDB.mdf and one table in a self created 3L_Daten.mdf.
After dragging the tables into the Query Design window and connecting them VWD creates this query (here I added the control declaration):
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:ConnectionStringAspNetDB %>"            SelectCommand="SELECT aspnet_Users.UserName,                                 aspnet_Membership.Password,                                 aspnet_Membership.Email,                                 aspnet_Membership.PasswordQuestion,                                  aspnet_Membership.PasswordAnswer,                                 aspnet_Membership.CreateDate,                                 aspnet_Membership.LastLoginDate,                                 aspnet_Roles.RoleName,                                 [D:VISUAL STUDIO 2005WEBSITES3L_V1APP_DATA3L_DATEN.MDF].dbo.Personendaten.Age,                                [D:VISUAL STUDIO 2005WEBSITES3L_V1APP_DATA3L_DATEN.MDF].dbo.Personendaten.Sex,                                [D:VISUAL STUDIO 2005WEBSITES3L_V1APP_DATA3L_DATEN.MDF].dbo.Personendaten.Area                            FROM [D:VISUAL STUDIO 2005WEBSITES3L_V1APP_DATA3L_DATEN.MDF].dbo.Personendaten                            INNER JOIN                                aspnet_Users                            ON                                 [D:VISUAL STUDIO 2005WEBSITES3L_V1APP_DATA3L_DATEN.MDF].dbo.Personendaten.User_ID = aspnet_Users.UserId                            LEFT OUTER JOIN                                aspnet_Roles                            INNER JOIN                                aspnet_UsersInRoles ON aspnet_Roles.RoleId = aspnet_UsersInRoles.RoleId                            ON                                 aspnet_Users.UserId = aspnet_UsersInRoles.UserId                            LEFT OUTER JOIN                                aspnet_Membership                            ON aspnet_Users.UserId = aspnet_Membership.UserId"></asp:SqlDataSource>
THIS WORKS, BUT:
As you can see the database 3L_Daten.mdf is inserted with its full path, which is not feasible for deployment reasons.
My question: How can I address both databases purely by their database names ? Both have been created within VWD and lie under App_Data.
(I tried almost everything, I practiced with the SQL Server 2005 Management Studio Express Edition, I tried linked servers, all without success).
Thank you for your consideration.
 
 

View 3 Replies View Related

Join Between Tables On Different Databases

Jan 22, 2004

Is it possible to make a join between two tables from different databases on same server? if yes, how can we do that?

And also if I want to make a join between tables on different databases on different server?how to do this. Please advise.

View 2 Replies View Related

How To Run A JOIN Between Databases On Two SQL Servers

Feb 23, 2008





Code Snippet
SELECT ReciptItems.acc_TopicCode, ReciptItems.acc_DetailCode, ReciptItems.acc_CTopicCode,
SUM(ReciptItems.TotalInputPrice + ReciptItems.TotalOutputPrice), a.MoeenName_L1
FROM ReciptItems LEFT OUTER JOIN
Acc_mydbname.dbo.Categories AS a ON ReciptItems.acc_TopicCode = a.TopicCode
GROUP BY ReciptItems.acc_TopicCode, ReciptItems.acc_DetailCode, ReciptItems.acc_CTopicCode, a.MoeenName_L1





How Replace Acc_mydbname with (SELECT AccountDBName FROM Config)

(SELECT AccountDBName FROM Config) ='Acc_mydbname_2008.dbo.'

View 6 Replies View Related

Join Tables From Two Databases

Mar 1, 2007

Say I have two database files, database1.sdf and database2.sdf, how can I make a select that joins tables from both?

I'm using C#.

Thanks!

View 1 Replies View Related

How Do I Join Tables From Different Databases (under Same Server Though)

May 12, 2006

Hey, I have two databases (db1 and db2) under the same server. How do I combine tables from both of them?I searched the forum and triedSELECT

View 3 Replies View Related

Join 2 Tables Exist In Two Databases

Nov 23, 2005

Hi,
I want to get the results of a query between 2 tables that exist in the same server but in 2 different databases. I want this query to be executed in a stored procedure (use the reults in a cursor), or if it's possible to be used in an ado dataset or a simple dataset in Delphi. Can this be done also if the two datases (MSSQL) exist in different servers??
Can anyone show an example of how to accomplish these tasks??

Best Regards,
Manolis Perrakis

View 9 Replies View Related

Query And Join Between 2 Databases On Same Server?

Nov 9, 2011

I am trying to query 2 SQL databases on the same server and inner joining together.

Databases: Goldmine_MIOA
Goldmine_GMBase

From the "Goldmine_MIOA" database I need to query contact1.* and from the "Goldmine_GMBase" database i need to query cal.

I would then like to inner join both these together.

View 12 Replies View Related

Join Tables In Same Instance Different Databases

Sep 28, 2015

I have a problem in that a database I have created required splitting into three databases. The records were similar but reporting and maintenance differed enough that splitting the data seemed the best option. So now I have three databases that are needing to be updated from one file and I am not sure how to do that. See the illustration below. The Machine table is a one to many to the Job table, the Job table is a one to one with the Run Data.

The first idea was to query the first database if the record was not found go to the next. If no match was found in any table drop it in a reject bucket. Then what about perhaps a lookup table with starting and ending Job ranges. I would query it to find the correct database then do another query to update the record. But then I could have some type of joining table. Not sure how that would look across databases.

The tables I am updating or adding new records to has the same fields, unique key across all the databases. I could make one big table which gets rid of the query to find the correct database to update but I still would have to connect this back to the Job table which brings me back to a join table or index table.

View 6 Replies View Related

SQL 2012 :: Compare And Join Databases

Feb 7, 2014

A customer has messed up while moving their databases. After working for a week they found that data is missing in the database.I have two backups, one from the old server and one from the new server today, they have been working in the new one for a week.

I need to compare these two databases and then update the new database with all data that is in the old one but not in the new database. Join the data in the two databases so to say. Both databases are from the same application so they use the same users, schema and so on.

View 9 Replies View Related

SSIS - How To Run A JOIN Between Databases On Two SQL Servers?

Jan 17, 2006

Hi All,

I need to run an Insert query which pulls data from a table located on server A database AA Table AAA conditional on (or JOINED with) Table BBB in database BB sever B. In SQL 2000 I would simply do the following:

From Server A:

sp_addlinkedserver B

INSERT dbo.ResultsTable

SELECT SourceTable.* FROM B.BB.dbo.BBB SourceTable

INNER JOIN A.AA.dbo.AAA ConditionTable ON SourceTable.RecID = ConditionTable.RecID

sp_dropserver B



What do I need to do to perform the same operation in SSIS world?

Thank you !

View 1 Replies View Related

How To Join Tables From Different Databases In SQL Select Statement?

Apr 30, 2008

I have a basic sql statement, where I have a usersID, and I want to joing that usersID to another table in another database to get the users first and last names.  How do I join across databases... each with a different connection string? 
 Here's what I want..
Select usersID from tableA in databaseA, and usersFirstName, usersLastName from table B in database B where the usersID from tableA = the usersID in tableb. 

View 6 Replies View Related

DB Design :: Join Two Table From Two Different Databases But Same Server

Nov 9, 2015

I am trying to join two table from two different databases.

Database 1 = Agent and Table = StatsĀ 
Database 2 = Amount and Table = SalesĀ 

The common field is Expr1 (table1) and Initials (table2)

View 7 Replies View Related

JOIN Multiple Tables From Multiple Databases

May 23, 2008

Hello,
I am in the progress of designing a new section of my database and was thinking of creating a hole new database instead of just creating tables inside the database.  My question is can you JOIN multiple tables in an SQL Statement from multiple databases.  Ie, In the Management program I have a database called 'Convention' and another one called 'Services', inside the two databases there are many tables.  Can I link say tblRegister from Convention to tblUser in Services?
Thanks

View 3 Replies View Related

Can Any One Tell Me The Difference Between Cross Join, Inner Join And Outer Join In Laymans Language

Apr 30, 2008

Hello

Can any one tell me the difference between Cross Join, inner join and outer join in laymans language

by just taking examples of two tables such as Customers and Customer Addresses


Thank You

View 1 Replies View Related

Linking Tables From Different Databases Or Querying From Multiple Databases

Dec 10, 2007

Dear Readers,Is it possible, like in Access, to link to tables in other SQL databases that are on the same server? I have a query that I originally had in Access that queered from multiply databases. It did this by having those other tables in the other databases linked to the database that had the query. 
 

View 3 Replies View Related

Integration Services :: How To Perform Left Restricted Join In Merge Join Transformation

May 22, 2015

I have two xml source and i need only left restricted data.

how can i perform left restricted join?

View 2 Replies View Related







Copyrights 2005-15 www.BigResource.com, All rights reserved