Understanding the logic from creating,inserting, and updating a table

  • Here is the scenario I am trying to learn. I have three tables:

    1)customer table

    cust_name nchar (50)

    cust_emai nchar(255)

    cust_Id int

    2)orders table

    order_num int

    Order_date

    cust_id

    3)orderitems table

    order_number int

    order_item int

    Now I create a sql stmt to manipulate the table #sold. What is

    the proper way to do an insert and update to have those columns populate into the #SOLD table?

    Create table #Sold(

    customername nchar(50) null default null,

    customeremail nchar(255) null default null,

    ordernumber int not null,

    orderitem int null default null,

    orderdate datetime null default null

    )

    ----Insert----

    insert into #Sold

    select c.cust_name,c.cust_email,O.order_num

    from dbo.customers c, orders o

    where c.cust_id=o.cust_id

  • Thanks but I figure this out. Sorry I will post in the newbie section.

    Create table #Sold(

    customername nchar(50) null default null,

    customeremail nchar(255) null default null,

    ordernumber int not null,

    orderitem int null default null,

    orderdate datetime null default null

    )

    ----Insert----

    insert into #Sold (customername,customeremail,ordernumber)

    select c.cust_name,c.cust_email,O.order_num

    from customers c inner join orders o

    on c.cust_id=o.cust_id

    -----Update #SOLD table-----

    Update #Sold

    Set orderitem = os.order_item,

    orderdate = s.order_date

    From orderitems os

    INNER JOIN orders s ON os.order_num = s.order_num

Viewing 2 posts - 1 through 2 (of 2 total)

You must be logged in to reply to this topic. Login to reply