1

I have two columns named 'Name' in two different tables. One is in the Store table the other is in the Product table. How do I differentiate which one is read?

The data is taken from 4 different tables using left joins as shown in the query below.

var conn = new SqlConnection("serverinfo");
SqlCommand query = new SqlCommand("SELECT Store.StoreID, Store.Name, 
Product.Name, " +
" StockRequest.Quantity, StoreInventory.StockLevel from Store" +
" LEFT JOIN StoreInventory ON StoreInventory.StoreID = Store.StoreID" 
+ " LEFT JOIN Product ON StoreInventory.ProductID = 
Product.ProductID" + " LEFT JOIN StockRequest ON StockRequest.StoreID 
= Store.StoreID", conn);

SqlDataReader read;

        try {
            conn.Open();
            read = query.ExecuteReader();
            while (read.Read()) {
                Console.WriteLine("{0} {1} {2} {3} {4}",
                                  read["StoreID"],
                                  read["Name"],
                                  read["Name"],
                                  read["Quantity"],
                                  read["StockLevel"]);

            }
            conn.Close();
            } catch (Exception e) {}

The table names are Store, Product, StockRequest, StoreInventory.

Right now the data for both of the 'Name' columns are taken from the Store table. I am unable to take the 'Name' data from the Product table.

7
  • ain't u reading the data from one table ? Commented Mar 24, 2018 at 23:49
  • and what are the table names ??ur SqlCommand doesn't include any table name ! Commented Mar 24, 2018 at 23:50
  • Why not use another SqlCommand and read the tables that contain Name separately ? Commented Mar 24, 2018 at 23:54
  • 2
    Give a alias to duplicate column using 'as'; one simple way to solve if everything else is working for you. Commented Mar 24, 2018 at 23:57
  • 1
    Rename the columns like "SELECT Store.StoreID, Store.Name AS StoreName, Product.Name AS ProductName, …". Commented Mar 24, 2018 at 23:58

1 Answer 1

3

You could use alias to give a different name to the duplicate column.

SqlCommand query = new SqlCommand("SELECT Store.StoreID, 
"Store.Name as StoreName," + 
"Product.Name as ProductName," +
" StockRequest.Quantity, StoreInventory.StockLevel from Store" +
" LEFT JOIN StoreInventory ON StoreInventory.StoreID = Store.StoreID" 
+ " LEFT JOIN Product ON StoreInventory.ProductID = 
Product.ProductID" + " LEFT JOIN StockRequest ON StockRequest.StoreID 
= Store.StoreID", conn);

Then just use the right name when reading it, like:

read["StoreID"],
read["StoreName"], read["ProductName"],
read["Quantity"], read["StockLevel"]
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.