web 2.0

Get Column name From Stored Procedure

The requirement of the day is to extract the name of the columns returned by procedures. Stored Procedures are dynamic that is why we need to create a function that takes Stored Procedure name as parameter and return the column names in string. So here is the quick snippet for that

   1: Public Shared Function getMetaData(ByVal spName As String) As String()
   2:        Dim sqlCon As New SqlConnection(ConfigurationManager.ConnectionStrings("lmString").ConnectionString)
   3:        sqlCon.Open()
   4:  
   5:        Dim sqlCmd As New SqlCommand("sp_helptext " + spName, sqlCon)
   6:        Dim sqlDataAdapter As New SqlDataAdapter(sqlCmd)
   7:        Dim dt As New DataTable
   8:        Dim strTempQuery As String = String.Empty
   9:        Dim strColumns As String()
  10:        Dim strCol As String = String.Empty
  11:  
  12:        sqlDataAdapter.Fill(dt)
  13:        If dt.Rows.Count > 0 Then
  14:            For Each dr As DataRow In dt.Rows
  15:                strTempQuery += dr.Item(0)
  16:            Next
  17:        End If
  18:  
  19:        If Not strTempQuery = "" Then
  20:  
  21:            'Dim objRegex As New Regex("select([^<]*)from")
  22:  
  23:  
  24:            Dim objMatches As MatchCollection = Regex.Matches(strTempQuery, "select([^<]*)from", RegexOptions.IgnoreCase)
  25:  
  26:            For Each mymatch As Match In objMatches
  27:                strCol += mymatch.Groups(1).Value
  28:            Next
  29:  
  30:            If Not strCol = "" Then
  31:                strColumns = strCol.Split(",")
  32:                For a As Integer = 0 To strColumns.Length - 1
  33:                    strColumns(a) = strColumns(a).Trim()
  34:                Next
  35:            End If
  36:        End If
  37:        Return strColumns
  38:    End Function

 

Restriction : Though, we have achieved the target, but since we have used sp_helptext to extract the Stored Procedure data that is why it is not possible to process encrypted stored procedure.

Will make it more better in the future to accommodate all type of Stored Procedures.

Comments

Aziz ur Rahman , on 1/3/2009 9:36:36 AM Said:

Aziz ur Rahman

1) What will happen if there is a conditional statement like If ELSE in the SP and it returns different columns based upon the condition?

2) What if the user is making some kind of dynamic query inside the SP by conacting the column names based upon some conditions and firing the query using exec(query). What will regex will do?

admin , on 1/18/2009 10:36:28 AM Said:

admin

@Aziz ur Rahman

The way I describe here in this post is very limited. You should rather use Multi-Statement Table value function.

magic list bot review United States, on 7/3/2009 10:43:06 AM Said:

magic list bot review

Thanks buddy. This is very helpful. I am looking forward for new tutorials. Is it possible to process encrypted SP?

Physics Online Russia, on 7/18/2009 3:34:53 PM Said:

Physics Online

Really interesting article, thanks for the writing!

How to Fix Rundll32.Exe Error United States, on 7/19/2009 2:20:43 PM Said:

How to Fix Rundll32.Exe Error

wow  great stuff man

check scanner United States, on 7/25/2009 7:32:50 AM Said:

check scanner

this is a basic post for beginner thats why its not too complicated, its addressed to the new users of this kind of applicattion.

garden lawn mower United States, on 8/23/2009 3:53:14 AM Said:

garden lawn mower

It always have a restriction for usual modification. But over all, you have done your best and its a good job! You have tried to be innovative and creative in there.

Hotel GDS United States, on 8/30/2009 10:31:18 PM Said:

Hotel GDS

The blog was absolutely fantastic! Lots of great information and inspiration, both of which we all need!

Online Booking Software United States, on 8/30/2009 10:58:21 PM Said:

Online Booking Software

I admit, I have not been on this webpage in a long time... however it was another joy to see It is such an important topic and ignored by so many, even professionals. I thank you to help making people more aware of possible issues.
Great stuff as usual...

wow quest helper United States, on 8/31/2009 7:33:06 PM Said:

wow quest helper

I admire what you have done here. I like the part where you say you are doing this to give back but I would assume by all the comments that this is working for you as well.

Free Online Games United States, on 9/4/2009 6:01:27 AM Said:

Free Online Games

You got a really useful blog I have been here reading for about an hour. I am a newbie and your success is very much an inspiration for me.

Black Friday United States, on 9/8/2009 11:01:03 AM Said:

Black Friday

Providing you with the best prices and deals for laptops,
notebooks and netbooks anywhere on the internet during Black Friday.

Linda Mirano United States, on 9/8/2009 10:42:10 PM Said:

Linda Mirano

Resources like the one you mentioned here will be very useful to me! I will post a link to this page on my blog. I am sure my visitors will find that very useful.

Christmas Tips United States, on 9/12/2009 6:46:14 AM Said:

Christmas Tips

Great post! I am just starting out in community management/marketing media and trying to learn how to do it well - resources like this article are incredibly helpful. As our company is based in the US, it?s all a bit new to us. The example above is something that I worry about as well, how to show your own genuine enthusiasm and share the fact that your product is useful in that case.

billigt internet United States, on 9/14/2009 5:35:31 AM Said:

billigt internet

I was very pleased to find this site.I wanted to thank you for this great read!! I definitely enjoying every little bit of it and I have you bookmarked to check out new stuff you post.

Forever Epiblanc United States, on 9/17/2009 4:38:01 AM Said:

Forever Epiblanc

Keep 'em coming... you all do such a great job at such Concepts... can't tell you how much I, for one appreciate all you do!

Trotter Netherlands, on 10/1/2009 1:21:45 AM Said:

Trotter

Good Afternoon, It im sure this website does not viewed fitly in google chrome.

leopard print coats United States, on 10/12/2009 2:36:08 PM Said:

leopard print coats

Dude.. I am not much into reading, but somehow I got to read lots of articles on your blog. Its amazing how interesting it is for me to visit you very often.

doctors in washington dc United States, on 10/13/2009 9:28:30 AM Said:

doctors in washington dc

Doctors in Washington, DC, See Reviews and Book Online Instantly.  All appointment times are guaranteed by our dentists and doctors.

bay area doctor United States, on 10/20/2009 11:38:22 AM Said:

bay area doctor

Doctors in Duboce Triangle, San Francisco, CA, See Reviews and Book Online Instantly.  All appointment times are guaranteed by our dentists and doctors.

bridal gowns People's Republic of China, on 10/20/2009 8:17:53 PM Said:

bridal gowns

appreciate your topic that you post here. Thanx for sharing your great info that is actually helpful.This is fantastic information for blog. I really love your writing style. I have bookmarked it…

Brisbane carpet cleaning United Kingdom, on 11/13/2009 10:00:17 PM Said:

Brisbane carpet cleaning

Thanks for the post. Keep the great work.

Add comment


(Will show your Gravatar icon)

  Country flag

biuquote
  • Comment
  • Preview
Loading