Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save DForshner/2f6c96857c647d6c5c8f to your computer and use it in GitHub Desktop.

Select an option

Save DForshner/2f6c96857c647d6c5c8f to your computer and use it in GitHub Desktop.
Passing nullable parameter to stored procedure
CREATE PROCEDURE [DB].[PassNullParameterToStoredProcedure] (@Started DateTime = NULL)
AS
BEGIN
DECLARE @Data TABLE( Started DateTime NOT NULL)
INSERT INTO @Data (Started)
VALUES ('2015-01-01T00:00:00'), ('2015-01-02T00:00:00'), ('2015-01-03T00:00:00')
SELECT * FROM @Data
WHERE (@SessionStarted IS NULL OR Started = @SessionStarted)
END
GO
EXEC [DB].[TestNullParameter] @SessionStarted = NULL -- Returns all
EXEC [DB].[TestNullParameter] @SessionStarted = '2015-01-03T00:00:00' -- Returns one record
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment