Maybe it's just my design of the database that is driving me nuts.That, and the unfamiliarity with the join statements, and trying to figure out how to select certain things from certain tables and update/delete certain things from certain tables.
Follow along with the video below to see how to install our site as a web app on your home screen.
Note: This feature may not be available in some browsers.
Maybe it's just my design of the database that is driving me nuts.That, and the unfamiliarity with the join statements, and trying to figure out how to select certain things from certain tables and update/delete certain things from certain tables.
UPDATE
USERS
SET
FullName = 'John Doe',
SpouseName = 'Jane Doe'
genDate = GetDate()
I guess the thing that really confuses me the most is when information is added/deleted/modified when you are dealing with Keys. For example, if I add a new User to your setup, the way the information (idUser) that gets added into REFERRAL_ADVISOR is through the join statements, right? The same would apply to deleting and modifying?
set ANSI_NULLS ON
set QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[addUser]
@FullName VARCHAR(50),
@SpouseName VARCHAR(50)
AS
INSERT INTO USERS
(FullName, SpouseName, genDate)
VALUES
(@FullName,@SpouseName. GetDate()) --GetDate() gets the current datetime
I'm also going to have to figure out how to create a table like REFERRAL_ADVISOR. Yea, it has 1 value of idReferralAdvisor, but how do you add those foreign keys? You add them into the table as another data type, and link them? Am I over thinking this? (I just found the details of each item in the table (thank you so much again). I'm really going to study this and use it as a good reference when reading my textbook.
ALTER TABLE USER_ADDRESS
ADD CONSTRAINT FK_USER
FOREIGN KEY (idUser)
REFERENCES USERS (idUser)
ON DELETE CASCADE
SqlCommand cmd = new SqlCommand();
cmd.CommandText = "addUser"; // SPROC Name
cmd.Parameters.AddWithValue("@UserName", "John Doe");
cmd.Parameters.AddWithValue("@SpouseName", "Jane Doe");
cmd.CommandType = CommandType.StoredProcedure;
cmd.Connection = m_conn; //your connection
SqlDataAdapter da = new SqlDataAdapter();
da.SelectCommand = cmd;
DataSet ds = new DataSet();
try
{
m_conn.Open();
da.Fill(ds);
//alternately: skip the dataadapter and dataset
// int intIdUser = Convert.ToInt32(cmd.ExecuteScalar().ToString());
//ExecuteScalar just gets the very first item returned by your query
//and disposes of everything else. It's much faster / lower overhead
//than a dataadapter...
}
catch (Exception ex)
{
throw ex; //to be handled higher up the stack
//or Console.WriteLine(ex.Message.ToString());
}
finally
{
//best practice to close your connection and
//dispose of your dataadapter in the finally block
//in case of exception!
m_conn.Close();
da.Dispose();
}
int intNewIdUser = Convert.ToInt32(ds.Tables[0].Rows[0][0].ToString());
//run some more code with your new idUser - like add an address for them
SELECT USERS.FullName, USERS.idUser, USERS.Type
WHERE USERS.Type = "Customer" AS [Customer Name]
AND USERS.Type = "Referrer" AS [Referrer Name]
That's what I'm looking to do, but that obviously doesn't work.
Ideas?
SELECT
U1.idUser AS [Customer idUser],
U1.FullName AS [Customer Name],
U2.idUser AS [Referrer idUser],
U2.FullName AS [Referrer Name]
FROM USERS U
--Note! I did two things in this join. I only joined to the second USERS (U2) where the type is referrer!
INNER JOIN USERS U2 on U2.idUser = U1.idUser AND U2.Type = "Referrer"
--Note! I only use WHERE on the first USERS (U1) to limit it to customers.
WHERE
U1.Type = "Customer"
Msg 4104, Level 16, State 1, Line 1
The multi-part identifier "U1.idUser" could not be bound.
Msg 207, Level 16, State 1, Line 8
Invalid column name 'Referrer'.
Msg 4104, Level 16, State 1, Line 8
The multi-part identifier "U1.Type" could not be bound.
Msg 207, Level 16, State 1, Line 11
Invalid column name 'Customer'.
Msg 4104, Level 16, State 1, Line 11
The multi-part identifier "U1.idUser" could not be bound.
Msg 4104, Level 16, State 1, Line 11
The multi-part identifier "U1.FullName" could not be bound.
I think I understand how the U1 and U2 work, but I don't see how that can be implemented in here.
What is the U and UR that you are writing? In the first one, it seemed to be a typo, but now I see it again.
USERS
FullName
SpouseName
Type
genDate
I've current got 2 entries in the USERS table. One that has a type of Customer, another that has a Type of Referrer. Take a look here about how I want to have the entries of both customer and referrer to be in the same table (actually, I'll need the 3rd type, Advisor as well).
Can you have multiple select statements in 1 query?
SELECT
U1.idUser AS [idUser from the first USERS table],
U1.FullName AS [FullName from the first USERS table],
U2.idUser AS [idUser from the second USERS table],
U2.FullName AS [FullName from the second USERS table]
FROM USERS U
INNER JOIN USERS U2 on U2.idUser = U1.idUser
-- BAD! This will throw several ambiguous column errors
SELECT
idUser AS [idUser from the first USERS table],
FullName AS [FullName from the first USERS table],
idUser AS [idUser from the second USERS table],
FullName AS [FullName from the second USERS table]
FROM USERS
INNER JOIN USERS on USERS.idUser = USERS.idUser
SELECT
U2.idUser AS [idUser Of Referrer],
U2.FullName AS [FullName Of Referrer]
FROM
USERS U1
INNER JOIN USERS U2 ON U2.idUser = U1.idUser and U2.Type = 'Referrer'
WHERE U1.idUser = @idUser -- replace this with the idUser of the CUSTOMER
ORDER by genDate ASC
Whoops. I totally forgot to include idUser in the USERS table, sorry. And I didn't get rid of the user_referrals table. I simply added type to USERS to identify what Type of user each entry is.
My question is...Look at the screenshot that I posted (the same one you did too). Notice that there are column headings on Customer Name, Referrer Name, and Advisor name. All 3 of these names reside within the USERS table.
Gotta dive into something else quick before I get to this, so I'll be back here soon I'm sure.
I've been working with it in Management Studio already.
You say to replace the @idUser with the idUser of the customer. What if I want to display ALL of the customers, instead of just one? The way I see what you are saying, is that I replace that @idUser with 1,2,3,etc whatever the id is for customers.
Even when placing a 1 there (id = 1 is a customer right now), I get an error:
Msg 209, Level 16, State 1, Line 8
Ambiguous column name 'genDate'.
It gives me that regardless of what I put in there. If I disregard that ORDER BY clause, the column headings show, but no data is filled. (and just so you know, I'm not waiting for a response. I am playing around looking for examples to reference in between your posts)
SELECT
U1.idUser AS [idUser Of Referrer],
U1.FullName AS [FullName Of Referrer],
U2.idUser AS [idUser Of Customer],
U2.FullName AS [FullName Of Customer]
FROM
USERS U1, U2
INNER JOIN USERS U2 ON U2.idUser = U1.idUser and U2.Type = 'Referrer'
INNER JOIN USERS U1 ON U1.idUser = U2.idUser and U1.Type = 'Customer'
ORDER by U1.genDate ASC
SELECT
U1.idUser AS [IdUser from tbl1],
U1.FullName AS [FullName from tbl1],
U2.idUser AS [idUser from tbl2],
U2.FullName AS [FullName from tbl2]
FROM USERS U1
INNER JOIN USERS U2 on U2.idUser = U1.idUser
Everything is displaying twice in both columns...Gahhh, this stuff is really frustrating.idtbl1 -------name table1 -------------idtbl2----------name table2
1--------------Joe Customer---------------1-------------Joe Customer
2--------------Josh Referrer----------------2-------------Josh Referrer
I went back to your example a few posts back, and changed it to meet what I have...
and it worked! Edit: Just 1 problem though. Each row is showing the information two times. It's not actually working how I want it to be, but the data is displaying. Screenshot comingNow to figure out getting advisor in here...
After I get Advisor working, what would you recommend to add in all of the other fields? Should I make another SELECT query for those remaining fields?
Are you manually populating the combo boxes or using the drag/drop designer method?
Why does "To get Referrers and To get Advisors" display each column twice? idUser, Name, idUser, Name.
Nope. Copy/Paste
Edit: I'd like your take on what you think it is I'm trying to accomplish. I just want to make sure that we are on the same page with things.
You wouldn't learn nearly as much if I did thatBy gnashing your teeth now, you'll become a very valuable programmer later
![]()
If you only want the referrer, just keep the selects from U2. But without the idUser from U1 (the customer), how will you connect the user to the referrer? The two places you can do this are in the WHERE idUser = 1234 or by selecting all idusers....
idUser FullName Type
1 Joe Customer
2 Jane Referrer
idUser FullName Type
1 Joe Customer
2 Jane Referrer
idUser FullName Type
1 Joe Customer
2 Jane Referrer
idUser FullName Type idUser FullName Type
1 Joe Customer 1 Joe Customer
1 Joe Customer 2 Jane Referrer
2 Jane Referrer 1 Joe Customer
2 Jane Referrer 2 Jane Referrer
idUser FullName Type idUser FullName Type
1 Joe Customer 2 Jane Referrer
SELECT *
FROM USERS U1
CROSS JOIN USERS U2
CROSS JOIN USERS U3
WHERE U1.Type = 'Customer' AND U2.Type = 'Referrer' AND U3.Type = 'Advisor'
Thank you so much. For some reason, today I just cant wrap my head around anything. That makes 100% on what you did and why it works the way it does.
There never was a type in the referrer table. I guess there was a link to the referrer's in the users table, and I somehow must have over looked that til now.
What do you mean by expensive? Resources wise to process that join?
idUser idRelatedUser RelationshipType
1 2 REFERRER
SELECT * FROM USERS U1
INNER JOIN USERS_RELATED UR --ALIAS USERS_RELATED TO UR
ON U1.idUser = U2.idUser --Immediately pare down to only U1.idUser relationships
AND U2.RelationshipType = 'REFERRER' --Further whittle down to only Referrers.
SELECT * FROM USERS U1
INNER JOIN USERS_RELATED UR ON U1.idUser = U2.idUser AND U2.RelationshipType = 'REFERRER'
Thank you so much. For some reason, today I just cant wrap my head around anything. That makes 100% on what you did and why it works the way it does.
There never was a type in the referrer table. I guess there was a link to the referrer's in the users table, and I somehow must have over looked that til now.
What do you mean by expensive? Resources wise to process that join? I need to look a bit more into joins and what exactly they are comparing.
Edit:
....and now I have my advisor information too...
Code:SELECT * FROM USERS U1 CROSS JOIN USERS U2 CROSS JOIN USERS U3 WHERE U1.Type = 'Customer' AND U2.Type = 'Referrer' AND U3.Type = 'Advisor'
Forgive me for being a bit incompetent in the field. I'm not even half way through my database class and we are just getting to data normalization (past modeling, just before sql).