Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
| Author |
Topic |
|
basicconfiguration
Constraint Violating Yak Guru
358 Posts |
Posted - 2011-10-20 : 14:06:14
|
| I want to get documnetid and partnerid. In my criteria i have @type hard-coded however I want it to be dynamic.I want to be able to replace from this Date.value('(/Dates/Date[@Type="Export-Ad"]/@Value)[1]', 'varchar(50)') to Date.value('(/Dates/Date[@Type="@type"]/@Value)[1]', 'varchar(50)'). HOpefully makes sense.I also have a entitytype table with all the types if it helps.declare @entitytype table (typename varchar(30))insert @entitytypeselect 'Export-Ad' union allselect 'Import-Ad' union allselect 'Emails' select * from @entitytypedeclare @PartnerDocument table (documentid int, partnerid int, date xml)insert @PartnerDocumentselect 1, 20, '<Dates> <Date Type="Export-Ad" Value="2011-10-19T18:16:09.880" /></Dates>' union allselect 2, 21, '<Dates> <Date Type="Import-Ad" Value="2010-11-19T18:16:09.880" /></Dates>' union allselect 3, 22, '<Dates> <Date Type="Emails" Value="2009-11-19T18:16:09.880" /></Dates>'select * from @PartnerDocument/*SELECT DocumentID, partnerid FROM @PartnerDocumentWHERE Date.value('(/Dates/Date[@Type="Export-Ad"]/@Value)[1]', 'varchar(50)') IS NOT NULL */ declare @type varchar(30)declare @DocumentID intdeclare @partnerid intDECLARE Employee_Cursor CURSOR FORSELECT DocumentID, partnerid FROM @PartnerDocument WHERE Date.value('(/Dates/Date[@Type="Export-Ad"]/@Value)[1]', 'varchar(50)') IS NOT NULLOPEN Employee_Cursor;FETCH NEXT FROM Employee_Cursor into @DocumentID, @partneridWHILE @@FETCH_STATUS = 0 BEGIN SELECT @DocumentID, @partnerid FETCH NEXT FROM Employee_Cursor into @DocumentID, @partnerid ENDCLOSE Employee_CursorDEALLOCATE Employee_Cursor |
|
|
nathans
Aged Yak Warrior
938 Posts |
Posted - 2011-10-20 : 21:14:23
|
I think I understand... let me know if this is in right direction:declare @DateType varchar(25);set @DateType = 'Emails';-- if only ever 1 Date element per document:select DocumentId, PartnerIdfrom @PartnerDocument pdwhere pd.[date].exist('(Dates/Date)[1][@Type = sql:variable("@DateType")]') = 1;-- if > 1 Date element:select DocumentId, PartnerIdfrom @PartnerDocument pdcrossapply pd.[date].nodes('Dates') p(n)where p.n.exist('Date[@Type = sql:variable("@DateType")]') = 1;Nathan Skerl |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2011-10-21 : 00:32:36
|
| [code]declare @entitytype table (typename varchar(30))insert @entitytypeselect 'Export-Ad' union allselect 'Import-Ad' union allselect 'Emails' select * from @entitytypedeclare @PartnerDocument table (documentid int, partnerid int, date xml)insert @PartnerDocumentselect 1, 20, '<Dates><Date Type="Export-Ad" Value="2011-10-19T18:16:09.880" /></Dates>' union allselect 2, 21, '<Dates><Date Type="Import-Ad" Value="2010-11-19T18:16:09.880" /></Dates>' union allselect 3, 22, '<Dates><Date Type="Emails" Value="2009-11-19T18:16:09.880" /></Dates>'select *from (select t.u.value('./@Type[1]','varchar(100)') as [type], t.u.value('./@Value[1]','datetime') AS [dateval] from @PartnerDocument p cross apply [date].nodes('/Dates/Date')t(u))m inner join @entitytype et on et.typename = m.type[/code]------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
basicconfiguration
Constraint Violating Yak Guru
358 Posts |
Posted - 2011-10-24 : 15:55:48
|
| Thanks v |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2011-10-25 : 00:17:22
|
| wc------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
|
|
|
|
|