Click here to Skip to main content
13,197,767 members (43,002 online)
Click here to Skip to main content
Add your own
alternative version


20 bookmarked
Posted 29 May 2007

Pass Serialized Tabular Data as a Stored Proc Parameter

, 29 May 2007
Rate this:
Please Sign up or sign in to vote.
How to solve the age-old problem of passing complex structures as parameters to sprocs


This article attempts to solve the age-old issue of passing complex tabular data structures into sprocs as parameters. I have seen numerous implementations ranging from simple and elegant to complex and verbose. While this solution may have its flaws, it is simple, quick and very extensible!

No while loops or casting data types...

What I wanted was, ideally, the ability to pass a serialized result set into a stored proc as a parameter, without having to do lots of nested loops, string parsing and casting of varchars to other data types. The result was so simple that I couldn't believe I hadn't thought of it sooner.

Using the code

Below is an example of how it works. You can extrapolate the same concept for multiple parameters instead of just one, but here goes...

One example is worth a thousand words, so I used the common scenario of Order and OrderItems:

create proc addOrder @customerid int, @paymentauthid varchar(50), 
    @orderitems varchar(8000), @deliveryid int
begin tran
    --create orderitem hash table
    create table #orderitem(prodid int not null, 
        qty int not null, price money)
    declare @sql varchar(8000), @orderId int
    --construct insert for all orderitem rows
    set @sql = 'insert #orderitem(prodid, qty, price) select ' + 
        replace(@orderitems, ';', ' union select ')

    insert orders(customerid, orderdate, orderstatus, paymentauthid, 
    values(@customerid, getdate(),1, @paymentauthid, @deliveryid )
    select @orderid =  @@identity from orders
    insert orderitem(orderid, prodid, qty, price)
    select @orderid, prodid, qty, price from #orderitem

    if @@error <> 0 begin
        rollback tran
    commit tran

First, create a hash table representing the tabular data structure that you would like to pass in as a parameter, in this case for @orderitems.

create table #orderitem(prodid int not null, qty int not null, price money)

Instead of looping and casting substrings of the "@orderitems" variable, simply construct this simple insert statement and replace your row delimiter ';' with ' union select '.

set @sql = 'insert #orderitem(prodid, qty, price) select ' + 
    replace(@orderitems, ';', ' union select ')

Finally, execute the constructed SQL statement to populate your hash table, which you can then use for whatever purpose you like.


Calling the proc is now as simple as...

addOrder 1, 'ABC123', '1,2, 22.50; 2,4, 110; 3,6, 250.30; 
    4,8, 32.25; 5,10, 20; 6,12, 1000.99; 7,14, 211.45', 2

Points of interest

This is not the first solution to this problem and it certainly won't be the last, but for me it is really simple and, above all, fast to implement.


  • 29 May, 2007 - Original version posted


This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


About the Author

Web Developer
South Africa South Africa
Software Engineer. Programming since 1992.
C#, C/C++, VB/VB.NET, SQL, PowerBuilder, HTML, XML, Javascript, ActionScript, Flex, JSON, ASP/ASP.NET, PHP, Delphi.

I specialise in Enterprise Web2 Applications.

You may also be interested in...


Comments and Discussions

GeneralIt would be better to use XML in an Image parameter of sproc and when use OpenXml Pin
v_rus17-Aug-08 22:59
memberv_rus17-Aug-08 22:59 
GeneralThanks Pin
c_paresh5-Jun-07 14:10
memberc_paresh5-Jun-07 14:10 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.171020.1 | Last Updated 29 May 2007
Article Copyright 2007 by habanero
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid