简体   繁体   English

如何改善“关键字'选择'附近的语法不正确”。

[英]How to improve 'Incorrect syntax near the keyword 'select'.'

this query give me 'Incorrect syntax near the keyword 'select'.' 该查询给我“关键字'选择'附近的语法不正确”。 look please below bold character area. 请在粗体字区域下方查看。

declare @date1 smalldatetime, @date2 smalldatetime, @page nvarchar(100) ,@sum int
select @date1='2009-06-06',@date2='2009-06-13',@page='Tüm Sayfalar'
set @sum = select Sum(t.[VISITINGCOUNT]) from
(        
select count(page) as [VISITINGCOUNT],                 
     cast(DATENAME ( year ,DATE)+'-'+DATENAME (month ,DATE)+
     '-'+DATENAME (day ,DATE) as smalldatetime)  as [DATE]               
    from scr_StatisticaLog                
    where Date between @date1 and  @date2  
          and (Page=@page or  @page='Tüm Sayfalar')  and ProcessType='PageView'           
GROUP BY 
cast(DATENAME ( year ,DATE)+
'-'+DATENAME (month ,DATE)+
'-'+DATENAME (day ,DATE) as smalldatetime)) as t    

select 100*(t.[VISITINGCOUNT]/@sum),t.[DATE] from        
(        
select count(page) as [VISITINGCOUNT],                 
     cast(DATENAME ( year ,DATE)+'-'+DATENAME (month ,DATE)+
     '-'+DATENAME (day ,DATE) as smalldatetime)  as [DATE]               
    from scr_StatisticaLog                
    where Date between @date1 and  @date2  
          and (Page=@page or  @page='Tüm Sayfalar')  and ProcessType='PageView'           
GROUP BY 
cast(DATENAME ( year ,DATE)+
'-'+DATENAME (month ,DATE)+
'-'+DATENAME (day ,DATE) as smalldatetime)) as t   

I don't believe you can have SET @var = SELECT ... 我不相信你可以有SET @var = SELECT ...

Try the following instead: 请尝试以下操作:

SELECT @sum = Sum(t.[VISITINGCOUNT]) from ...

I would also like to say that seeing as all you are doing is SUM-ing a single column in your sub-query there is no point in doing the group by, or returning the second column. 我还要说的是,您正在做的全部工作就是对子查询中的单个列求和,因此没有必要进行分组依据或返回第二列。

You are basically counting the number of records in various groups, then adding all the groups together. 您基本上是在统计各个组中的记录数,然后将所有组加在一起。 Far quicker just to count the total number of pages in total if that is all you need. 如果需要的话,只计算总数的总页数要快得多。

The following would be much simpler: 以下内容会简单得多:

SELECT @sum = COUNT(page) FROM scr_StatisticaLog                
   WHERE Date BETWEEN @date1 AND  @date2  
   AND (Page=@page OR  @page='Tüm Sayfalar') AND ProcessType='PageView'

Try changing 尝试改变

set @sum = select Sum(t.[VISITINGCOUNT]) from

to

select @sum = Sum(t.[VISITINGCOUNT]) from

Haven't tested it works though, just that it parses correctly without errors. 尚未测试它是否有效,只是它可以正确解析而没有错误。

如果要通过SSMS执行此操作,则应在执行脚本之前尝试从菜单启用“ SQLCMD模式”。

declare @date1 smalldatetime, @date2 smalldatetime, @page nvarchar(100) ,@sum int
select @date1='2009-06-06',@date2='2009-06-13',@page='Tüm Sayfalar'

--Changed here
select @sum = Sum(t.[VISITINGCOUNT]) from
(        
select count(page) as [VISITINGCOUNT],                 
     cast(DATENAME ( year ,DATE)+'-'+DATENAME (month ,DATE)+
     '-'+DATENAME (day ,DATE) as smalldatetime)  as [DATE]               
    from scr_StatisticaLog                
    where Date between @date1 and  @date2  
          and (Page=@page or  @page='Tüm Sayfalar')  and ProcessType='PageView'           
GROUP BY 
cast(DATENAME ( year ,DATE)+
'-'+DATENAME (month ,DATE)+
'-'+DATENAME (day ,DATE) as smalldatetime)) as t    




select 100*(t.[VISITINGCOUNT]/@sum),t.[DATE] from        
(        
select count(page) as [VISITINGCOUNT],                 
     cast(DATENAME ( year ,DATE)+'-'+DATENAME (month ,DATE)+
     '-'+DATENAME (day ,DATE) as smalldatetime)  as [DATE]               
    from scr_StatisticaLog                
    where Date between @date1 and  @date2  
          and (Page=@page or  @page='Tüm Sayfalar')  and ProcessType='PageView'           
GROUP BY 
cast(DATENAME ( year ,DATE)+
'-'+DATENAME (month ,DATE)+
'-'+DATENAME (day ,DATE) as smalldatetime)) as t   

If you insist on sticking with SET, then wrap the entire subquery in parentheses () 如果您坚持使用SET,则将整个子查询括在括号()中

set @sum = ( select Sum(t.[VISITINGCOUNT]) from ....... ) set @sum = select Sum(t.[VISITINGCOUNT]) from .......

otherwise change the variable assignment to a ' SELECT @var = ' as suggested by other answers 否则将变量赋值更改为其他答案建议的' SELECT @var = '

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM