home bbs files messages ]

Forums before death by AOL, social media and spammers... "We can't have nice things"

   comp.databases.ms-sqlserver      Notorious Rube Goldberg contraption      19,505 messages   

[   << oldest   |   < older   |   list   |   newer >   |   newest >>   ]

   Message 19,052 of 19,505   
   Erland Sommarskog to Tony Johansson   
   Re: How could I add ProductName in this    
   15 Mar 14 17:30:24   
   
   From: esquel@sommarskog.se   
      
   Tony Johansson (johansson.andersson@telia.com) writes:   
   > The primary key in Products is ProductID In this select clause I want to   
   > add in the result the ProductName that exist in the Product table.   
   > How could I do that ?   
   >   
   > Command.CommandText = "SELECT BokningFromDate, BokningToDate, Name,   
   > Address,   
   > Phone, ProductID, EnhetsPris " +   
   >           "FROM Bokningar " +   
   >           "INNER JOIN Customers " +   
   >           "ON Bokningar.CustomerID = Customers.CustomerID " +   
   >           "INNER JOIN BokningDetails " +   
   >           "ON Bokningar.BokningarID = BokningDetails.BokningarID " +   
   >           "ORDER BY Name asc";   
   >   
      
    Command.CommandText =   
       @"SELECT BokningFromDate, BokningToDate, Name, Address,   
                Phone, ProductID, EnhetsPris, P.ProductID   
         FROM   Bokningar B   
         JOIN   Customers C ON B.CustomerID = C.CustomerID   
         JOIN   BokningDetails BD ON B.BokningarID = BD.BokningarID   
         JOIN   Products P ON BD.ProudctID = P.ProductID   
         ORDER BY Name asc";   
      
   Assumining that the language is C#, by using the @ prefix for a string   
   literal, it can spill over many lines. This increases the readability   
   of the SQL. I have also introduced aliases for the tables, also to   
   improve readability. Repeating the table names again and again, makes   
   it difficult to see the forest for the trees.   
      
   You should have prefixes for all columns in a multi-table query. This   
   makes the query easier to read for anyone who don't know the tables   
   by heart. It also prevents the query from breaking if a column with   
   the same name is added to another table in the query.   
      
      
   --   
   Erland Sommarskog, Stockholm, esquel@sommarskog.se   
      
   --- SoupGate-Win32 v1.05   
    * Origin: you cannot sedate... all the things you hate (1:229/2)   

[   << oldest   |   < older   |   list   |   newer >   |   newest >>   ]


(c) 1994,  bbs@darkrealms.ca