April 13, 2020 at 7:26 pm
Thanks So Much For Your Help. I was wondering what will this stored procedure return if I the SSN doesn't exist in any table. Also I was wondering what if I wanted a column for the Table Name is that possible? Thanks !!!
April 13, 2020 at 7:29 pm
So, from the output in the file, it appears that more than one SSN can be input to the stored procedure. That contradicts everything you have said in this post and others. So what are you trying to get in the end?
April 13, 2020 at 7:55 pm
I'm sorry that was a mistake. I was just focused on showing the multiple rows. But yes there will only be one ssn per result set. Again I greatly apologize.
April 13, 2020 at 9:33 pm
Thanks So Much For Your Help. I was wondering what will this stored procedure return if I the SSN doesn't exist in any table. Also I was wondering what if I wanted a column for the Table Name is that possible? Thanks !!!
The solution proposed by Jonathan Roberts gives you the table name in its own column. Is there something about this solution which you do not like?
What do you want to be returned if the SSN is not found in any table?
The absence of evidence is not evidence of absence.
Martin Rees
You can lead a horse to water, but a pencil must be lead.
Stan Laurel
April 13, 2020 at 9:57 pm
Thanks for the great example. What if I wanted to display the SSN's with the Table Names ?
April 14, 2020 at 10:11 am
Thanks Sir !!! I could display "No Records Found"
April 14, 2020 at 12:34 pm
Thanks Sir !!! I could display "No Records Found"
Borrowing from the script posted earlier, you can do this by adding an extra code block near the end:
/* Put an insert for each table */
INSERT INTO #temp
(
TableName
)
SELECT 'dbo.MyTable35'
WHERE EXISTS
(
SELECT * FROM dbo.MyTable35 x WHERE x.SSN = @SSN
);
--Handle the case where there are zero rows in the temp table
IF NOT EXISTS (SELECT * FROM #temp t)
INSERT #temp
(
TableName
)
VALUES
('No results found');
SELECT *
FROM #temp;
(Note also that SQL Server tables contain 'rows' of data, not 'records'.)
The absence of evidence is not evidence of absence.
Martin Rees
You can lead a horse to water, but a pencil must be lead.
Stan Laurel
April 20, 2020 at 2:52 pm
I shouldnt have a problem joining the tables but my main concern was when i get to the end and write the where filter clause do I have to ask where the SSN number is in all of the tables. Thanks !!!
I'm confused. Do you intend to join the individual datasets in your c# mvc code using linq or something, or are you asking a rookie question about how to do joins in T-SQL?
If it's the latter, then we normally specify join conditions in the ON section:
SELECT a.SSN, a.someOtherColumn, b.someColumn, c.someOtherColumn
FROM dbo.FirstTable AS a
INNER JOIN dbo.SecondTable AS b ON b.SSN=a.SSN
LEFT JOIN dbo.ThirdTable AS c ON c.SSN=a.SSN
WHERE a.SSN = @pSSN
As you can see, the WHERE clause only contain one SSN column reference, since the joins take care of the other row selection criterias with the same SSN from the other joined tables.
April 20, 2020 at 2:59 pm
I shouldnt have a problem joining the tables but my main concern was when i get to the end and write the where filter clause do I have to ask where the SSN number is in all of the tables. Thanks !!!
I'm confused. Do you intend to join the individual datasets in your c# mvc code using linq or something, or are you asking a rookie question about how to do joins in T-SQL?
If it's the latter, then we normally specify join conditions in the ON section:
SELECT a.SSN, a.someOtherColumn, b.someColumn, c.someOtherColumn
FROM dbo.FirstTable AS a
INNER JOIN dbo.SecondTable AS b ON b.SSN=a.SSN
LEFT JOIN dbo.ThirdTable AS c ON c.SSN=a.SSN
WHERE a.SSN = @pSSNAs you can see, the WHERE clause only contain one SSN column reference, since the joins take care of the other row selection criterias with the same SSN from the other joined tables.
This would return NULL if the SSN existed only in the third table (for example). Not what the OP requires, I believe.
The absence of evidence is not evidence of absence.
Martin Rees
You can lead a horse to water, but a pencil must be lead.
Stan Laurel
April 20, 2020 at 3:37 pm
This would return NULL if the SSN existed only in the third table (for example). Not what the OP requires, I believe.
Yes, well that's the question, isn't it? I wouldn't presume to know that (even after all that is said and done).
Because we don't know anything about the data, I just made up a simple demo query. The OP would have to come up with his own query based on his knowledge of the data. One of the tables is most likely a SSN master table. If we call that table FirstTable, I'm home free (maybe). 🙂
April 20, 2020 at 3:44 pm
@DataRat and to all those helping him... please raise your hand if you wouldn't be upset if it was YOUR plain text SSN that was splattered across 35 tables even on a supposedly "in-house only" database. 🙁 This is just wrong and so is helping someone that has such a thing. You're just helping someone perpetuate the problem.
--Jeff Moden
Change is inevitable... Change for the better is not.
April 20, 2020 at 4:08 pm
I don't know that we know that it's Social Security Numbers in clear text, do we? We only know that the name of the column is SSN, I think. It could theoretically contain anything. 🙂 Including a properly scrambled Social Security Number.
But, sure, it would be a very bad design if it does indeed use clear-text SSN information as primary and foreign key!
Viewing 15 posts - 16 through 30 (of 30 total)
You must be logged in to reply to this topic. Login to reply