Is there a way to automatically join many tables in SQL Server? - sql-server

I just imported ~50 tables; each table has 2 common foreign keys (making each record unique). My goal is to setup a query that joins all these tables; I obviously don't want to have to this manually and was thinking about setting up a procedure that loops through all tables to dynamically build this query ... is this the best way or is there an obvious solution I'm not seeing? Thanks you

No there is not. You have to manually write the query to JOIN the tables.
You can also check Automatically Generate a Set of Join Filters Between Merge Articles (SQL Server Management Studio) but I am not sure if that is going to help.

Related

Is there a query to see all the references in a SQL Server table?

I want a simple way to find all the Foreign Keys of my table.
For example having a table Customers, I want to find the tables with a relationship to this table, I use to find relationships using the Diagram but it is too slow.
Desired result = Customers_Accounts, Customers_Cities, Customers_Properites, etc
Yes just select your table and press ALT + F1, at the bottom of the result set you will see all the references
The reason that the dependencies is slow is due to the number and complexity of queries that sql sever runs in order to show you a nice hierarchical structure.
There are some ways to get the data you need which vary depending on the version of sql and how much info you are looking for.
Taken from Microsoft Docs - View the Dependencies of a Table
USE AdventureWorks2012;
GO
SELECT * FROM sys.sql_expression_dependencies
WHERE referencing_id = OBJECT_ID(N'Production.vProductAndDescription');
GO
Obviously you'll need to change the AdventureWorks2012 to your database and Production.vProductAndDescription to your Customer schema and table name.
The above works for the latest sql server editions, for older editions you may need to refer to the following links:
MSSQL Tips - Different Ways to Find SQL Server Object Dependencies
MSSQL Tips - Listing SQL Server Object Dependencies

How automatically create SQL Server relationship diagram?

I am new to SQL Server. On the previous job I used working with Postgres and MySQL. But now I was faced with the task connected with SQL Server. And I discovered very strange thing in the DB with that I should work. There don't exist any relations!
Is it normal to SQL Server? How can I automatically connect tables according to their primary keys? Any other ideas?
An screenshot of the ER diagram:
Unless you are talking about creating hundreds of FKs you may be better off by just adding those relationships manually either through SSMS database diagram or through a sql script.
If the number of possible relations is really large or you expect to have to do this again in the future, you may want to look to SMO (SQL Management Objects) and either use Powershell or a small C# program to script out the tsql that would join those tables. But you would need to make sure that there is a repeatable pattern / naming convention between the columns and tables that you can leverage. For Example:
Table1
ID
Name
Table2
ID
Table1ID
Name
Here you could consider Table1ID as a FK referencing Table1.ID

Quick way to perform a fulltext-search on MS SQL Server

First of all: i don't need a full-text-search engine, i don't need full-text-search in my code. I have a database with ~2000 tables, and i need to find the table and column in which certain information is stored, for developing purposes. Is there any quick way (maybe an SQL Server Management Studio trick that i should know of) to do this? I think phpmyadmin provides such a feature for mysql dbs. At the moment i'm seriously thinking of dumping the database to an .sql file and use a text editor to search for the phrases i'm looking for.
Check the INFORMATION_SCHEMA. You can select on it - there is a table containing all the field names etc. and you can then do search on that one.
I don't see a way how to do it without dynamic SQL - get list of all tables and their columns from sys.tables and sys.columns (don't forget to add proper schema if you're using them), construct query that checks for the values you're trying to find and stores table and column name in temporary table, place all queries into (temp) table and finally cursor/loop over that table executing all queries.
PS. your idea of dumping everything into *.sql files should work as well, depends on the volume of data.

using ADO.NET to select from multiple VFP tables

I need to select from 2 VFP free tables on some different network locations.
I've managed to get both tables into a dataset as two DataTables and add a Relation for them.
Now I need to join them and the result to insert into a SQL server database.
Any ideas?
Use two different queries to load the required subset of rows and columns from each table, upload each table individually to temp SQL server tables (using SqlBulkUpload), and then run your query in SQL against the two temp tables to create the required table.
Use Microsoft Access to add them as linked tables. You can then read the data from .net as though it were native Access data. You might be able to create queries in Access that join the tables and then read the queries from .net too.

How do I join two tables from two different databases?

Is there any way to use a query and join two tables that is in two different database on the same server for DbVisualizer? I used the following for the SQL server
Select * from table union select * from datbase.dbo.table2
I tried this for the DbVisualizer, and it didnt work. How do I do this?
If the databases are in different servers you need to make sure that they are set up as linked servers.
Also be warned that the optimizer is relatively weak in this scenario, same server or not. The problem is that the statistics used for weighting costs of different operations aren't necessarily meaningful between different databases, especially at the point where the two databases will "intersect". So performance isn't what it could be.
If DBVisualizer supports views, manually setup a view of table2 in your database.
create view table2 as select * from database.dbo.table2
I dont think it can be done. I resolved the situation, by running a nightly data transfer to the SQL server. I do the union select from there...

Resources