CREATE PROCEDURE [dbo].[BO_GetKnokTags]
	@KnokId int,
	@AnalysisTypeId int = NULL,
	@AnalysisTagId int = NULL,
	@Name varchar(255) = NULL
AS
	SELECT a.AnalysisTagId, a.AnalysisTagTypeId, a.Name from KnokAnalysisTags k 
	inner join AnalysisTags a on a.AnalysisTagId=k.AnalysisTagId where k.KnokId=@KnokId 
	and a.AnalysisTagTypeId = ISNULL(@AnalysisTypeId, a.AnalysisTagTypeId) 
	and a.AnalysisTagId = ISNULL(@AnalysisTagId, a.AnalysisTagId)
	union ALL select -1 "AnalysisTagId", c.AnalysisTagTypeId, c.Name from KnokCustomTags c where c.KnokId=@KnokId 
	and c.AnalysisTagTypeId = ISNULL(@AnalysisTypeId, c.AnalysisTagTypeId) and -1 = ISNULL(@AnalysisTagId, -1) 
	and c.Name = ISNULL(@Name, c.Name)
RETURN 0
