2013-01-11 3 views
2

Ce code:SQL Server 2005 - L'identifiant plusieurs parties ne pouvait être liée

USE [db] 
GO 
/****** Object: UserDefinedFunction [dbo].[GetFieldPickerReports] Script Date: 01/11/2013 19:12:27 ******/ 
SET ANSI_NULLS ON 
GO 
SET QUOTED_IDENTIFIER ON 
GO 

ALTER FUNCTION [dbo].[GetFieldPickerReports] (@CustomerName varchar(100)) 
RETURNS TABLE 
AS 
    RETURN SELECT [fpr].[ID], [fpr].[Name], [fpr].[Global], [fpr].[FPRCategoryID] 
    FROM FieldPickerReport fpr, dbo.GetProductKeyIdByCustomer(@CustomerName) pk 
    LEFT JOIN [FPRCategory] fprcat on ([fpr].[FPRCategoryID]=[fprcat].[ID]) 
    WHERE [Global]=1 OR ProductKeyID=pk.id 

génère une erreur:

The multi-part identifier "fpr.FPRCategoryID" could not be bound.

Cette requête fonctionne sans Left Join (dont j'ai besoin actuellement d'ajouter) ou avec la suppression de dbo.GetProductKeyIdByCustomer() et la clause Where.

Voici le code pour dbo.GetProductKeyIdByCustomer()

USE [db] 
GO 
/****** Object: UserDefinedFunction [dbo].[GetProductKeyIdByCustomer] Script Date: 01/11/2013 19:58:34 ******/ 
SET ANSI_NULLS ON 
GO 
SET QUOTED_IDENTIFIER ON 
GO 

ALTER FUNCTION [dbo].[GetProductKeyIdByCustomer] (@CustomerName varchar(100)) 
RETURNS TABLE 
AS 
    RETURN SELECT id 
    FROM ProductKey 
    WHERE [CustomerName][email protected] AND Enabled=1 

Quel est le problème avec ma requête?

+1

Y a-t-il une logique à laquelle les noms sont mis entre crochets? –

+0

@MrLister Peut-être le développeur précédent avec lequel je n'ai aucun contact. – Dante

Répondre

3

Tout d'abord, je pense que ces fonctions UDF seraient mieux implémentées en tant que vues. Deuxièmement, vous mélangez des jointures implicites et explicites. L'utilisation de toutes les jointures explicites devrait résoudre votre problème:

SELECT 
    [fpr].[ID], 
    [fpr].[Name], 
    [fpr].[Global], 
    [fpr].[FPRCategoryID] 
FROM 
    FieldPickerReport fpr 
    JOIN dbo.GetProductKeyIdByCustomer(@CustomerName) pk 
     ON [Global] = 1 OR ProductKeyID = pk.id 
    LEFT JOIN FPRCategory fprcat 
     ON fpr.FPRCategoryID = fprcat.ID 
Questions connexes