views:

30

answers:

1

this is what my query looks like:

select top 5   cast(content_html as xml) as [prodxml],
prodxml.value('data(ClassTemplate[1]', 'nvarchar(max) ') as prod2
from content 
where 
end_date >= getdate()
and folder_id != 71682 

and i keep getting:

Msg 4121, Level 16, State 1, Line 1
Cannot find either column "prodxml" or the user-defined function or aggregate "prodxml.value", or the name is ambiguous.

what am i doing wrong??

+2  A: 

i can't query prod1 directly, how else can i find all records that have "Other" as the Class Template?

You can't reference a column alias in another column of the same SELECT statement - use:

SELECT TOP 5   
       CAST(t.content_html AS XML).value('(/root/ClassTemplate)[1]', 'NVARCHAR(max)') AS prod2
  FROM CONTENT t
 WHERE t.end_date >= getdate()
   AND t.folder_id != 71682 

If you want to filter out based on the prod2 value in the WHERE clause - use:

  FROM CONTENT t
 WHERE CAST(t.content_html AS XML).value('(/root/ClassTemplate)[1]', 'NVARCHAR(max)') = 'Other'
   AND t.end_date >= getdate()
   AND t.folder_id != 71682 
OMG Ponies
select top 5 cast(content_html as xml).value('data(ClassTemplate[1]', 'nvarchar(500)) ') as prod1from content where content_status = 'A' and content_type=3333and end_date >= getdate()and folder_id != 71682 and i am now getting: The data type 'nvarchar(500)) ' used in the VALUE method is invalid.
Tanya Xrum
@Tanya Xrum: There's a typo in what you posted - `'nvarchar(500))` should be `nvarchar(500)`
OMG Ponies
thanks, now i get 5 nulls..i should be getting the values of the "ClassTemplate" node, right?
Tanya Xrum
There's a missing close bracket I think? `'data(ClassTemplate[1]'` should be `'data(ClassTemplate[1])'`
Martin Smith
@Tanya Xrum: The query is looking for a `data(ClassTemplate` node - can you post a sample of the XML?
OMG Ponies
<root><ClassTemplate>Other</ClassTemplate> <Default><ClassTitle>test class</ClassTitle> <ClassDescription>description</ClassDescription> <ClassType_>Demonstration</ClassType_> <Location>Bethesda</Location> <Instructor>[email protected]</Instructor> <Menu>menu </Menu> <StartDate>2010-10-17</StartDate> <EndHours>01</EndHours> <EndMinutes>00</EndMinutes> <StartTime>AM</StartTime> <EndDate>2010-11-06</EndDate> <StartHours>01</StartHours> <StartMinutes>00</StartMinutes> <EndTime>AM</EndTime> <ClassID></ClassID> </Default> </root>
Tanya Xrum
hm, if i can't query prod1 directly, how else can i find all records that have "Other" as the Class Template?
Tanya Xrum
@Tanya Xrum: I updated my answer based on the XML example.
OMG Ponies
@Tanya Xrum: I don't see how, based on the sample you provided - I tested on SQL Server 2005.
OMG Ponies
Tanya Xrum