July 5, 2021 at 9:44 am
Hi
I have 2 tables. How 2 relate 2 tables. I want to relate No with DocumentNo. Order Lines can have multiple rowsOrder. No is Primary KeyNoDocDateCustomerOrder Lines. DocumentNo + ItemNo is Primary keyDocumentNoItemNoQuantity
Thanks
July 5, 2021 at 3:08 pm
What part are you having problems with? You say you want to relate No with DocumentNo. This feels trivial to do as long as those 2 columns ARE actually related.
What I think would help is if you could:
1 - post DDL and sample data so we can see your tables
2 - post what you have tried that didn't work
3 - post your results
4 - post expected results
With what you posted, it sound like you are just trying to do:
SELECT *
FROM [dbo].[Table1] AS T1
JOIN [dbo].[Table2] AS T2 ON T1.No = T2.DocumentNo
BUT in the above, I am making a LOT of assumptions. I am assuming the JOIN type, I am assuming you want all columns, I am assuming you want all rows, I am assuming by "relate 2 tables" you mean you are wanting to SELECT the data, No and DocumentNo ARE actually relatable in their current form (ie don't need to CAST/CONVERT either of them or do any tweaks to them such as truncating any data prior to joining them), etc.
You could very well be talking about making a foreign key to relate the data in which case the SELECT query I gave above makes no sense.
The above is all just my opinion on what you should do.
As with all advice you find on a random internet forum - you shouldn't blindly follow it. Always test on a test server to see if there is negative side effects before making changes to live!
I recommend you NEVER run "random code" you found online on any system you care about UNLESS you understand and can verify the code OR you don't care if the code trashes your system.
July 5, 2021 at 8:55 pm
In other words, please read and heed the article at the first link in my signature line below. 😀 It'll really help us help you.
Brian... You've been around for quite a while... please feel free to add it to you signature line if you would find it convenient. Looking back at how long ago it was written, I should probably modernize it a bit although it still works just fine.
--Jeff Moden
Change is inevitable... Change for the better is not.
Viewing 3 posts - 1 through 2 (of 2 total)
You must be logged in to reply to this topic. Login to reply