简体   繁体   中英

How to copy some data from one table to another?

I have two tables Products and NotArrivedOrders (all columns are nvarchar(50) ).

Now in products I have this columns :

  1. ProductName.
  2. ProductPrice.
  3. ProductAmount.

And in NotArrivedOrder I have this :

  1. OrderId
  2. ProductName
  3. ProductPrice
  4. ProductAmount
  5. ArriveDate

And I want to insert to products all the orders that arrived(that today date is bigger then the arrived date).

How can I do it ? (there's a copy query or something like that).

I tried this :

INSERT INTO Products 
values(Select ProductName,ProductPrice,ProductAmount FROM NotArrivedOrder Where ArriveDate ='30/05/2013')"

and I'm getting this errors:

Incorrect syntax near the keywork 'Select'
Incorrect syntax near ')'.
the conversion of a nvarchar data type to a datetime data type resulted in an out-of-range value.

Please try this..

INSERT INTO PRODUCT 
SELECT ProductName, ProductPrice, ProductAmount 
FROM NotArrivedOrder 
WHERE ArriveDate<CONVERT(nvarchar(50),GetDate())

Try like this.......

 Insert Into Product(ProductName,
    ProductPrice,
    ProductAmount)  (Select ProductName,
    ProductPrice,
    ProductAmount
    from NotArrivedOrder  where Casr(ArriveDate as Datetime)< GetDate())

Query:

insert into Product 
values(select productname,ProductPrice,ProductAmount 
       from NotArrived order 
       where ArriveDate<GetDate())

C# Code:

    try
    {
    con.open();
    cmd=new sqlcommand("insert into Product values(select productname,ProductPrice,ProductAmount from NotArrived order where ArriveDate<GetDate())",conn);
    cmd.executenonquery();
    con.close();
    }
    catch(exception ex)
    {
    }
    finally
    {
     con.close();
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM