简体   繁体   English

子查询时只能指定表达式…SQL Server

[英]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. 我不知道这是否是您查询的真正语法,但对此我有一些疑问。

  1. Why naming db.TableA as A and then in the select do : TableA.xxx do A.xxx 为什么将db.TableA命名为A,然后在select do中:TableA.xxx做A.xxx
  2. In your select you have a ColA and in your where clause a Col1 is it correct and supposed to be different? 在您的选择中,您有一个ColA,而在where子句中,Col1是否正确并且应该不同?
  3. Still in your where clause, you have a table B, but what is the B = 12? 仍然在您的where子句中,您有一个表B,但是B = 12是什么?
  4. If you wanna have a sum of everything by date, you shoud remove the third column A.ColA or write your query differently. 如果您想按日期汇总所有内容,则应删除第三列A.ColA或以其他方式编写查询。 At this moment as you are doing a group by Date, ColA it's splitted :) 现在,当您按日期进行分组时,ColA已被拆分:)
  5. in the where clause, amount is the date of the "transaction"? 在where子句中,金额是“交易”的日期?

Except these and they are most syntax than logic, it should work. 除了这些并且它们是逻辑之外的大多数语法,它应该起作用。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如果未使用EXISTS引入子查询,则只能在选择列表中指定一个表达式。 -SQL Server - Only one expression can be specified in the select list when the subquery is not introduced with EXISTS. - SQL Server SQL Server:当EXISTS未引入子查询时,只能在选择列表中指定一个表达式 - SQL Server : Only one expression can be specified in the select list when the subquery is not introduced with EXISTS ERROR SQL Server ONLY 一个表达式可以在选择列表中指定当子查询没有引入时 - ERROR SQL Server ONLY one expression can be specified in the select list when the subquery is not introduced with exists SQL Server错误:当未使用EXISTS引入子查询时,只能在选择列表中指定一个表达式 - SQL Server error :only one expression can be specified in the select list when the subquery is not introduced with EXISTS SQL Server数据库错误:如果未使用EXISTS引入子查询,则只能在选择列表中指定一个表达式 - SQL Server Database Error: Only one expression can be specified in the select list when the subquery is not introduced with EXISTS SQL-如果未使用EXISTS引入子查询,则只能在选择列表中指定一个表达式 - SQL - Only one expression can be specified in the select list when the subquery is not introduced with EXISTS 未使用EXISTS引入子查询时,只能在选择列表中指定一个表达式-sql中的错误 - Only one expression can be specified in the select list when the subquery is not introduced with EXISTS — error in sql SQL : 当子查询没有用 EXISTS 引入时,选择列表中只能指定一个表达式 - SQL : Only one expression can be specified in the select list when the subquery is not introduced with EXISTS SQL错误-如果未使用EXISTS引入子查询,则只能在选择列表中指定一个表达式 - SQL Error - Only one expression can be specified in the select list when the subquery is not introduced with EXISTS SQL中删除语法的问题-错误如果未使用EXISTS引入子查询,则只能在选择列表中指定一个表达式 - Problem with delete syntax in SQL - Error Only one expression can be specified in the select list when the subquery is not introduced with EXISTS
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM