Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
| Author |
Topic |
|
myksdsu
Starting Member
13 Posts |
Posted - 2007-06-03 : 13:42:50
|
| I have two tables:1) table of customers: CustomerName, CustomerId, CustomerAddress2) table of orders: OrderId, CustomerId, OrderAmountI would like to have a query that returns everything from the customer table and add one column that has the amount of orders the customer has made, this is what I have so far:CREATE PROCEDURE dbo.GetAllCutomerInfoASDECLARE @OrderCount intSELECT CustomerName, CustomerId, CustomerAddress, (SELECT COUNT(OrderId) FROM Cust_Orders WHERE CustomerId= CustomerId)FROM CustomersORDER BY CustomerNameCan you add a variable:CREATE PROCEDURE dbo.GetAllCutomerInfoASDECLARE @CustID intDECLARE @OrderCount intSELECT CustomerName, @CustID=CustomerId, CustomerAddress, (SELECT COUNT(OrderId) FROM Cust_Orders WHERE CustomerId= @CustID)FROM CustomersORDER BY CustomerNameThanks for any help. |
|
|
pootle_flump
1064 Posts |
Posted - 2007-06-03 : 13:51:35
|
| [code]SELECT dbo.Customers.CustomerName , dbo.Customers.CustomerId , dbo.Customers.CustomerAddress , CustOrdCount.CustomerOrderCountFROM dbo.CustomersLEFT OUTER JOIN --Customers' order counts (SELECT CustomerId , COUNT(OrderId) AS CustomerOrderCount FROM dbo.Cust_Orders GROUP BY CustomerId) AS CustOrdCountON CustOrdCount.CustomerId = dbo.Customers.CustomerIdWHERE dbo.Customers.CustomerId= @CustIDORDER BY CustomerName[/code] |
 |
|
|
myksdsu
Starting Member
13 Posts |
Posted - 2007-06-03 : 14:42:31
|
| That looks good, but I can't see where a value is read into @CustID? |
 |
|
|
pootle_flump
1064 Posts |
Posted - 2007-06-03 : 16:21:21
|
| I assumed you had that covered. What do you want to do? Do you know how to write stored procedures that accept parameters? |
 |
|
|
myksdsu
Starting Member
13 Posts |
Posted - 2007-06-04 : 15:25:54
|
| Thanks pootle_flump I got it. |
 |
|
|
|
|
|
|
|