插入从源

问题描述:

多行运行SQL Server 2008的插入从源

我在做每次执行此查询时间上的临时表,并试图找到一种方法如何添加多行,如果有更多的则是1线输入。我有来自客户的收据信息。

create table #temp (col1 varchar(100),col2 varchar(100),col3 varchar(100)) 

insert into #temp (col1, col2, col3)values('Store ID', '01', '') 
insert into #temp (col1, col2, col3)values('Product','Quantity','Amount') 
insert into #temp (col1, col2, col3)values 
(
    (select product from receipt where receiptnum = 1), 
    (select quantity from receipt where receiptnum = 1), 
    (select amount from receipt where receiptnum = 1), 
) 
select * from #temp 

给予我我的输出(这是我的整个数据结构,但相同的概念只是让我不淹没代码): enter image description here

我的问题是我怎么能做到这一点,如果收据有超过1个产品,不知道未来。所有这些数据需要显示的是收据编号和商店编号,所以我不知道要输入的行数。

从环顾四周,似乎我将不得不使用光标,但我对光标的知识是没有的。

用下面的语句替换第三条插入语句会将所有记录插入到select中返回的临时表中。

insert into #temp 
select product, quality, amount from receipt where receiptnum = 1 
+0

我有多个插入来格式化它,就像你在图片中看到的那样,包括'Store ID','Register#'等设置值,然后它们后面的值从select查询中得到输入。使用你的插入给我: 子查询返回多个值。这是当子查询如下=,!=,,> =,或当子查询用作表达不允许的。 ,因为其他插入仅添加一行。否则,是的,这确实有效。 – JohnZ 2013-02-12 21:00:37

+0

我建议的陈述是只替换你的第三个插入。 – 2013-02-12 21:08:26

+0

我做到了,但因为所有其他插件而尖叫着我。我没有编辑。您的查询在任何其他插入中都没有select语句。如果我只是将原始数据手动插入到它们中,则不会出现错误。 – JohnZ 2013-02-12 21:11:46