Tuesday, November 19, 2013

SQL query runs fast but stored procedure equivalent runs slow

If a SQL stored procedure is running slow, but the query is quick, try declaring local variables to replace the parameters in the stored procedure.

* Not sure why this works, please leave a comment if you have any info.

Replace the stored procedure paramters with local variables

ALTER PROCEDURE [dbo].[SlowProcedure]
(
@PARAM_begindate SMALLDATETIME = NULL,
@PARAM_enddate SMALLDATETIME = NULL
)
AS
--Add @PARAM_ variables to speed up query

DECLARE @begindate SMALLDATETIME,
                  @enddate SMALLDATETIME

SELECT  @begindate = @PARAM_begindate,
                @enddate = @PARAM_enddate
...

SELECT *
FROM [Table]
WHERE Date BETWEEN @begindate AND @enddate

Friday, November 15, 2013

Use XSD to parse XML

1.) Find xsd.exe utility to convert .xsd into .cs class
   - xsd.exe should be found here: C:\Program Files\Microsoft SDKs\Windows\v6.0A\Bin\

2.) Run xsd.exe against the file to be converted.
   - xsd /c yourfile.xsd
   - This will turn the .xsd file into a C# class

3.) Now, with the C# class, you are able to deserializing the XML into an instance of your new object.

XmlSerializer serializer = new XmlSerializer(typeof(yourClass));
string filename = Path.Combine(FilePath, "theXml.xml");
yourClass myClass = serializer.Deserialize(new FileStream(filename, FileMode.Open)) as yourClass;
if (myClass != null)
{
   //...
}