开发者

OleDb SQL Insert Statement

开发者 https://www.devze.com 2023-02-10 06:22 出处:网络
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 Quanti

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

精彩评论

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