Last active
June 10, 2016 21:03
-
-
Save cathode/692975a1ef66010b4f7492ef47d533cc to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| -- UDF definition: | |
| CREATE FUNCTION [dbo].[GetSessionParameter] | |
| ( | |
| @key nvarchar(255), | |
| @defaultValue nvarchar(MAX) = null | |
| ) | |
| RETURNS NVARCHAR(MAX) | |
| AS | |
| BEGIN | |
| DECLARE @Ret as NVARCHAR(MAX) | |
| SELECT | |
| @Ret = [Value] | |
| FROM | |
| [UserSessionParameters] | |
| WHERE | |
| [Key] = @key | |
| IF @@ROWCOUNT = 0 | |
| BEGIN | |
| SET @ret = @defaultValue; | |
| END | |
| RETURN @ret; | |
| END | |
| -- version 1 | |
| SELECT | |
| PartNumber, | |
| AnnualUsage, | |
| [AnnualUsage] * ( | |
| DATEDIFF( | |
| day, | |
| CONVERT(datetime, dbo.GetSessionParameter('Purchasing.BeginningDate', GETDATE())), | |
| CONVERT(datetime, dbo.GetSessionParameter('Purchasing.EndingDate', GETDATE())) | |
| ) + 1) / 365.0 AS [ProjectedConsumption] | |
| FROM | |
| [Purchas_Invt_Parts-1] | |
| -- version 2 | |
| SELECT | |
| PartNumber, | |
| AnnualUsage, | |
| [AnnualUsage] * ( | |
| DATEDIFF( | |
| day, | |
| CONVERT(datetime, [SP].BeginningDate), | |
| CONVERT(datetime, [SP].EndingDate) | |
| ) + 1) / 365.0 AS [ProjectedConsumption] | |
| FROM | |
| [Purchas_Invt_Parts-1] | |
| LEFT JOIN | |
| (SELECT | |
| dbo.GetSessionParameter('Purchasing.BeginningDate', GETDATE()) AS [BeginningDate], | |
| dbo.GetSessionParameter('Purchasing.EndingDate', GETDATE()) AS [EndingDate] | |
| ) AS [SP] | |
| ON 1 = 1 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment