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
Related
I am new to using SQL Server from RStudio. I am connected to SQL Server from RStudio and the server has several different projects listed in the below image. For this work I am using odbc library. I am trying to retrieve the tables of a specific project(Project_3960). I have tried dbListTables(conn,"Project_3960") but this command retrieve the tables from all the projects listed in the below Picture. I just want to retrieve the table which are listed in dbo in Project_3690.
The first picture is from RStudio and the second picture is from SQL Management Studio to show the structure of the folders, in case for executing SQL Query.
Thanks
Click on the arrow to the left of the dbo object under Project_3690, and it should show you the tables you have access to. If it does not, then you have a permissions problem and will need to talk with the DBA. That allows you to see them via the GUI. In fact, if you don't already know the names of the tables you should be accessing (such as to follow my code below), then this is the easiest, as they are already filtering out the system and other tables that obscure what you need.
To see them in R code, then dbListTables(conn) will show you all tables, including the ones in the Connections pane I just described but also a lot of system and otherwise-internal tables that you don't need. On my SQL Server instance, it returns over 600 tables, so ... you may not want to do just that, but you can look for specific tables.
For example, if you know you should have tables Table_A and Table_B, then you could do
alltables <- dbListTables(conn)
grep("table_", alltables, value = TRUE, ignore.case = TRUE)
to see all of the table names with that string in its name.
If you do not see tables that you know you need to access, then it is likely that your connection code did not include the specific database, in which case you need something like:
conn <- dbConnect(odbc(), database="Project_3690", uid="...", pwd="...",
server="...", driver = "...")
(Most fields should already be in your connection code, don't use literal ... for your strings.)
One can use a system table to find the other tables:
DBI::dbGetQuery(conn, "select * from information_schema.tables where table_type = 'BASE TABLE' and table_schema = 'dbo'")
# TABLE_CATALOG TABLE_SCHEMA TABLE_NAME TABLE_TYPE
# 1 Project_3690 dbo Table_A BASE TABLE
# 2 Project_3690 dbo Table_B BASE TABLE
# 3 Project_3690 dbo Table_C BASE TABLE
(Notional output but representative of what it should look like.)
Its not quite direct to retrieve the data from SQL server using RStudio when you have different schemas and all are connected to the server. It is easy to view the connected Databases with schema in SQL Server Management Studio but not in RStudio. The easiest way while using Rodbc is to use dot(.) operator and its easy to retrieve the tables of a specific data base is by using "." with dbGetQuery function. I tried dbGetQuery(conn, "select * from project_3690.dbo.AE_ISD ") and it works perfectly fine.
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
I'm developing a ASP.NET web application that displays some SSRS reports. My application's database is located in a different server from my report's database, considering that reports consume data from both servers there's a linked server in report database server to application's server.
In one of dataset reports I have a query as bellow:
SELECT T.column1, T1.column2
FROM localTable T
JOIN [linkedServer].[databasename].tablename T1 ON T.id = T1.id
There's only 3 reports, so it was ok to replicate [linkedServer].[databasename] when i changed the application to another environment.
My question is: what's the best way to parameterize [linkedServer].[databasename] in order not to need replace all occurrences every time the server and/or database names change?
next to the query field in the dataset, there is a little fx which allows you to enter an expression for the query.
from your example the query would be
= "SELECT T.column1, T1.column2 FROM localTable T JOIN [" + Parameters!ReportParameter2.Value + "].[databasename].tablename T1 ON T.id = T1.id"
or sth in the like.
in a off topic note i would also recommend to never use the server.database.owner.table syntax, but openquery(server, 'query'). this is for performance reasons, as the server.database.owner.table will copy the whole table over and the openquery retrieves only the result of the query.
Following up on my earlier query , is there a way to specify the condition for the records that would be copied over using the column names on the same table? i.e. I want to copy all data from sandbox server to production server for all rows where COL_A in sandbox that do not exist in the production server. So the intended select query should be:
SELECT * FROM <Sandbox><TABLE_C> WHERE <Sandbox><TABLE_C>COL_A NOT EXISTS (SELECT <production>COL_A FROM <production>TABLE_C)
i.e. all the records from sandbox to production where a matching COL_A could not be found
I am not sure about Oracle specific syntax but something along these lines assuming that you are able to access them as linked servers -
INSERT INTO TABLE_C#prod_link
SELECT source.*
FROM TABLE_C source
LEFT JOIN TABLE_C#prod_link target
ON source.COL_A = target.COL_A
WHERE target.COL_A IS NULL
where prod_link is a database link
CREATE PUBLIC DATABASE LINK
prod_link
CONNECT TO
remote_username
IDENTIFIED BY
mypassword
USING 'tns_service_name';
I do not have an Oracle instance running that I can try this on but it should work
I successfully retrieved data from an already populated table of a live database using mvc3 and linq 2 SQL. The table is defined in the DataClasses1.dbml.
Now I have to retrieve data from other tables with the same identical structure of DataClasses1 but from different databases on the same SQL Server( DB1.Customers DB2.Customers ecc), and display them grouped by database name.
1) How can I do that without creating N DataClassesN.dbml ? I guess since it's the same table structure I can avoid doing it.
2) (Optional): How can I automatically retrieve data also from tables of new created databases?
3) (Not relevant): How can I define a strongly type view? Seems I can do it using EF but I cannot do it using LINQ 2 SQL.
I already thought of creating a view on the database with all the customers tables, but it seems it's a too heavy view!
I have a query that returns all the database names (Select name from master..syttables), is it useful?
Thanks in advance
You just pass a different connection string to the data context when you create it. If the databases are truly identical, including all the foreign key relationships, then just do something like:
var dc = new DataClasses1(db1connectionstring);
// Do your display of database 1 data
var dc2 = new DataClasses1(db2connectionstring);
// Do your display of database 2 data
I have no idea what you mean by #2. Data doesn't retrieve itself.
You can't obviously join results from 2 databases in SQL so you'd probably have to use 2 queries (one to each database) with one of them selecting into a new Entity of the other database and then join the results in memory using LINQ afterwards. So one query returns DB1.EntityName and the other returns DB2.EntityName but with a select mapping this to new DB1.EntityName entities and then join the two. It's not a pretty solution but is the best I can think of off the top of my head.
If you just want each database to have a set of results each then obvioulsy you can just return 2 result sets. Let me know if I misunderstood your question.