I am not sure if I worded the title right, but this is my issue.
I have a SQL database and a C# program that manipulates the database and lets the user add records and whatever else they need to do.
I have two tables, one for customers called tb_Customers
and one for products called tb_Products
.
Now customers have a unique ID that is set by the user when they create the customer account. It's not just like 1, 2, 3, 4 and so on - it's an 8 character string that combines letters and numbers. All the search boxes and lists that the program lets the user view that involve customers always go by LastName, FirstName.
The product ID is the same way. It's primary key in the tb_Products
table is 8 characters long with numbers and letters but has a product name that they can search. Its easier for the user to search and use like "Example Product" rather than EXPRO497.
I need to make a junction table for the sale records that ill call tb_Sales
or something that will have the date, cost, and all the basic sale info.
Here is the issue I am running into. When the user logs a sale, they aren't doing it with the CustomerID
(primary key on customer table), they are using the AccountName (FirstName, LastName) which is a column on the customer table.
The way the data is entered is actually on a datagridview that is unbound because there could be multiple records with multiple customers in one sale. Before anyone says anything about doing it this way, it has to be this way. It was either use a datagridview, or have to repeat the record entry upwards of 20 times in a row instead of in one batch kind of deal.
So this datagridview has 2 columns. It's the AccountName, which is a combobox list from SQL that gets the AccountName column from my customer table and then another column with the % of the sale that is being allocated to them.
When the user presses the button to send the records in the datagridview over to SQL, I cannot get it to reference the CustomerID associated with the AccountName without a ridiculously round about way of doing it. The productID will be the same for every record added and I don't have an issue with that.
The only way I have gotten it to work is by having the datagridview send its entire column of AccountName's over to a list and then the CustomerID and AccountName's again from SQL, then it just checks the list to see if those AccountName values are present and then if they are, makes a new list that basically replaces AccountName's with the CustomerID in the same order. then once it does that it sends it over. It's a really long command because it queries the entire client base when it might only need 3 records.
Is there a way to just say
CREATE PROCEDURE dbo.Whatever
@SalesID,
@CustomerID,
@ProductID
AS
BEGIN
INSERT INTO tb_Sales (SalesID, CustomerID, ProductID)
VALUES (@SalesID, @CustomerID, @ProductID)
WHERE CustomerID = //the CustomerID column in the customer table based on the AccountName?
END
Like how do you put in a where clause that is going to reference another table?
So the entire process would be
I am not really looking for how to add a bunch of records from the datagridview to the SQL server, but mainly how would I set up this stored procedure in SQL to switch out the AccountName with the CustomerID.
Sorry for the rambling, I am new to SQL and do not know what its called when you do this lol.
That was a lot of text to read! I won't be able to get the exact syntax, because I don't know your database. But from what you've said, I think your INSERT statement is going to look something like this:
INSERT INTO tb_Sales (SalesID, CustomerID, ProductID)
SELECT
SalesID, CustomerID, ProductID
FROM
tb_Customers c
WHERE
c.CustomerID = @CustomerID
This assumes that tb_Customers
has these three columns: SalesID, CustomerID, and ProductID.
Again, I'm flying blind here. But, keep in mind that an INSERT statement doesn't have to follow the format of...
INSERT INTO myTable
(Column1, Column2, Column3)
VALUES
("value1", "value2", "value3")
It can also look like this:
INSERT INTO myTable (Column1, Column2, Column3)
SELECT
ColumnX, ColumnY, ColumnZ
FROM
myOtherTable ot
WHERE
ot.ColumnX= @SomeValue
In the example above, you can comment out the INSERT
line and just run the SELECT
to make sure you are going to get the exact values you want.
I hope this helps?
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.