DECLARE @param nvarchar(400)
SET @param = N' @ProductID INT, @ProductInventoryStatusID INT, @ManagedByPersonID INT'
SET @sql = N' INSERT INTO #tbRangesStart
SELECT DISTINCT t1.serialnumber
FROM productinventory t1
LEFT OUTER JOIN productinventory t2 ON t1.serialnumber - 1 = t2.serialnumber AND t1.productid=t2.productid
WHERE '
DECLARE @where nvarchar(1000)
IF @ProductID IS NOT NULL
SET @where = N' t1.productid = @ProductID AND'
ELSE
SET @where = N' '
IF @ProductInventoryStatusID IS NULL and @ManagedByPersonID IS NULL
SET @where = @where + N' t2.serialnumber is null ORDER BY t1.serialnumber'
IF @ProductInventoryStatusID IS NOT NULL and @ManagedByPersonID IS NULL
SET @where = @where + N' t1.productinventorystatusid = @ProductInventoryStatusID AND (t2.serialnumber is null OR (t1.productinventorystatusid = @ProductInventoryStatusID AND t2.productinventorystatusid <> @ProductInventoryStatusID)) ORDER BY t1.serialnumber'
IF @ProductInventoryStatusID IS NULL and @ManagedByPersonID IS NOT NULL
SET @where = @where + N' t1.ManagedByPersonID = @ManagedByPersonID AND (t2.serialnumber is null OR (t1.ManagedByPersonID = @ManagedByPersonID AND t2.ManagedByPersonID <> @ManagedByPersonID)) ORDER BY t1.serialnumber'
IF @ProductInventoryStatusID IS NOT NULL and @ManagedByPersonID IS NOT NULL
SET @where = @where + N' t1.productinventorystatusid = @ProductInventoryStatusID AND t1.ManagedByPersonID = @ManagedByPersonID AND (t2.serialnumber is null OR (t1.productinventorystatusid = @ProductInventoryStatusID AND t2.productinventorystatusid <> @ProductInventoryStatusID) OR (t1.ManagedByPersonID = @ManagedByPersonID AND t2.ManagedByPersonID <> @ManagedByPersonID)) ORDER BY t1.serialnumber'
SET @sql = @sql + @where
EXEC sp_executesql @sql, @PARAM, @ProductID, @ProductInventoryStatusID, @ManagedByPersonID
No comments:
Post a Comment