After some advice if there is a simpler and more efficient way of what I'm about to do....
I have a table with product data in sqlserver then a frontend in asp.net c#, this has export to excel, txt file options and publish to API's.
now I need to build in that we hold certain fields like product description in a different format for certain customers,
so product table is like
PT_PRODUCT|PT_DESC |PT_SIZE
ABC123 |Super Cool Ice-Cream |small
but then for 'Customer 1' the product description needs to be 'Ice Cool Lollypop'
I was going to create a class for 'Product' in my application and fill that with the values from the main table,
then query a second table that would look like this,
CUST |PRODUCT | FIELD_ID | FIELD_VAL
CU1 |ABC123 |PT_DESC |Ice Cool Lollypop
and would run something like
select * from table2 where cust='CA1' and product='ABC123'
for(int i=0;i< ds.tables[0].rows.count;i++)
{
switch(ds.Tables[0].Rows[i]["FIELD_ID"])
{
case "PT_DESC":
ClassProd.DESC = ds.Tables[0].Rows[i]["FIELD_VAL");
//and so on updating the class
}
}
the use the updated class to update the customers site via the API or exporting to excel ect,
now for the slight curve-ball, there may be around 20+ fields that need to be overridden by the customers data, also going down this route I will be dictating the fields that can be overridden, so was wondering if there was a way of doing this in the original sql select.
You could create a stored procedure and forget about having to do any of the C# to get the customer's custom products.
This left joins the CustomerProducts table on the Product Id and the Customer Id. If it is NULL, it didn't find a customer product description so it will use the default one from the Products table. If it is NOT NULL, then it found a customer product description in CustomerProducts and uses that instead.
I don't know your schema exactly, but this is the gist:
CREATE PROCEDURE GetCustomerProducts
(
@CustomerId VARCHAR(255),
@ProductId VARCHAR(255)
)
AS
BEGIN
SELECT PRODUCT
,FIELD_ID
,CASE
WHEN cp.FIELD_VAL IS NOT NULL
THEN cp.FIELD_VAL
ELSE p.FIELD_VAL
END AS FIELD_VAL
FROM Products p
LEFT JOIN CustProducts cp ON cp.PT_PRODUCT = p.PT_PRODUCT
AND cp.CUST = @CustomerID
WHERE p.PT_PRODUCT = @ProductId
END
Then:
EXEC GetCustomerProducts @CustomerId = 'CU1', @ProductId = 'ABC123'
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.