开发者

OleDb SQL Insert Statement

My bill table has the columns Item, Price and Quantity and my following codes are fetching the item and price from another table (Products), however I would also like to insert another item for Quantity. How can i combine that in th开发者_Python百科is statement? Quantity = 1

INSERT INTO Bill (Item, Price) SELECT Product, Price FROM Products WHERE Product = 'Soft Drink'";


You could insert a sub-query into your main select statement:

INSERT INTO Bill (Item, Price)
SELECT Produce, Price, (Select Quantity FROM SomewhereElse) 
WHERE Products = 'Soft Drink'

Or you could use a variable:

declare @Quantity decimal(18,2)
select @Quantity = Quantity from SomewhereElse

INSERT INTO Bill (Item, Price)
SELECT Produce, Price, @Quantity WHERE Products = 'Soft Drink'


I solved it with the following statement:

"INSERT INTO Bill (Item, Price, Quantity) SELECT Product, Price, '1' as Quantity FROM Products WHERE Product = 'Soft Drink'";

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