Pulling data only when it matches a column in another table - sql-server

I'm new to the T-SQL language as my office is now using the Microsoft SQL Server Management Studio.
I am trying to pull all of the info from a large table but only when the item number shows in 1 column in another table. I have a small subset of items and I need to pull all the info from another that is more robust.
The small set table is named Itemmaster (IM) and has the following columns:
IM.item number
IM.description
IM.manuf
IM.item_Code
The second table, named Item_Directory (ID), has all of the info about the items including items with the same item codes. I want to pull all of the data from the Item_Directory where:
ID.item_Code = IM.Item_Code
No matter how I "think" it should be written, I seem to be wrong. I know this will probably be a simple formula but I'm still learning T-SQL.
My previous employer used Oracle and that just seemed easier for me to learn. Of course I am completely self taught so forgive me if I don't seem to know some of the basics.
Thanks for the suggestions.

select IM.item number,
IM.description,
IM.manuf,
IM.item_Code,
ID.*
From itemmaster IM
inner join item_details ID
on ID.item_Code = IM.Item_Code

Related

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.

Planning out basic TSQL to manipulate table data

Can someone help me get into the thinking of knowing how to fix data in SQL tables (by trying NOT to give me an SQL routines I could run).
Ok, this is the situation…. Suppose I have a single table with has a column called ColumnA which has lots of duplicate values. I need to remove all the duplicate entries from the table in question. Question is….if I had to write pseudo-code as a plan, what SQL should be written
Many thanks to anyone who can offer me any pointers.
Kind Regards
James
I think you've already articulated a very basic psuedocode for the issue you describe in stating that you wish to delete duplicate values from column A.
For this example, I would tend to;
Find all instances of duplicates
Work out method of determining which one to keep (Google "MAX N in
Group" for ideas) There are good articles here on SO and DBA
Stackexchange also other external articles with examples
Write your delete to cater for the records you identify as unwanted
duplicates in the previous step
For me, when working through these types of issues in SQL Server, I tend to write a series of Common Table Expressions (CTE) to identify my target records and then delete based on that.
For example;
;WITH Duplicates AS (
-- Write your select query to identify which subset of your records are affected by having duplicate values in Column A
), TargetRows AS (
-- Further select from Duplicates some method of MAX N in Group to identify which of the rows are unwanted
) -- Then here DELETE from your table based upon your findings from above

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

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.

cakephp linked data HABTM by JOIN need both related data?

This should be a simple Yes/No answer so here goes.
If I set a 3 tables, 2 typical recordsets and 1 that joins them by the id of the 2 tables, do I need the id from both tables in order to have an entry in the join table?
The scenario is a Jobs table and a Parts table linked by JobsParts table. But some parts are not in the Parts table, they are just freetext entries (so as to avoid stock control issues) belonging to a Job.
Hope this is enough to explain my question.
Thanks
BTW using CakePHP 2.0
For database sanity, I'd say the join table 'jobs_parts' should have both IDs.
If you try entering free-form parts into the join table, you're not only going to increase the size of the join table, but you've effectively lost the ability to grow/expand - ie. what if you want to add a few more fields to this unknown part? Or what if it turns out to be a part that you actually want in your normal parts table.... it just gets confusing.
There are other options for dealing with free-form parts vs actual parts...
have a field in the parts table that's a tinyint(1) for whether or not it's a verified part
OR make an UnknownParts model/table
In my opinion, go with what makes logical sense for ease of understanding and for future updates to your database/website...etc. And IMO, adding a freeform part into the join table would not fit that bill.

access: how to add a workorder to a customer

I have a noob question but he, i'm learning :-)
I'm making a form with the following tables 1 tblCustomers and 1 tblWorkorders.
My question is:
When I add a customer to a new record, this person is stored in the table: tblCustomers this is going fine.
The problem is that I also have a table: tblWorkorders, in this table I store all the technical information, sollutions and the customers belongings. (adapter, notebook bag etc etc)
My problem excists when for example a customer named John Doe comes back with another problem 2 weeks later. In the table tblWorkorders should be 2 records with the problems of John Doe I think it has something to do with relationships between the tables, can someone tell me where to find a good example or when it's a short story, how to do this?
Very difficult to explain this concept and start you off from scratch. Be prepared for further research on different item. Here is a place to start: http://office.microsoft.com/en-us/access-help/guide-to-table-relationships-HA010120534.aspx
The following is how you would use your tables:
You need to have a common field in both tables (it can be more than one field, but let's keep it simple). The easy way is to have a CustomerID field that is a Data Type field set to: AutoNumber (It does just what it says.).
tblWorkOrders will have the same field (doesn't have to be the same name, but let's keep it simple) BUT, the Data Type is: Number Field Size: Long Interger.
If you're able to use: Database Tools | Relationships, and join the two tables by this field, developing forms and reports is a lot easier.
Your form will be based on the tblCustomers table (I know, let's keep it simple.) and a Sub Form will use the tblWorkorders table and the 'Link Master Fields' and the 'Link Child Fields' will use the CustomerID from each table.

Resources