我有一个动态存储过程,它很长,所以我只提供导致错误的部分。
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
无法绑定多部分标识符“thePrefix.Prefix”。Msg 4104,Level 16,State 1,Line 12
无法绑定多部分标识符“thePrefix.Prefix”。
当我在另一个窗口中使用此确切语句创建查询时,它将返回out out:
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'
有人能看出为什么这会造成问题吗?我意识到使用这么多代码可能无法做到,但我想也许我忽视了一些重要的事情。
谢谢,
答案 0 :(得分:0)
在执行之前,您是否忘记将@sFrom连接到@sSql? 尝试更改
SELECT @sFrom = ' FROM .....
到
SELECT @sSql = @sSql + ' FROM .....
您还可以尝试插入要执行的sql的print语句,以确保执行您认为正在执行的操作。 ...
另外,@ sFrom声明为什么?也许它在最后一次加入之前被截断,因此错误?
希望有所帮助