SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Parsing Parameters in a Stored Procedure


Parsing Parameters in a Stored Procedure

Author
Message
janusz pazgier
janusz pazgier
SSC Journeyman
SSC Journeyman (83 reputation)SSC Journeyman (83 reputation)SSC Journeyman (83 reputation)SSC Journeyman (83 reputation)SSC Journeyman (83 reputation)SSC Journeyman (83 reputation)SSC Journeyman (83 reputation)SSC Journeyman (83 reputation)

Group: General Forum Members
Points: 83 Visits: 11
Nice article to provide help for parsing delimited values, but I like better XML version provided by some user in the comments which is simpler but maybe more resource consuming.
But my question is why this article has title :parsing stored procedure parameters if you just looking for specific switch value in single parameter? You are not looking for pair Name=Value, just for values. Could you provide example how you use values received from your parsing process? Because maybe all your work is not really required if you next do something based on your parsed values using for example CASE statement with fixed values and you have to do word search again later in he code.
Could you provide example of usage of parameter values received from your parse routine?
Jeff Moden
Jeff Moden
SSC Guru
SSC Guru (433K reputation)SSC Guru (433K reputation)SSC Guru (433K reputation)SSC Guru (433K reputation)SSC Guru (433K reputation)SSC Guru (433K reputation)SSC Guru (433K reputation)SSC Guru (433K reputation)

Group: General Forum Members
Points: 433355 Visits: 43494
jcboyer-1091017 (8/10/2010)
I have been using an XML string to achieve the same results. Not sure that it is faster, but it is quite flexible as:

EXEC sp_xml_preparedocument @idoc OUTPUT, @XMLParameters
insert into #T
select *
FROM OPENXML (@idoc, '/OrderGroup/Item', 1)
WITH #T
EXEC sp_xml_removedocument @idoc

In this example #T is a large table with over 20 columns. @XMLParameters is varchar(max) to pass as many rows as necessary at once, which is and added bonus, because there is still no Table as parameter. (I am using varchar instead of XML because XML is more strict and we do not need this here.)

Comments on this approach are welcome.

Jerome


Hi Jerome,

Can you provide an example of what the content of @XMLParameters would be for your code? I don't want to presume. Thanks.

--Jeff Moden

RBAR is pronounced ree-bar and is a Modenism for Row-By-Agonizing-Row.
First step towards the paradigm shift of writing Set Based code:
Stop thinking about what you want to do to a row... think, instead, of what you want to do to a column.
If you think its expensive to hire a professional to do the job, wait until you hire an amateur. -- Red Adair

Helpful Links:
How to post code problems
How to post performance problems
Forum FAQs
TheSQLGuru
TheSQLGuru
SSC Guru
SSC Guru (65K reputation)SSC Guru (65K reputation)SSC Guru (65K reputation)SSC Guru (65K reputation)SSC Guru (65K reputation)SSC Guru (65K reputation)SSC Guru (65K reputation)SSC Guru (65K reputation)

Group: General Forum Members
Points: 65969 Visits: 8838
Didn't we have a MASSIVE thread here on SSC.com on string parsing, with amazing code samples and wonderful benchmarks??

Best,
Kevin G. Boles
SQL Server Consultant
SQL MVP 2007-2012
TheSQLGuru on googles mail service
Jeff Moden
Jeff Moden
SSC Guru
SSC Guru (433K reputation)SSC Guru (433K reputation)SSC Guru (433K reputation)SSC Guru (433K reputation)SSC Guru (433K reputation)SSC Guru (433K reputation)SSC Guru (433K reputation)SSC Guru (433K reputation)

Group: General Forum Members
Points: 433355 Visits: 43494
I'm torn. The article is well written and the code examples are pertinent to the article. However...

It was a quick read I did through the article so I may have missed something, but I didn't see anywhere where non-validated "elements" in the input string were handled. I saw where an "alert" was created if there were no matches, but didn't see anywhere individual elements would produce an alert if the existed.

For all of you good folks using XML to split things... I suppose that XML either does something special for you or that your consider its performance to be good enough. Just be aware that there are faster methods than XML... sometimes a lot faster.

Last but not least, this reminds me of the old "LIKE" method which also failed to notify if a single element of a parameter could not be validated with the only difference being that the validation list is table driven instead of built into code.

I'll try to post some test code tonight to show all of what I believe can happen with the code from the article. I might even have time to show why I don't use XML splitter code.

