Advertisement

11.02.2007 at 08:48PM PDT, ID: 22936329
[x]
Attachment Details

T-sql

Asked by salesprod in MS SQL Server, SQL Server 2005

Tags: create

I have the following tables created:

CREATE TABLE Orders
   (OrderID                 int

        Constraint Order_ID_pk primary key
  , CustomerID              nchar (5)
  , EmployeeID              int
  , OrderDate               datetime
  , RequiredDate            datetime
  , ShippedDate             datetime
  , ShipVia                 int
  , Freight                 money
  , ShipName                nvarchar (40)
  , ShipAddress             nvarchar (60)
  , ShipCity                nvarchar (15)
  , ShipRegion              nvarchar (15)
  , ShipPostalCode          nvarchar (10)
  , ShipCountry             nvarchar (15)  
   )

CREATE TABLE Order_Details
   (OrderID                 int      NOT NULL
  , ProductID               int      NOT NULL
  , UnitPrice               money    NOT NULL
  , Quantity                smallint NOT NULL
  , Discount                real     NOT NULL
  , constraint Order_Details_PK
        Primary key (OrderID, ProductID)
   )

and I need to create a Transact SQL procedure, customer_activity, that would, for a given CustomerID, return the number of orders that customer has made, average amount of all the customer orders, and the maximum customers order.  The CustomerID should be the stored procedures input parameter. The stored procedure should use the view customer_orders.

Can you tell me where i went wrong?

Create procedure customer_activity (@customerID nchar(5))
as
select sum(orders) as Sum_Customer_Orders, max(orders)as Max_Customer_Orders, avg(orders) as Avg_Customer_Orders
from Order_Details inner join Orders on
Order_Details.OrderID = Orders.OrderID
where customerid = @customerID

the view i created is this:
Create view customer_orders
as
select sum(Quantity * UnitPrice * (1.00-Discount)) as OrderAmount, order_details.OrderID, orders.CustomerID
from Order_Details inner join Orders on
Order_Details.OrderID = Orders.OrderID
group by order_details.orderID, orders.customerid

1. how would i populate records in those tables to test the view and procedure.
2. how would i create a trigger for any order entered (inserted), print the order amount as well as customer average and max order using view and store procedure above
3. I would like to know how to test the trigger running the transaction that would insert an order (one row in the orders table) with three order details.

your assistance is appreciated. Start Free Trial
[+][-]11.02.2007 at 09:29PM PDT, ID: 20205931

View this solution now by starting your 7-day free trial. Setting up your free trial is quick, easy, and secure. We will return you to this solution, unlocked, when you're done.

 

About this solution

Zones: MS SQL Server, SQL Server 2005
Tags: create
Sign Up Now!
Solution Provided By: HuyBD
Participating Experts: 2
Solution Grade: B
 
 
[+][-]11.02.2007 at 09:32PM PDT, ID: 20205935

Assisted solutions are selected by the member who asked the question as a comment that contributed to their question's solution.

Start your 7-day free trial to view this Assisted Solution or ask the Experts your question.

 
[+][-]11.02.2007 at 09:34PM PDT, ID: 20205942

Assisted solutions are selected by the member who asked the question as a comment that contributed to their question's solution.

Start your 7-day free trial to view this Assisted Solution or ask the Experts your question.

 
[+][-]11.02.2007 at 09:42PM PDT, ID: 20205952

Assisted solutions are selected by the member who asked the question as a comment that contributed to their question's solution.

Start your 7-day free trial to view this Assisted Solution or ask the Experts your question.

 
[+][-]11.02.2007 at 09:45PM PDT, ID: 20205960

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]11.02.2007 at 09:56PM PDT, ID: 20205980

Assisted solutions are selected by the member who asked the question as a comment that contributed to their question's solution.

Start your 7-day free trial to view this Assisted Solution or ask the Experts your question.

 
[+][-]11.03.2007 at 03:10AM PDT, ID: 20206410

Assisted solutions are selected by the member who asked the question as a comment that contributed to their question's solution.

Start your 7-day free trial to view this Assisted Solution or ask the Experts your question.

 
[+][-]11.04.2007 at 08:12PM PST, ID: 20213793

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
 
Loading Advertisement...
20080716-EE-VQP-32 / EE_QW_2_20070628