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.

 All Forums
 SQL Server 2008 Forums
 Transact-SQL (2008)
 insert table from one to another

Author  Topic 

teamjai
Yak Posting Veteran

52 Posts

Posted - 2012-02-10 : 02:07:07
Hi,
how to insert values from one to another,My table is
Table 1--Cart, Table 2--Order

Cart Table:
1.Cno----------Primarykey
2.ProductName.
3.Price.
----------------
Order Tabel:
1.Ordno.------Primarykey
2.ProductName.
3.Price.

i want insert ProductName and Price from Order table to Cart Table,
i got an error below query

'insert into Cart(ProductName,Price)select ProductName, Price from Order'

error-
Cannot insert the value NULL into column 'Cno', table 'Cart'; column does not allow nulls. INSERT fails.
The statement has been terminated.

So, i modify that Query

Query----
'insert into Cart(123,ProductName,Price)select ProductName, Price from Order'

again i got an error

error is
'Incorrect syntax near 'ProductName''


how to clear this error.pls help me

sunitabeck
Master Smack Fu Yak Hacker

5155 Posts

Posted - 2012-02-10 : 06:45:04
Two things:
1. Order is a reserved keyword, so you need to escape it with square brackets. Most people recommend that you do not use reserved words as names for tables, views etc.

2. The value you want to insert should be in the select list.

So your query should be something like this:
insert into Cart(OrdNo, ProductName,Price) select 123,ProductName, Price from [Order]
Go to Top of Page

teamjai
Yak Posting Veteran

52 Posts

Posted - 2012-02-10 : 07:07:58
hi,
its executed ur query.
thank u

quote:
Originally posted by sunitabeck

Two things:
1. Order is a reserved keyword, so you need to escape it with square brackets. Most people recommend that you do not use reserved words as names for tables, views etc.

2. The value you want to insert should be in the select list.

So your query should be something like this:
insert into Cart(OrdNo, ProductName,Price) select 123,ProductName, Price from [Order]


Go to Top of Page
   

- Advertisement -