Re: specify criteria dynamically with OpenXML
From: RobKaratzas (RobKaratzas_at_discussions.microsoft.com)
Date: 02/21/05
- Next message: Adam Machanic: "Re: OPENXML and "invalid" characters in a text"
- Previous message: Adam Machanic: "Re: specify criteria dynamically with OpenXML"
- In reply to: Adam Machanic: "Re: specify criteria dynamically with OpenXML"
- Messages sorted by: [ date ] [ thread ]
Date: Mon, 21 Feb 2005 09:59:03 -0800
hi adam
yep, you're right...
but for some real goofy reasons, I need to process it in a loop. (Might be
running an array of process steps, etc.)
But here's how I've done this (just for testing...):
---------------------------------------------
DECLARE @hDoc int,@done int, @SQL nvarchar(4000),@parms decimal(10),@StepID
nchar(5), @IsActive nchar(1),@StepName nchar(255), @statementID nchar(255),
@statement nvarchar(4000),@xmlID int,@xmlStepName varchar(255),@xmlIsActive
varchar(1)
set @parms=1
set @statementID=1
set @xmlID=1
set @xmlStepName = 'Process Part'
set @xmlIsActive = 'Y'
set @done=0
EXEC sp_xml_preparedocument @hDoc OUTPUT,
N'<ROOT>
<Steps StepID="1" IsActive="Y" StepName="Process Part">
<statements statementID="1"
sql="select..."/>
<statements statementID="2"
sql="INSERT INTO PRT_PRT SELECT TOP 1 RefID, IsActive, MetID,
PartName, PartDesc, MAX(VersionNo) + 1 AS VersionNo FROM dbo.PRT_PRT WHERE
(PrtID = ?) GROUP BY RefID, IsActive, MetID, PartName, PartDesc"/>
</Steps>
<Steps StepID="2" IsActive="Y" StepName="Process Meta">
<statements statementID="1"
sql="select..."/>
<statements statementID="2"
sql="insert..."/>
</Steps>
<Steps StepID="3" IsActive="N" StepName="Process Post">
<!--statements to be completed for job x by 3/1/2005...-->
</Steps>
</ROOT>'
WHILE @done=0
BEGIN
-- Use OPENXML to provide rowset consisting of customer data.
SET
@SQL='/ROOT/Steps[@StepName='+char(39)+@xmlStepName+char(39)+'][@IsActive='+char(39)+@xmlIsActive+char(39)+']/statements[@statementID='+char(39)+CONVERT(VARCHAR(2),@xmlID)+char(39)+']'
PRINT @SQL
SELECT
@StepID=StepID,@IsActive=IsActive,@StepName=StepName,@statementID=statementID,@statement=sql
FROM OPENXML(@hDoc, @SQL)
with (StepID nchar(5) '../@StepID', IsActive nchar(1)
'../@IsActive',StepName nchar(255) '../@StepName',
statementID nchar(255), sql nvarchar(4000))
print replicate('-',20)
print @StepID
print @IsActive
print @StepName
print @statementID
print @statement
print replicate('-',20)
If @xmlID = convert(int,@statementID)
BEGIN
PRINT 'process statement: '+convert(varchar(2),@statementID)+'...'
set @xmlID=@xmlID+1
END
ELSE
BEGIN
set @done=1
END
END
-- set @sql=replace(@sql,'?',@parms)
-- Remove the internal representation of the XML document.
-- set @sql=replace(@sql,'?',@parms)
-- Remove the internal representation of the XML document.
EXEC sp_xml_removedocument @hDoc
---------------------------------------------
"Adam Machanic" wrote:
> I think it would be quite a bit more efficient in this case to either insert
> all of the rows into a temp table and then loop over that, or write a cursor
> over the results of a single call to OPENXML. That way the XML would need
> to be processed only once.
>
> --
> Adam Machanic
> SQL Server MVP
> http://www.sqljunkies.com/weblog/amachanic
> --
>
>
> "RobKaratzas" <RobKaratzas@discussions.microsoft.com> wrote in message
> news:6CC29806-BB59-417C-A629-22BD853871A8@microsoft.com...
> > got it, u need to do something like this (do the assignments prior to the
> > OPENXML):
> >
> > SET @SQL='/ROOT/Steps[@StepName=''Process
> > Part''][@IsActive=''Y'']/statements[@statementID=''2'']'
> > SELECT
> >
> @StepID=StepID,@IsActive=IsActive,@StepName=StepName,@statementID=statementI
> D,@statement=sql
> > FROM OPENXML(@hDoc, @SQL)
> > with (StepID nchar(5) '../@StepID', IsActive nchar(1)
> > '../@IsActive',StepName nchar(255) '../@StepName',
> > statementID nchar(255), sql nvarchar(4000))
> >
> > rob
> >
> >
> > "RobKaratzas" wrote:
> >
> > > Hi Folks
> > >
> > > I need to put the following OPENXML into a while loop (to process each
> row
> > > individually), but don't seem to be able to apply a filter using
> variables.
> > >
> > > Don't seem to be able to find the docs/syntax to use variables here:
> > >
> > > [@StepName=''Process
> Part''][@IsActive=''Y'']/statements[@statementID=''1'']'
> > >
> > > I need to loop on @StepName and @statementID (IsActive will stay
> constant).
> > >
> > > Please see this code below, which you can run in Query Analyzer:
> > >
> > >
> > >
> > > DECLARE @hDoc int,@SQL nvarchar(4000),@parms decimal(10),@StepID
> nchar(5),
> > > @IsActive nchar(1),@StepName nchar(255), @statementID nchar(255),
> @statement
> > > nvarchar(4000)
> > > set @parms=1
> > > EXEC sp_xml_preparedocument @hDoc OUTPUT,
> > > N'<ROOT>
> > > <Steps StepID="1" IsActive="Y" StepName="Process Part">
> > > <statements statementID="1"
> > > sql="select..."/>
> > > <statements statementID="2"
> > > sql="INSERT INTO PRT_PRT SELECT TOP 1 RefID,
> > > IsActive,
> > > MetID,
> > > PartName,
> > > PartDesc,
> > > MAX(VersionNo) + 1 AS VersionNo
> > > FROM dbo.PRT_PRT
> > > WHERE (PrtID = ?)
> > > GROUP BY
> > > RefID, IsActive, MetID, PartName, PartDesc"/>
> > > </Steps>
> > > <Steps StepID="2" IsActive="Y" StepName="Process Meta">
> > > <statements statementID="1"
> > > sql="select..."/>
> > > <statements statementID="2"
> > > sql="insert..."/>
> > > </Steps>
> > > <Steps StepID="3" IsActive="N" StepName="Process Post">
> > > <!--statements to be completed for job x by 3/1/2005...-->
> > > </Steps>
> > > </ROOT>'
> > > -- Use OPENXML to provide rowset consisting of customer data.
> > > SELECT
> > >
> @StepID=StepID,@IsActive=IsActive,@StepName=StepName,@statementID=statementI
> D,@statement=sql
> > > FROM OPENXML(@hDoc, N'/ROOT/Steps[@StepName=''Process
> > > Part''][@IsActive=''Y'']/statements[@statementID=''1'']')
> > > with (StepID nchar(5) '../@StepID', IsActive nchar(1)
> > > '../@IsActive',StepName nchar(255) '../@StepName',
> > > statementID nchar(255), sql nvarchar(4000))
> > >
> > > print replicate('-',20)
> > > print @StepID
> > > print @IsActive
> > > print @StepName
> > > print @statementID
> > > print @statement
> > > print replicate('-',20)
> > >
> > > -- Remove the internal representation of the XML document.
> > > EXEC sp_xml_removedocument @hDoc
> > >
> > > Thanks Rob
>
>
>
- Next message: Adam Machanic: "Re: OPENXML and "invalid" characters in a text"
- Previous message: Adam Machanic: "Re: specify criteria dynamically with OpenXML"
- In reply to: Adam Machanic: "Re: specify criteria dynamically with OpenXML"
- Messages sorted by: [ date ] [ thread ]
Relevant Pages
|