SQL 服务器:'Could not be bound' - 错误

SQL Server : 'Could not be bound' - Error

我有一个动态存储过程,它很长,所以我只提供导致错误的部分。

  SELECT  @sSql = @sSql + ' 
            animal.AnimalId,
            species.Code AS Species,
            breed.Code AS Breed ,
            country.CountryId AS CountryId ,
            country.Code AS CountryCode ,
            animal.Sex AS Sex , 
            animalRegistration.Identifier AS RegNo ,
            breed.Description + country.Code + animal.Sex + animalRegistration.Identifier AS FullRegNo,
            thePrefix.Prefix AS Prefix,
            animal.Name AS Name,
            thePrefix.Prefix + animal.Name AS FullName, 
            animal.Name AS EarTag1,
            animal.DateOfBirth as DateOfBirth ,
            0 as AnimalFlagCount'
           --isnull( Genomics.GetAnimalFlagCount( a.AnimalId,' + CAST(@IsCatalogSamples AS VARCHAR(2)) + ' ), 0) AS AnimalFlagCount '
            --a.EarTag1 
    SELECT  @sFrom = '  
         FROM dbo.Animal AS animal 
         LEFT JOIN dbo.Breed breed ON animal.BreedId = breed.BreedId
         LEFT JOIN dbo.Species species ON species.SpeciesId = breed.SpeciesId
         LEFT JOIN dbo.AnimalIdentity animalIdentity ON animalIdentity.AnimalId = animal.AnimalId
         LEFT JOIN dbo.AnimalIdentity animalRegistration ON animalRegistration.AnimalId = animal.AnimalId and  animalRegistration.IdentityTypeId = 2
         LEFT JOIN dbo.Country country ON animalIdentity.CountryId = country.CountryId
         LEFT JOIN dbo.AnimalPrefix animalPrefix ON animalPrefix.AnimalId = animal.AnimalId
         LEFT JOIN dbo.Prefix thePrefix ON thePrefix.PrefixId = animalPrefix.PrefixId 

        WHERE
        '

返回错误:

Msg 4104, Level 16, State 1, Line 10
The multi-part identifier "thePrefix.Prefix" could not be bound.

Msg 4104, Level 16, State 1, Line 12
The multi-part identifier "thePrefix.Prefix" could not be bound.

当我在另一个 window 中使用这个确切的语句创建查询时,它 returns 没有问题:

  SELECT    
        animal.AnimalId,
        species.Code AS Species,
        breed.Code AS Breed ,
        country.CountryId AS CountryId ,
        country.Code AS CountryCode ,
        animal.Sex AS Sex , 
        animalRegistration.Identifier AS RegNo ,
        breed.Description + country.Code + animal.Sex + animalRegistration.Identifier AS FullRegNo,
        thePrefix.Prefix AS Prefix,
        animal.Name AS Name,
        thePrefix.Prefix + animal.Name AS FullName, 
        animal.Name AS EarTag1,
        animal.DateOfBirth as DateOfBirth ,
        0 as AnimalFlagCount
         FROM dbo.Animal AS animal 
         LEFT JOIN dbo.Breed breed ON animal.BreedId = breed.BreedId
         LEFT JOIN dbo.Species species ON species.SpeciesId = breed.SpeciesId
         LEFT JOIN dbo.AnimalIdentity animalIdentity ON animalIdentity.AnimalId = animal.AnimalId
         LEFT JOIN dbo.AnimalIdentity animalRegistration ON animalRegistration.AnimalId = animal.AnimalId and  animalRegistration.IdentityTypeId = 2
         LEFT JOIN dbo.Country country ON animalIdentity.CountryId = country.CountryId
         LEFT JOIN dbo.AnimalPrefix animalPrefix ON animalPrefix.AnimalId = animal.AnimalId
         LEFT JOIN dbo.Prefix thePrefix ON thePrefix.PrefixId = animalPrefix.PrefixId 
         where 
         animalRegistration.Identifier = '5573144'

任何人都可以看出为什么这会造成问题吗?我意识到用这么多代码可能是不可能的,但我想我可能忽略了一些大问题。

谢谢,

您是否可能忘记在执行之前将@sFrom 连接到@sSql? 尝试更改

SELECT @sFrom = ' FROM .....

SELECT @sSql = @sSql +  ' FROM .....

您也可以尝试插入您将要执行的 sql 的打印语句,以确保您正在执行您认为正在执行的内容。 ...

此外,@sFrom 声明为什么?也许它在最后一次加入之前就被截断了,因此出现了错误?

希望对您有所帮助