--Jeff Moden

RBAR is pronounced ree-bar and is a Modenism for Row-By-Agonizing-Row.
First step towards the paradigm shift of writing Set Based code:
Stop thinking about what you want to do to a row... think, instead, of what you want to do to a column.
If you think its expensive to hire a professional to do the job, wait until you hire an amateur. -- Red Adair

Helpful Links:
How to post code problems
How to post performance problems
Forum FAQs
khayot.khalimov
khayot.khalimov
Valued Member
Valued Member (63 reputation)Valued Member (63 reputation)Valued Member (63 reputation)Valued Member (63 reputation)Valued Member (63 reputation)Valued Member (63 reputation)Valued Member (63 reputation)Valued Member (63 reputation)

Group: General Forum Members
Points: 63 Visits: 68
There is a cute solution which i borrowed from some articles about Tallys on this Web-Site.
Check this out: http://www.sqlservercentral.com/articles/T-SQL/63003/

Here is how i implemented:

1. Create a Tally table. This is simple table with one Column N, which contains (for instance) numbers from 1 to 11000.

--Create a Tally Table

--===== Create and populate the Tally table on the fly SELECT TOP 11000 --equates to more than 30 years of dates IDENTITY(INT,1,1) AS N INTO dbo.Tally FROM Master.dbo.SysColumns sc1, Master.dbo.SysColumns sc2

--===== Add a Primary Key to maximize performance ALTER TABLE dbo.Tally ADD CONSTRAINT PK_Tally_N PRIMARY KEY CLUSTERED (N) WITH FILLFACTOR = 100

--===== Allow the general public to use it GRANT SELECT ON dbo.Tally TO PUBLIC



2. Create a parsing function:

create function [dbo].[FUNC_TBL_TEXT_IN_COL]
(
@Numbers varchar(max)
,@Delimiter char
) returns table

as

return SELECT SUBSTRING(@Delimiter+@Numbers+@Delimiter,N+1,CHARINDEX(@Delimiter,@Delimiter+@Numbers+@Delimiter,N+1)-N-1) as X
FROM dbo.Tally
WHERE N < LEN(@Delimiter+@Numbers+@Delimiter)
AND SUBSTRING(@Delimiter+@Numbers+@Delimiter,N,1) = @Delimiter

3. Run an example:

select X
from [dbo].[FUNC_TBL_TEXT_IN_COL]
(
'Jones,Schneider,68,whatever',','
)

Result is:

X
----
Jones
Schneider
68
whatever
Jeff Moden
Jeff Moden
SSC Guru
SSC Guru (433K reputation)SSC Guru (433K reputation)SSC Guru (433K reputation)SSC Guru (433K reputation)SSC Guru (433K reputation)SSC Guru (433K reputation)SSC Guru (433K reputation)SSC Guru (433K reputation)

Group: General Forum Members
Points: 433355 Visits: 43494
Be careful with the code in the article. Be aware that it doesn't do full validation. It only evaluates what's right and give no indication as to what is wrong. If that's good for you, so be it (although I believe there are better ways). Just make sure it's good for you. ;-)

The following example has been copied from the article code. "XX" has been added to the parameter. See for yourself that "XX" produces no error and no alert because "XX" isn't even considered in the validation.

-- Imagine this table exists previously in your database ----
Declare @T_STATES table ( state char(2) )

Insert @T_STATES Values ( 'AZ' )
Insert @T_STATES Values ( 'CA' )
Insert @T_STATES Values ( 'MT' )
Insert @T_STATES Values ( 'TX' )
Insert @T_STATES Values ( 'WA' )
Insert @T_STATES Values ( 'TS' )

-- Suppose this is the parameter ----
Declare @sParameter varchar(8000)
Set @sParameter = 'WA,AZ,CA,TS,XX' --<<< Code doesn't catch the invalid parameter

Declare @tStates table ( state char(2) )

Insert @tStates
Select state
From @T_STATES
Where CharIndex( state, @sParameter ) > 0

Select *
From @tStates




Even the final bit of code doesn't catch the fact that an invalid element was passed...

-- Imagine this table exists previously in your database ----
Declare @T_STATES table ( state char(2) )

Insert @T_STATES Values ( 'AZ' )
Insert @T_STATES Values ( 'CA' )
Insert @T_STATES Values ( 'MT' )
Insert @T_STATES Values ( 'TX' )
Insert @T_STATES Values ( 'WA' )
Insert @T_STATES Values ( 'TS' )

