MSSQLTips.com - your daily source for SQL Server tips

Google
 
Web mssqltips.com

ESSENTIALS: Home | Tips | Search | Categories | Top 10 | Products | Authors | Blogs | Forums | Webcasts | Advertise | About
How To Return a Result Set from a SQL Server 2005 CLR Stored Procedure -

in Search

How To Return a Result Set from a SQL Server 2005 CLR Stored Procedure

Last post 06-30-2008 2:57 PM by raybarley. 2 replies.
Page 1 of 1 (3 items)
Sort Posts: Previous Next
  • 06-12-2008 12:30 AM

    How To Return a Result Set from a SQL Server 2005 CLR Stored Procedure

    This post is related to this tip: How To Return a Result Set from a SQL Server 2005 CLR Stored Procedure

    http://www.mssqltips.com/tip.asp?tip=1518

  • 06-30-2008 10:00 AM In reply to

    • AndreQ
    • Top 500 Contributor
    • Joined on 06-30-2008
    • Posts 1

    Re: How To Return a Result Set from a SQL Server 2005 CLR Stored Procedure

    Its a very handy utility but the part that caught my eye is assigning each column for each row of a result set. Is this just one of those unfortunate requirements when translatng from a collection to a recordset?

     I remember having to do this in VBA way back when and although it always worked, its slow work. Are there any functions that would do this automatically, cycling through a collection's data elements and automatically creating recordset and writing to it?

     THAT would be an incredibly useful library function.

     Regards

    AndreQ

  • 06-30-2008 2:57 PM In reply to

    Re: How To Return a Result Set from a SQL Server 2005 CLR Stored Procedure

    As far as I know you have to do it this way.  You can also return a result set from a function (i.e. a table-valued function) using the CLR.  It is a bit more elegant (well maybe) but you still have to manually assign the values to the result set.  I have done a tip on that; it should be published soon.

    What you do is create a function (let's call it A) that retrieves the data you want to return as a result set and returns it as a collection class that implements the IEnumerable interface.  Then you have a companion function (let's call it B) that has an output parameter for each column in the result set.  When the CLR function is called it invokes A once then iterates over the collection calling B one time for each element in the collection.

    Here are the 2 functions:

    /* THIS IS 'A' */

        [SqlFunction(SystemDataAccess = SystemDataAccessKind.Read, FillRowMethodName = "GetListInfo")]
        public static IEnumerable GetListCollection(SqlString url)
        {
            DataTable t = new DataTable();
            WindowsImpersonationContext ctx = null;

            WindowsIdentity id = SqlContext.WindowsIdentity;

            try
            {
                ctx = id.Impersonate();

                WSS.Lists svc = new WSS.Lists();
                svc.Url = url.ToString();

                //svc.Credentials = new NetworkCredential("barley", "pass@word1", "VS");
                svc.Credentials = CredentialCache.DefaultNetworkCredentials;
                XmlNode node = svc.GetListCollection();
                XmlTextReader rdr = new XmlTextReader(node.OuterXml,
                                            XmlNodeType.Element, null);
                DataSet ds = new DataSet();
                ds.ReadXml(rdr);
                t = ds.Tables[0];
            }
            finally
            {
                if (ctx != null)
                    ctx.Undo();
            }

            return t.Rows;
        }

    /* THIS IS 'B' */
        public static void GetListInfo(
                            object obj,
                            out SqlString name,
                            out SqlString title,
                            out SqlString url )
        {
            DataRow r = (DataRow)obj;
            name = new SqlString(r["Name"].ToString());
            title = new SqlString(r["Title"].ToString());
            url = new SqlString(r["DefaultViewUrl"].ToString());
        }
     

     

     

Page 1 of 1 (3 items)