Thursday, May 31, 2012

Execute stored proc to select into a table



SELECT * INTO #MyTempTable FROM OPENROWSET('SQLNCLI', 'Server=;Trusted_Connection=yes;',
     'EXEC database.schema.usp_spName  ''parameter'' ')

SELECT * FROM #MyTempTable

If you get error like
"
Msg 15281, Level 16, State 1, Line 1

SQL Server blocked access to STATEMENT 'OpenRowset/OpenDatasource' of component 'Ad Hoc Distributed Queries' because this component is turned off as part of the security configuration for this server. A system administrator can enable the use of 'Ad Hoc Distributed Queries' by using sp_configure. For more information about enabling 'Ad Hoc Distributed Queries', see "Surface Area Configuration" in SQL Server Books Online.
"

then you need to turn on these options by:

executing following code:

sp_configure 'Show Advanced Options', 1
GO
RECONFIGURE
GO
sp_configure 'Ad Hoc Distributed Queries', 1
GO
RECONFIGURE
GO


Monday, May 21, 2012

XML string to Dataset

Lately I have worked a lot with SSIS where I am consuming some web services as source and writing to destinations like SQL server or a txt file.

following method is very useful when I use script task to connect to Source and get XML as string and convert it to oledb dataset and loop through each record to write it to destination.


  private DataSet XmlString2DataSet(string xmlString)
        {
            //create a new DataSet that will hold our values
            DataSet quoteDataSet = null;

            //check if the xmlString is not blank
            if (String.IsNullOrEmpty(xmlString))
            {
                //stop the processing
                return quoteDataSet;
            }

            try
            {
                //create a StringReader object to read our xml string
                using (StringReader stringReader = new StringReader(xmlString))
                {
                    //initialize our DataSet
                    quoteDataSet = new DataSet();

                    //load the StringReader to our DataSet
                    quoteDataSet.ReadXml(stringReader);
                }
            }
            catch
            {
                //return null
                quoteDataSet = null;
            }

            //return the DataSet containing the stock information
            return quoteDataSet;
        }