-- Suppose this is the parameter ----
Declare @sParameter varchar(8000)
Set @sParameter = 'WA,AZ,CA,TS,XX' --<<< Code doesn't catch the invalid parameter

Declare @tStates table ( state char(2) )

Insert @tStates
Select state
From @T_STATES
Where CharIndex( state, @sParameter ) > 0

If @@RowCount < 1
Begin
Select 'No matches. Valid values are:' As InfoMessage
Union All
Select state
From @T_STATES
Return
End




--Jeff Moden

RBAR is pronounced ree-bar and is a Modenism for Row-By-Agonizing-Row.
First step towards the paradigm shift of writing Set Based code:
Stop thinking about what you want to do to a row... think, instead, of what you want to do to a column.
If you think its expensive to hire a professional to do the job, wait until you hire an amateur. -- Red Adair

Helpful Links:
How to post code problems
How to post performance problems
Forum FAQs
jcboyer-1091017
jcboyer-1091017
SSC-Enthusiastic
SSC-Enthusiastic (113 reputation)SSC-Enthusiastic (113 reputation)SSC-Enthusiastic (113 reputation)SSC-Enthusiastic (113 reputation)SSC-Enthusiastic (113 reputation)SSC-Enthusiastic (113 reputation)SSC-Enthusiastic (113 reputation)SSC-Enthusiastic (113 reputation)

Group: General Forum Members
Points: 113 Visits: 89
something like:
set @XMLParameters =
'<OrderGroup>
<Item TargetPrinterType="DIAMOND_X" GroupStatus="ACTIVE_cc" ReferenceNo="Joe" />
</OrderGroup>'
tom.groszko
tom.groszko
Valued Member
Valued Member (62 reputation)Valued Member (62 reputation)Valued Member (62 reputation)Valued Member (62 reputation)Valued Member (62 reputation)Valued Member (62 reputation)Valued Member (62 reputation)Valued Member (62 reputation)

Group: General Forum Members
Points: 62 Visits: 187
With SQL2008 you do have the option of a table variable. I was using XML and found that the table variable provided all the flexibility but was much faster.
Jeff Moden
Jeff Moden
SSC Guru
SSC Guru (433K reputation)SSC Guru (433K reputation)SSC Guru (433K reputation)SSC Guru (433K reputation)SSC Guru (433K reputation)SSC Guru (433K reputation)SSC Guru (433K reputation)SSC Guru (433K reputation)

Group: General Forum Members
Points: 433355 Visits: 43494
tom.groszko (8/10/2010)
With SQL2008 you do have the option of a table variable. I was using XML and found that the table variable provided all the flexibility but was much faster.


Any chance of you posting some code that others can run to demonstrate that?

--Jeff Moden

RBAR is pronounced ree-bar and is a Modenism for Row-By-Agonizing-Row.
First step towards the paradigm shift of writing Set Based code:
Stop thinking about what you want to do to a row... think, instead, of what you want to do to a column.
If you think its expensive to hire a professional to do the job, wait until you hire an amateur. -- Red Adair

Helpful Links:
How to post code problems
How to post performance problems
Forum FAQs
Jeff Moden
Jeff Moden
SSC Guru
SSC Guru (433K reputation)SSC Guru (433K reputation)SSC Guru (433K reputation)SSC Guru (433K reputation)SSC Guru (433K reputation)SSC Guru (433K reputation)SSC Guru (433K reputation)SSC Guru (433K reputation)

Group: General Forum Members
Points: 433355 Visits: 43494
jcboyer-1091017 (8/10/2010)
something like:
set @XMLParameters =
'<OrderGroup>
<Item TargetPrinterType="DIAMOND_X" GroupStatus="ACTIVE_cc" ReferenceNo="Joe" />
</OrderGroup>'


Thanks, Jerome...

--Jeff Moden

RBAR is pronounced ree-bar and is a Modenism for Row-By-Agonizing-Row.
First step towards the paradigm shift of writing Set Based code:
Stop thinking about what you want to do to a row... think, instead, of what you want to do to a column.
If you think its expensive to hire a professional to do the job, wait until you hire an amateur. -- Red Adair

Helpful Links:
How to post code problems
How to post performance problems
Forum FAQs
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum







































































































































































SQLServerCentral


Search