Dynamically comparing two tables from two different databases and serves in SQL Server Management Studio - sql-server

I am working on a project, where a user will select a a table choice on a website. Once the table is selected, the website will then connect to the database and select the table from a server (Server A) under a database (ABC). The website will also have to choose the same table from a different server (Server B) under database (DEF). Also these tables will have the same name, they will have some different data entered into them.
Our goal is to come up with a dynamic SQL Query/Stored Procedure. Multiple table choices can be visible in the website and once the user picks an option, it DYNAMICALLY passes that information to the database to find the two tables and yield a final table that portrays the differences.
MY PROBLEM:
I am having a lot of problem with the syntax and doing this process dynamically. I have searched everywhere for a solution and am struggling with this for more than two weeks.
OUTLINE OF MY PLAN:
Find primary keys and the column names of all the columns in a selected table. Pass this information to a temporary table
Create a SQL query with something like :
SET #SQL = select table1.col1, table2.col1... inner join..
Take care of conditions where :
A. Data is present in one table but not the other
B. Data is present in both tables
C. What if there is no data in one or both tables.
I would really appreciate any help. I am very new to SQL and have been trying my hardest in this project for a while. Please help me and I will do my best to repay you. Thank you very much for your time.

Related

Is there a way to view the relationships of just one column in Oracle SQL Developer?

I´m quite new to the Oracle SQL Developer. I try to handle a big database and I need to define views. Therefore, I need the relationships of some tables. with the relational models of the Browser, I´m able to show ALL relationships of the table, which is quite confusing.
Is there a way to show the ralationships of just one column?
I want to pick the table and the column and I want to see to which other tables the column is connected.
Thank you.
My suggestion:
First find out in which tables the column name has been used :
select * from all_tab_columns where column_name='COLUMN_NAME';
And then you can see constraints defined for the respective columns in that tables

SQL Server - auto-populate field in one table with value from another table in another Database

I have two Azure SQL Server databases with the following as example:
Database Name: DataProp
Table Name: DataImports
Columns: SearchID, SourceID, Text, Status, Country
Database Name: Sources
Table Name: SourceInformation
Columns: SourceID, SourceTitle, Country
Right now, the Country column in the DataProp database is all NULL. I need to auto-populate the Country field in DataProp with the values of the Country fields in the Sources database. The common field between the two tables is SourceID. I need to do this for all existing data, as well as have it occur for future records.
What is the best way to accomplish this? A stored procedure that's set to run on a schedule? If so, I would appreciate guidance on the T-SQL syntax.
As a side-note, I looked at the possibility of a computed column, but this will not work for us b/c we maintain an Azure Search Index on our tables, and Azure Search can't index computed columns.
I don't think you'll be able to directly write a join between tables in two different DBs. We had a similar problem and decided to move all tables into a single DB in separate schemas. I think in your case you can write a Webjob to pull in data from one table and update the second table. I also found one article related to this but haven't personally tried, so not sure if it works.
https://ppolyzos.com/2016/07/30/cross-database-queries-in-azure-sql-databases/

MS Access link to SQL Server - validate input against 2nd table?

I'm trying to think of the easiest way for non-tech users to dump info into a database, without coding my own web application.
Essentially, they are recording subjective phone grading scores for employees.
I linked an Access form to our MS SQL Server database. The only validation I want it -- I want one field, 'employee' - to be validated against a list of employees from say table.employee on SQL Server.
Once the form is submitted it will be written to table.scorecard -- or what have you.
Is this possible in Access? Their standard validation rules don't seem to cover this. Also, is there simply a better way to accomplish this task in general? Thanks
There are two ways to solve this problem.
The simplest is to use a combobox field for your employee information. Use the employee table as the list data source for the combobox and then set the LimitToList property to true. This assumes that you have setup linked table connections for both your employee table and your 'scores' table.
The second solution is to create a foreign key between the employeeId (or whatever the key field is) in the scores table and the employee table on the SQL side. If someone tries to insert an invalid employee, you will get an insert error. Unfortunately, SQL errors tend to be very confusing to most Access users.
If you want to be very through, you could implement both solutions, this would prevent someone going straight to the linked tables and putting in bad data.
I just realized that I am assuming that you are doing proper relational design where the 'scores' table would contain the employeeId rather than a full name. The idea on the form is to have the combobox display the name, but insert the employeeid field.

query 2 tables with exact column names and types

I have two tables with identical column names and types, I would like to query all of the content from both tables into one result set, of just one set of column names. So tbl1.ID and tbl2.ID should be in one col.ID as tbl1.data and tbl2.data should be in one col.data. There are no common values between the tables, records are unrelated so nothing to JOIN on.
I am using vb.net to query an Access DB and update a SQL DB.
I believe in SQL I can use a SELECT INTO but I am not sure how to do this in access with one query, or do I need to create a table and just push everything into it first.
thanks,
In Access you can create a "Union Query". How this is done exactly depends on the version of Access you are using. Open that query in "SQL View" and then use the code from there in your VB application.

Tables Designs in SQL database

I am planning to move my access database to sql server using SSMA. I have a Column called Eligibility which have drop down list values as shown in Image. After Converting to sql I realized it doesn't have drop down list option. Can anybody suggest what will be the best solution of my situation? Either I can have any other option to design table in SQL which can hold List Values?
You can do one of the following:
Add CHECK constraint to Eligibility field allowing only a set of predifined values to be inserted into that field, as suggested in comment.
Better solution would be to create Eligiblity table (with id and value fields), and reference this table from main table by id field, possibly creating a PK-FK relationship. This way:
a) Only values from Eligibility table would be allowed. b) You could change and add entries in Elibility table without need to change constraint every time. c) A frontend application could use Elibility table to add drop-down functionality.
SQL Server does not work the same as access. It does not have dropdown option for you to choose from.
The proper way to implement dropdown option with SQL Server as database is to have another application as a front-end and let user access through the application. That way it is easier to manage security.

Resources