我收到“关键字'FROM'附近的语法不正确”错误

时间:2017-10-16 00:05:21

标签: sql-server tsql

当我执行以下代码时:

USE Database
GO

DECLARE @DF Datetime = CONVERT(datetime,'30/01/2017',103)
DECLARE @DT Datetime = CONVERT(datetime,'13/03/2017',103)

DECLARE @Owner nvarchar(255) = 'user@email.co.uk'
DECLARE @Box nvarchar(255) = 'Sent Items'
DECLARE @ResultTo int = 0
DECLARE @ResultCC int = 0
DECLARE @ResultBCC int = 0

SET @ResultTo = COUNT(ToEmailAddress) FROM EmailTo 
WHERE fkDataExtractTableID IN 
    (
        SELECT DataExtractTableID
        FROM [Database].[dbo].[DataExtractTable]
        WHERE (DateSent between @DF AND @DT)
        AND ([Owner] = @Owner)
        AND (Box = @Box)
      )

我收到此错误消息:

  

Msg 156,Level 15,State 1,Line 12
  关键字“FROM”附近的语法不正确。

我错过了什么?

4 个答案:

答案 0 :(得分:0)

我认为你的sql在第12行出错。可能是这样的:

public interface ICoverage
{
    string Name { get; set; }
    string Code { get; set; }
}

public class CoverageA : ICoverage
{
    public string Name { get; set; }
    public string Code { get; set; }
    public string Current { get; set; }
}

public class CoverageB : ICoverage
{
    public string Name { get; set; }
    public string Code { get; set; }
    public bool HasRecord { get; set; }
}

public class Application
{
    public int ApplicationId { get; set; }
    public string Code { get; set; }
    public List<ICoverage> Coverages { get; set; }

    public Application()
    {
        Coverages = new List<ICoverage>();
    }
}

public class StagingDto
{
    public string Referrer { get; set; }
    public string Code { get; set; }
    public CoverageADto CoverageA { get; set; }
    public CoverageBDto CoverageB { get; set; }
}

public class CoverageADto
{
    public string Current { get; set; }
}

public class CoverageBDto
{
    public bool HasRecord { get; set; }
}

答案 1 :(得分:0)

将变量分配更改为:

select @ResultTo = count(ToEmailAddress) from EmailTo

答案 2 :(得分:0)

以下是更正后的内容:

DECLARE @DF Datetime = CONVERT(datetime,'30/01/2017',103)
DECLARE @DT Datetime = CONVERT(datetime,'13/03/2017',103)

    DECLARE @Owner nvarchar(255) = 'user@email.co.uk'
    DECLARE @Box nvarchar(255) = 'Sent Items'
    Declare @ResultTo int = 0
    Declare @ResultCC int = 0
    Declare @ResultBCC int = 0

    SELECT @ResultTo = COUNT(ToEmailAddress) FROM EmailTo 
    WHERE fkDataExtractTableID IN 
    (
        SELECT DataExtractTableID
        FROM [Database].[dbo].[DataExtractTable]
        WHERE (DateSent between @DF AND @DT)
        AND ([Owner] = @Owner)
        AND (Box = @Box)
      )

答案 3 :(得分:0)

您不能在选择查询中使用2种不同的语法。使用Set语句或使用简单查询

设置声明 - 您将单个变量设置为选择查询的返回

SET @ResultTo = (Select COUNT(ToEmailAddress) FROM EmailTo 
WHERE fkDataExtractTableID IN 
    (
        SELECT DataExtractTableID
        FROM [Database].[dbo].[DataExtractTable]
        WHERE (DateSent between @DF AND @DT)
        AND ([Owner] = @Owner)
        AND (Box = @Box)
      ) )

或选择您设置多个变量值的语句,如

Select @ResultTo = COUNT(ToEmailAddress)  
--Aslo define other variable, if you want
-- like @ResultTo1 , but this query is aggregation, so only aggeregate function use
FROM EmailTo 
WHERE fkDataExtractTableID IN 
    (
        SELECT DataExtractTableID
        FROM [Database].[dbo].[DataExtractTable]
        WHERE (DateSent between @DF AND @DT)
        AND ([Owner] = @Owner)
        AND (Box = @Box)
      )