Popular Posts

Friday, June 12, 2015

Rules of Coding

Rules of Coding


  1. The most important language you will ever code in:  English (or the common spoken language used in your company).  -- So that everyone understands what you wrote and why.
  2. Never use a regular expression without saying in plain English what it is doing.  Regex is so hard to understand!!!  Example: what the heck is this doing.  Found in the gruntfile.js of bootstrap at https://github.com/twbs/bootstrap/blob/master/Gruntfile.js                                                          return string.replace(/[-\\^$*+?.()|[\]{}]/g, '\\$&');
  3. If you are going to modify something extensively, you are better off writing a new one and swapping it in when it is ready to go live.  For example, if you are extensivley modifying an email template that is currently being used in production, just make a new template with a creative name and use that, so you don't possibly mess up code that is still using the old template.   --This way you can also keep a clean backup of the old template and be able to rollback quickly.
  4. No one cares.  No matter how much you think a given coding principle is important, there is nothing worse than alienating your fellow co-workers by sticking to a principle.  It may still be important, but no one cares in the end.  Try living without it for a day and you'll find you are still alive.
  5. Don't bother committing code if you haven't tested it.  There are runtime errors and compile time errors.  So even if your code builds, it doesn't mean it's even going to work.  This will save you time in the end, believe me.  You will end up having to change your code that doesn't work, or rollback a feature that you comitted and later find out will never work (e.g. I added logic to update the CreatedDate of some table in the database, and come to find out you can't update a database autopopulated field.  So I get to rollback the "addition" I made across 5 files).

Friday, June 5, 2015

Using XML to pass dynamic paramters to SQL stored procedure.

You can use an XML string to pass any number of options to a SQL stored procedure like so


Use a scalar valued function to get one value at a time, which is still very fast unless you have hundreds of key-value pairs with XML like MyValue

Use like this

USE [SaveOn]
GO
/****** Object:  StoredProcedure [dbo].[sp_GetClubUserRenewals]    Script Date: 5/6/2015 12:37:00 PM ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author: Adam Alt
-- Create date: 2015-4-30
-- Description: Gets count of users and total dollar value from user renewals per club.
-- =============================================
ALTER PROCEDURE [dbo].[sp_GetClubUserRenewals]
      @Filters NVARCHAR(MAX)
AS

     BEGIN
         SET NOCOUNT ON;

         --Declare variables
         DECLARE @BeginDate DATETIME

         --Parse out values from xml object
         SELECT @BeginDate = dbo.GetFilterValue(@Filters,'BeginDate');

         --Select statement
         SELECT * FROM SOMETABLE  WHERE(@BeginDate IS NULL OR DateField >= @BeginDate)
         

     END


And here is the scalar valued function.


GO
/****** Object:  UserDefinedFunction [dbo].[GetFilterValue]    Script Date: 5/6/2015 12:35:35 PM ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author: Adam Alt
-- Create date: 2015-4-30
-- Description: Gets the value of a single node in a Filters xml object defined like Value
-- =============================================
ALTER FUNCTION [dbo].[GetFilterValue](
               @Filters XML,
               @FilterName NVARCHAR(MAX))
RETURNS NVARCHAR(MAX)
AS
     BEGIN
         -- Declare the return variable here
         DECLARE @RetVal NVARCHAR(MAX);

         -- Add the T-SQL statements to compute the return value here
         SELECT @RetVal = NULLIF(LTRIM(RTRIM(node.value('.','nvarchar(max)'))),'')
           FROM @Filters.nodes('/Filters/*') AS Nodes(node)
           WHERE node.value('local-name(.)','nvarchar(max)') = @FilterName;

         -- Return the result of the function
         RETURN @RetVal;

     END;

Or get them all at once with a table valued function:
USE [SaveOn]
GO
/****** Object:  UserDefinedFunction [dbo].[ParseFilters]    Script Date: 5/4/2015 3:18:07 PM ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author: Adam Alt
-- Create date: 2015-4-30
-- Description: Takes xml like MyValue and returns all name-value pairs in a table, removing empty entries.
-- =============================================
ALTER FUNCTION [dbo].[ParseFilters](
               @XML XML)
RETURNS @RtnTable TABLE(ID INT IDENTITY(1,1),
                        Name NVARCHAR(MAX),
                        Value NVARCHAR(MAX))
AS
     BEGIN
         INSERT INTO @RtnTable(Name,
                               [Value])
         SELECT node.value('local-name(.)','nvarchar(max)') AS Name,
                NULLIF(LTRIM(RTRIM(node.value('.','nvarchar(max)'))),'') AS Value
           FROM @XML.nodes('/Filters/*') AS Nodes(node);

         RETURN;
     END;