I currently find myself stuck in a situation where I have to union different tables from two different databases. The other database served as an archive for records which are no longer accessed but still needed for reports. I am developing an interface where they could view historical reports. I am using MS SQL Server. Is it possible to do this?
Any help would be highly appreciated...
you need to use three part naming convention
ex:
database name:A
Schema name:dbo
table name test
You can refer like
select top 10* from a.dbo.test
Similarly to union two different tables in two different databases,you can do like below
select top 10* from a.dbo.test
union
select top 10* from b.dbo.test
if those databases are in different server ( say server1 and server 2),you need to create linked server and you can access like below
select top 10* from server1.a.dbo.test
union
select top 10* from server2.a.dbo.test
Related
Currently I am developing an Access 2013 based Application for tracking evaluation information of the school students. The database contains only 3 simple tables:
tblSubjects = Contains different subject information
tblStudents = Contains student's personal information
tblMarks = Contains subject wise evaluation marks for each student
and few other queries based on these 3 tables. Now I have a (bit ugly) sql query like following:
SELECT tblStudents.*,
(SELECT COUNT(*) FROM qryPapers WHERE qryPapers.STUDID=tblStudents.STUDID) AS PAPER_COUNT,
(SELECT SUM(MR_TOTAL) FROM qryPapers WHERE qryPapers.STUDID=tblStudents.STUDID) AS ALL_TOTAL,
(SELECT MIN(MR_TOTAL) FROM qryPapers WHERE qryPapers.STUDID=tblStudents.STUDID AND qryPapers.PAPER_TYPE LIKE 'E?') AS MIN_ELEC,
(SELECT COUNT(*) FROM qryPapers WHERE qryPapers.STUDID=tblStudents.STUDID AND qryPapers.PAPER_TYPE LIKE 'A?') AS LANG_PS,
(SELECT COUNT(*) FROM qryPapers WHERE qryPapers.STUDID=tblStudents.STUDID AND qryPapers.PAPER_TYPE LIKE 'E?') AS ELCT_PS,
IIf([PAPER_COUNT]>5,ALL_TOTAL-MIN_ELEC,ALL_TOTAL) AS [GT],
IIf([LANG_PS]=2 And [ELCT_PS]>=3,'PASS','FAIL') AS STATUS
FROM tblStudents;
The Problem is, whenever I try to run a filter on the STATUS field of this query (Like When STATUS='PASS') the entire ACCESS is first STOPES RESPONDING! and then SHUTS DOWN and RESTARTS.
I have no idea what is going on here. I have seen far more complex queries running perfectly well, but not this one. Any help will be appreciated.
I've experienced this a lot in Access. I don't know the cause of the problem but I just export the query to Excel or create a table based on the query to apply filtering.
I have two tables vendors and customers. My problem is that I want to make the search of the name and in result I get the common data from both the tables(if any exist).
Example: If I search the name John and it exits in both the tables so in result I must get the gridviews of both the tables.
Your question is not quite clear for me. Are you looking for a SQL statement?
Try with:
SELECT * FROM Vendors LEFT JOIN Customers ON Vendors.Name = Customers.Name
or
SELECT * FROM Vendors WHERE Vendors.Name IN (SELECT Customers.Name FROM Customers)
I have a split Access 2010 database. Users have a copy of this database on their laptops and there is a main copy that resides on the I: drive server. Two tables exist for input on all copies: tblMedData and tblMyMedData. Users can perform a synchronization that moves the tblMyMedData data from their laptops into the tblMedData table on the I: drive server. tblMedData is then copied back to the tblMedData table on the laptop, so they have the latest data residing on their laptop.
The problem we're facing: if a change is made in the tblMedData table on the server, this change gets overwritten during the synchronization. During the synchronization, I've tried using a select query that checks for medications that exist in both the laptop tblMedData table and the server tblMedData table and if there are any differences between these records, but I can't figure out how to do this? Here's what I have so far:
SELECT tblMedData.* AS tblLaptopMeds, tblMedData.* AS tblServerMeds, tblMedData.Ratio,
tblMedData.Duration, tblMedData.Withdrawal, tblMedData.WaterOrInject, tblMedData.Deleted
FROM [C:\FolderName\DB.accdb].tblMedData AS tblLaptopMeds INNER JOIN
[I:\FolderName\Folder\DB_be.accdb].tblMedData AS tblServerMeds ON tblLaptopMeds.InvNo =
tblServerMeds.InvNo
WHERE (((tblLaptopMeds.Ratio)<>tblServerMeds!Ratio)) Or (((tblLaptopMeds.Duration)
<>tblServerMeds!Duration)) Or (((tblLaptopMeds.Withdrawal)<>tblServerMeds!Withdrawal))
Or (((tblLaptopMeds.WaterOrInject)<>tblServerMeds!WaterOrInject)) Or
(((tblLaptopMeds.Deleted)<>tblServerMeds!Deleted)) Or (((tblLaptopMeds.Ratio)
<>tblServerMeds!Ratio)) Or (((tblLaptopMeds.Duration)<>tblServerMeds!Duration)) Or
(((tblLaptopMeds.Withdrawal)<>tblServerMeds!Withdrawal)) Or
(((tblLaptopMeds.WaterOrInject)<>tblServerMeds!WaterOrInject)) Or
(((tblLaptopMeds.Deleted)<>tblServerMeds!Deleted)) OR
(((tblLaptopMeds.ChangedBy)<>tblServerMeds!ChangedBy));
Does anyone have suggestions? Am I making this too complicated?
Here is a snippet of searching for missing values:
select * from YourTable a
where a.YourIDField not in
(select b.YourIDFieldfrom YourTable b
where a.YourIDField= b.YourIDField)
I'm not sure if Not In will work in Access (esp if you're examining multiple fields), so try this if it doesn't:
select * from YourTable a
where a.YourIDField not exists
(select b.YourIDFieldfrom YourTable b
where a.YourIDField= b.Your
See this post for similar answer
I think if you look into what I posted along with Selecting from 2 databases on different servers you should be able to go figure it out fairly quickly.
I need to assign one of multiple parent types to a single child item. The problem I encounter is that in an Access 2010 web database I cannot create a Union query to bring all the potential parents (from multiple tables) into a single drop down / listbox.
I'm a bit green to all this and could be going about it completely wrong. I'm very open to suggestions. Here is my example:
Contracts are the parent of Subcontracts.
Both Contracts and Subcontracts have a Statement of Work (SoW).
Contracts and Subcontracts can both be direct parents of a SoW.
Each SoW will have only one parent
SoWs are split into paragraphs (not overly consequential)
With a union query I would build the database this way:
Contracts table
Subcontracts table
Union table for contracts and subcontracts
Lookup to union table from SoW table in order to select either a contract or a subcontract as parent from a single data source.
The problem here is that I cannot create a union query in a web database.
My only other thought is to construct the database in this fashion:
Contracts table
Subcontracts table
Contracts SoW table
Subcontracts SoW table
This design (using two tables) might work more effectively for data entry as there could be issues with subforms when attempting to use a union table. I'm not sure as I haven't yet tried. With this method, the Access report should be able to bind the subcontract to the parent contract and display all data in a detail section. However, this design still means that I will use two separate tables to house identical data.
I would put the two contract tables together into one table that would look something like this:
CREATE TABLE ContractTable(
ContactID INTEGER NOT NULL PRIMARY KEY, -- Possibly an autonumber
[various contract columns],
ParentContract INTEGER
);
Note, I know this is not Access friendly syntax. I usually use bigger DBs, but you should be able to get the idea.
Then your query to find parent contracts is SELECT ... FROM ContractTable WHERE ParentContract IS NULL.
To find sub contracts SELECT ... FROM ContractTable WHERE ParentContract IS NOT NULL.
My concern with this approach is that if you need to search through chains of contracts (i.e. A parent of B parent of C parent of D, and you need to go from A to D), you could run into recursive SQL which I don't think Access can handle. You'd have to do it VBA code.
I want to retrieve some data to use it in my grid, but the problem is my database design contains two databases.
So, can I retrieve data from more than database, to use it at one grid ?
I use ASP.Net, C#.Net 4, Microsoft SQL Server 2008 R2
SQL Server knows 4 part object names. The first part is the (linked) server name. The second part is the database name. third and fourth are schema and object name.
Assuming your databases are on the same server you can just write a query like:
SELECT *
FROM Database1.dbo.Table1 t1
JOIN Database2.dbo.Table2 t2
ON t1.column = t2.column;
There are several ways depending on your architecture.
If your databases are deployed in the same machine you can select data from two different databases like this:
SELECT a.userID, b.usersFirstName, b.usersLastName
FROM databaseA.dbo.TableA a
inner join database B.dbo.TableB b ON a.userID=b.userID
Of course you should have permission in both databases.
You can check here for more information: http://forums.asp.net/t/1254974.aspx/1