[英]Only expression can be specified when subquery… SQL server
So, I've been working on a loop to get data from each day going back 211 days for a report. 因此,我一直在做一个循环,以获取从211天开始的每一天的数据以生成报告。 And I'm getting this error message. 而且我收到此错误消息。 I'm using SQL Server 2008 R2, and although I don't know why I get this error, I've tried some different things that haven't worked. 我正在使用SQL Server 2008 R2,尽管我不知道为什么会收到此错误,但我已经尝试了一些不起作用的方法。 So I'm asking here really thankful for answers. 因此,我要在这里非常感谢您的回答。
Msg 116, Level 16, State 1, Line 21 消息116,第16层,状态1,第21行
Only one expression can be specified in the select list when the subquery is not introduced with EXISTS. 如果未使用EXISTS引入子查询,则只能在选择列表中指定一个表达式。
My code 我的密码
create table #ExcelPrint (row int IDENTITY (1, 1) NOT NULL, Col01 varchar(100),
Col02 varchar(100), Col03 varchar(100), Col04 varchar(100),Col05 varchar(100))
declare @counter int
set @counter = 0
insert into #excelprint (Col01,Col02,Col03) values ('Text','Number', 'Amount')
while @counter > -211
begin
insert into #ExcelPrint (Col01,Col02)
select (CONVERT(varchar,Dateadd(DD,@counter,GETDATE()),112)),
(
select TableA.ColA ,sum(colB)
from db.TableA as A
inner join db.TableB B on B.Col1 = A.Col1
inner join db.TableC C on C.Col1 = B.Col1
where amount = CONVERT(varchar,Dateadd(DD,@counter,GETDATE()),112)
and A.Col1 = 123
and B = 12
group by ColA.A
)
set @counter = @counter -1
end
select isnull(Col01,''), Replace(ISNULL(col02,''),'.',','),
Replace(ISNULL(col03,''),'.',',')
,Replace(ISNULL(Col04,''),'.',',') from #ExcelPrint
order by row
drop table #ExcelPrint
change 更改
select (CONVERT(varchar,Dateadd(DD,@counter,GETDATE()),112)),
(
select TableA.ColA ,sum(colB)
from db.TableA as A
to 至
select CONVERT(varchar,Dateadd(DD,@counter,GETDATE()),112))
,TableA.ColA
,sum(colB)
from db.TableA as A ...
fix the number of column in the insert clause and group on the appropriate column... 固定insert子句中的列数,并在适当的列上分组...
i don't know if it's the real syntax of your query but i have some interrogation about it. 我不知道这是否是您查询的真正语法,但对此我有一些疑问。
Except these and they are most syntax than logic, it should work. 除了这些并且它们是逻辑之外的大多数语法,它应该起作用。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.