Advertisement

07.18.2008 at 09:26AM PDT, ID: 23577372
[x]
Attachment Details
[x]
The Solution Rating System

With so many solutions, how can you tell which solutions are most likely to help you and which ones are not? To provide you with a tool to use, we rate our solutions based on various elements that most accurately determine if a solution is a quality solution. To explain what factors affect the solution rating, here are the elements we take into consideration when formulating our solution rating.

  • The Grade of the Solution
  • The Zone Rank of the Expert Providing the Solution
  • The Number of Author and Expert Comments
  • The Number of Experts Contributing
  • The Feedback of the Community

Your Input Matters
Because of the way the system is set up, the most important variable in this equation is you. As a member of Experts Exchange, you are able to cast your vote on the quality of the solutions in regard to how complete, accurate, helpful and easy to understand each solution is. When you provide your feedback, each rating is adjusted accordingly. So, if you see a solution that has a poor rating that you think is a good solution, let us know by rating it. As you do, the rating will be adjusted and will become more accurate for other members of our site.

If you have any suggestions that you would like to make for our rating system, please ask a question in the Suggestions Zone of Community Support.

Thank you!

8.8

T-Sql User Defined Function with Subquery Fails to Compile - Column Prefix error

Asked by kdnezz in SQL Query Syntax, SQL Server 2005

Tags: , ,

I am using SQL Server 2000 and I have created a user defined function named TT_GET_MARKUP2.  The select statement of the function works in Microsoft's SQL Query Analyzer, but when I try to create the function using the same select statement the error messages "The column prefix 'ERN' does not match with a table name or alias name used in the query."  and "The column prefix "LINE" does not match with a table name or alias name used in the query."
are returned.

This is the SQL Statement that works when it is run outside of the function:
SELECT  
  ((ERN.REG_BILL_RATE - ERN.REG_PAY_RATE)/ERN.REG_PAY_RATE)*100
FROM
  PS_BI_HDR HDR WITH(NOLOCK)
  JOIN PS_BI_LINE AS LINE WITH(NOLOCK) ON
        HDR.BUSINESS_UNIT = LINE.BUSINESS_UNIT
    AND HDR.INVOICE = LINE.INVOICE
  JOIN PS_PROJ_ACTIVITY PRJACT WITH(NOLOCK) ON
        LINE.BUSINESS_UNIT = PRJACT.BUSINESS_UNIT
    AND LINE.PROJECT_ID = PRJACT.PROJECT_ID
    AND LINE.ACTIVITY_ID = PRJACT.ACTIVITY_ID
  JOIN FO880PRD.dbo.PS_FO_ASSIGNMENT FOASSN WITH(NOLOCK) ON
        PRJACT.PC_SCH_FIELD2 = FOASSN.ORDER_ID
    AND PRJACT.PC_SCH_FIELD1 = FOASSN.BUSINESS_UNIT
    AND PRJACT.PC_SCH_FIELD5 = FOASSN.FO_ASSIGNMENT_ID
  JOIN FO880PRD.dbo.PS_FO_ASGN_ERN_DTL ERN WITH(NOLOCK) ON
       ERN.BUSINESS_UNIT = FOASSN.BUSINESS_UNIT
   AND ERN.ORDER_ID = FOASSN.ORDER_ID
   AND ERN.FO_ASSIGNMENT_ID = FOASSN.FO_ASSIGNMENT_ID
   AND ERN.DETAIL_NO = 1
 WHERE
      HDR.INVOICE = '40175618' --@INVOICE in function
  AND LINE.EMPLID = '00091350' --@EMPLID in fucntion
  AND ERN.EFFDT = (SELECT MAX(B.EFFDT)
                   FROM FO880PRD.dbo.PS_FO_ASGN_ERN_DTL B WITH(NOLOCK)
                   WHERE B.BUSINESS_UNIT = ERN.BUSINESS_UNIT AND
                         B.FO_ASSIGNMENT_ID = ERN.FO_ASSIGNMENT_ID AND
                         B.ORDER_ID = ERN.ORDER_ID AND
                         B.DETAIL_NO = ERN.DETAIL_NO AND
                         B.EFFDT <= '2008-04-06 00:00:00.000' --LINE.CHARGE_FROM_DATE
                        )

This is the Function I am trying to compile using the above SQL statement:
CREATE FUNCTION TT_CALC_MARKUP2 (@INVOICE nvarchar(50), @EMPLID nvarchar(20))
RETURNS NUMERIC AS
BEGIN
     RETURN (
SELECT  
  ((ERN.REG_BILL_RATE - ERN.REG_PAY_RATE)/ERN.REG_PAY_RATE)*100
FROM
  PS_BI_HDR HDR WITH(NOLOCK)
  JOIN PS_BI_LINE LINE WITH(NOLOCK) ON
        HDR.BUSINESS_UNIT = LINE.BUSINESS_UNIT
    AND HDR.INVOICE = LINE.INVOICE
  JOIN PS_PROJ_ACTIVITY PRJACT WITH(NOLOCK) ON
        LINE.BUSINESS_UNIT = PRJACT.BUSINESS_UNIT
    AND LINE.PROJECT_ID = PRJACT.PROJECT_ID
    AND LINE.ACTIVITY_ID = PRJACT.ACTIVITY_ID
  JOIN FO880PRD.dbo.PS_FO_ASSIGNMENT FOASSN WITH(NOLOCK) ON
        PRJACT.PC_SCH_FIELD2 = FOASSN.ORDER_ID
    AND PRJACT.PC_SCH_FIELD1 = FOASSN.BUSINESS_UNIT
    AND PRJACT.PC_SCH_FIELD5 = FOASSN.FO_ASSIGNMENT_ID
  JOIN FO880PRD.dbo.PS_FO_ASGN_ERN_DTL ERN WITH(NOLOCK) ON
       ERN.BUSINESS_UNIT = FOASSN.BUSINESS_UNIT
   AND ERN.ORDER_ID = FOASSN.ORDER_ID
   AND ERN.FO_ASSIGNMENT_ID = FOASSN.FO_ASSIGNMENT_ID
   AND ERN.DETAIL_NO = 1
 WHERE
        HDR.INVOICE = @INVOICE
    AND LINE.EMPLID = @EMPLID
    AND ERN.EFFDT = (SELECT MAX(B.EFFDT)
                         FROM FO880PRD.dbo.PS_FO_ASGN_ERN_DTL B WITH(NOLOCK)
                         WHERE B.BUSINESS_UNIT = ERN.BUSINESS_UNIT AND
                               B.FO_ASSIGNMENT_ID = ERN.FO_ASSIGNMENT_ID AND
                               B.ORDER_ID = ERN.ORDER_ID AND
                               B.DETAIL_NO = ERN.DETAIL_NO AND
                               B.EFFDT <= LINE.CHARGE_FROM_DT
                        )
            )
END
GO
COMMIT

Can anyone explain how I should structure the select statement so that what appears to be a problem with the "scope" of the LINE and ERN aliases is resolved?Start Free Trial
[+][-]07.18.2008 at 09:34AM PDT, ID: 22037572

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 14-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]07.18.2008 at 09:40AM PDT, ID: 22037627

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 14-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]07.18.2008 at 11:33AM PDT, ID: 22038503

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 14-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]07.18.2008 at 12:44PM PDT, ID: 22039124

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 14-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]07.18.2008 at 01:12PM PDT, ID: 22039381

View this solution now by starting your 14-day free trial. Setting up your free trial is quick, easy, and secure. We will return you to this solution, unlocked, when you're done.

 

About this solution

Zones: SQL Query Syntax, SQL Server 2005
Tags: T-Sql, Internet Explorer 7, The column prefix does not match with a table name or alias name used in the query.
Sign Up Now!
Solution Provided By: angelIII
Participating Experts: 2
Solution Grade: A
 
 
[+][-]07.25.2008 at 08:34AM PDT, ID: 22089481

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 14-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]07.25.2008 at 10:59AM PDT, ID: 22090848

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 14-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]07.25.2008 at 11:20AM PDT, ID: 22091025

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 14-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]07.25.2008 at 11:22AM PDT, ID: 22091049

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 14-day free trial to view this Expert Comment or ask the Experts your question.

 
 
Loading Advertisement...
20081112-EE-VQP-43 / EE_QW_2_20070628