Link to home
Create AccountLog in
Avatar of johnnyg123
johnnyg123Flag for United States of America

asked on

sql to add additional rows

Here is some sample data from the order and customer tables (SQL Server 2014)

Order Table
OrderID  OrderAmount    
1              300    
2              400          


Customer Table
CustomerID  CustomerFirstName CustomerLastName
500                Fred                          Smith

Need to write a query that will return all rows from customer table with a row for each order that exists

Given sample data would like query to return the following

CustomerID  CustomerFirstName CustomerLastName OrderID  OrderAmount
500                Fred                          Smith                        1             300
500                Fred                          Smith                        2             400

Not sure of best way to do this


ASKER CERTIFIED SOLUTION
Avatar of slightwv (䄆 Netminder)
slightwv (䄆 Netminder)

Link to home
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
See answer
try:

select c.*, o.*
from Customer c, Order o

Open in new window

But you really want to know what you want to return, and if this is the expected result
Avatar of slightwv (䄆 Netminder)
slightwv (䄆 Netminder)

>>from Customer c, Order o

That will work but probably better to use the new ANSI join syntax.
Hi,
Try
select a.*,b.*
from customertable,ordertable

Open in new window

I think better to have/add customerID column (as one foreign key) in Ordertable, to know which customer does make which order.


@Peter,

Please review previous Experts comments.  That has already been posted.

and yours is incorrect.  You cannot select a.* if you don't alias the tables.
:: snicker ::   This question was answered in the first comment, with helpful commentary.