whats the best way to handle a complex param in a stored proc?
whats the best way to handle a complex param in a stored proc?
let me explain a little about the param so its clear what i am trying to do h开发者_开发百科ere...
I have a project management app - so i have things like a release, a project and milestones. A project has a single release and a release has its milestones.
i am adding reporting and need to generate a report that allows users to select any number of releases and any milestones of a release. this will result in a report that shows all the projects that are part of the release and their data for the milestones selected.
since each release has multiple milestones (milestone table) to keep the associations of what milestone was selected in what release i was thinking to have coma separated list like this to pass the UI data to SQL.
release1 | m2, m3, m4 release2 | m2, m7 release3 | m5
as a varchar or maybe xml...
whats the best practice for sending in something like this that has relational data built into the param? am i way off and over thinking the problem?
please tell me there is a simple solution i am not seeing...
XML would be the best way and simplest with least amount of code... see a sample below.
As starter sample:
DECLARE @Param XML
SET @Param = '
<chriteria>
<release id="1">
<milestone id="1" />
<milestone id="2" />
<milestone id="3" />
</release>
<release id="2">
<milestone id="1" />
<milestone id="2" />
</release>
</chriteria>
'
SELECT c.value('../@id', 'INT') AS ReleaseId, c.value('@id', 'INT') AS MilestoneId
FROM @Param.nodes('/chriteria/release/milestone') AS T(c)
SQL Server 2008 has Table Valued Parameters to solve just this problem. For SQL Server 2005, you will have to either pass in a string to parse in the Stored Proc, or have your caller and Stored Proc use some shared (or temporary) table to hold the data to process.
精彩评论