• SQL006 (1/14/2013)


    thanks vinu

    Now i am trying to join this query with a table to get the desire results

    CREATE TABLE #tProduct

    (

    tProductID bigint PRIMARY KEY,

    tProductCode nvarchar(128),

    tProductName nvarchar(256),

    tManufacturerName nvarchar(256),

    tProductDescription nvarchar(4000)

    )

    EXECUTE ('SELECT *

    FROM #tProduct AS P

    JOIN(SELECT ProductID, +STUFF((Select DISTINCT '',MAX(Case When ParentFeatureName = '' + CHAR(39) + ParentFeatureName + CHAR(39) + '' Then ChilFeatureName Else '' + CHAR(39) + CHAR(39) + '' End) As '' + ParentFeatureName From #Test For XML PATH('')),1,1,'''')

    + FROM #Test Group By ProductID'' ) AS A ON P.tProductID = A.ProductID')

    I am getting this error

    Msg 156, Level 15, State 1, Line 4

    Incorrect syntax near the keyword 'AS'.

    First suggestion I have is build the dynamic query into a variable. Second, PRINT it out so you can see the query itself to be sure it is what you expect. If you print it, you can copy it to another query window to test that it actually runs. This is what I do when writing a dynamic query as it is sometimes hard to be sure you have the correct number of single quotes in the correct places otherwise.