We help IT Professionals succeed at work.
Get Started

Dynamic Query Error:  Is Not a Valid Identifier

954 Views
Last Modified: 2010-04-21
I need help creating a dynamic query that references table valued functions that have parameters passed.  The below dynamic query generates the error below the query.  Please advise how to resolve:

Declare @SQL nVarChar(max)

Set @SQL =
'SELECT V.YR AS [Budget Year], V.Project_Number, V.Task
FROM  dbo.vEFF_NAFTA_WORKSLATE_REPORT AS V       
        LEFT OUTER JOIN dbo.tvfLatestStatus('+ cast(@HeaderID as nvarchar) + ') AS LS ON V.Trial_NO = LS.TRIAL_NUMBER_NAME
      LEFT OUTER JOIN dbo.tvfGDBLoad(' + cast(@HeaderID as nvarchar) + ') AS GL ON V.Trial_NO = GL.TRIAL_NUMBER
        LEFT OUTER JOIN  tvfCountPercentages(#' + Convert(nvarchar(max), @YearEntered, 101) + '#) AS P ON V.Trial_NO = P.TRIAL_NUMBER_NAME
      LEFT JOIN tvfCopStat(' + cast(@HeaderID as nvarchar) + ', ' + cast(@Year as nvarchar) +  ') AS C ON V.Trial_NO = C.[TRIAL NUMBER]
WHERE       V.YR=' + cast(@Year as nvarchar)

********************************************************************
Msg 203, Level 16, State 2, Procedure spIH_COI_CRO_Workslate, Line 115
The name 'SELECT V.YR AS [Budget Year],       V.Project_Number, V.Task
FROM  dbo.vEFF_NAFTA_WORKSLATE_REPORT AS V       
        LEFT OUTER JOIN dbo.tvfLatestStatus(1) AS LS ON V.Trial_NO = LS.TRIAL_NUMBER_NAME
      LEFT OUTER JOIN dbo.tvfGDBLoad(1) AS GL ON V.Trial_NO = GL.TRIAL_NUMBER
        LEFT OUTER JOIN  tvfCountPercentages(#12/31/2006#) AS P ON V.Trial_NO = P.TRIAL_NUMBER_NAME
      LEFT JOIN tvfCopStat(1, 2007) AS C ON V.Trial_NO = C.[TRIAL NUMBER]
WHERE       V.YR=2007' is not a valid identifier.

Comment
Watch Question
Billing Engineer
CERTIFIED EXPERT
Most Valuable Expert 2014
Top Expert 2009
Commented:
This problem has been solved!
Unlock 1 Answer and 3 Comments.
See Answer
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE