sqlsql-serverstored-proceduresk2

Adding Parameters to a Stored Procedure in SQL Server


I am new to K2and SQL Server.

I want to add a parameter to a stored procedure, which will be later tied to a K2 smart object for the respective views and forms.

Currently it takes in 1 parameter, lang, which is an input from a label from the K2 Smartform View.

I added a label labelHideInactiveCompany on the same view, and I would like to pass that value into my Stored Procedure but I don't know how to do it.

I am told that the first thing I need to change is the stored procedure, and then updating the smart object.

May I know what are the steps I should be take? Thank you.

Below is my query:

USE [K2_Database]
GO

SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO

ALTER procedure [Config].[usp_ListBusinessUnit] 
@lang varchar(2) = null
as

SELECT EntityId
      ,EntityCode
      ,EntityName
      ,CO.OrganizationDesc
      ,EntityAbbreviation
      ,PBU.ParentBusinessUnitName
      ,EntityAttribute 
      ,EntityOwnedCompany
      ,EntityPrincipal
      ,EntityAccountingProgram 
      ,EntityCurrency
      ,EntityCurrenyExchRate 
      ,BU.CountryRegion
      ,EntityNCOwnedIndustry
      ,BU.IsActive 
      ,BU.CreatedBy 
      ,CreateOn 
      ,BU.ModifiedBy
      ,BU.ModifiedOn 
      ,BU.Lang AS LangAbbr
  FROM Config.BusinessUnit BU
  LEFT JOIN Config.Organization CO on BU.OrganizationId = CO.OrganizationId
  LEFT JOIN Config.ParentBusinessUnit PBU on BU.ParentBusinessUnitId = PBU.ParentBusinessUnitId 
  ORDER BY CASE WHEN @lang = 'cn' THEN BU.Lang END,
           CASE WHEN @lang = 'en' THEN BU.Lang END DESC, 
           EntityName

Solution

  • It sounds like you want this parameter and this WHERE clause:

    ALTER procedure [Config].[usp_ListBusinessUnit] 
    @lang varchar(2) = null, 
    @hideInactiveCompany bit = 0 -- Add this parameter!
    as
    
    SELECT EntityId
          ,EntityCode
          ,EntityName
          ,CO.OrganizationDesc
          ,EntityAbbreviation
          ,PBU.ParentBusinessUnitName
          ,EntityAttribute 
          ,EntityOwnedCompany
          ,EntityPrincipal
          ,EntityAccountingProgram 
          ,EntityCurrency
          ,EntityCurrenyExchRate 
          ,BU.CountryRegion
          ,EntityNCOwnedIndustry
          ,BU.IsActive 
          ,BU.CreatedBy 
          ,CreateOn 
          ,BU.ModifiedBy
          ,BU.ModifiedOn 
          ,BU.Lang AS LangAbbr
      FROM Config.BusinessUnit BU
      LEFT JOIN Config.Organization CO on BU.OrganizationId = CO.OrganizationId
      LEFT JOIN Config.ParentBusinessUnit PBU on BU.ParentBusinessUnitId = PBU.ParentBusinessUnitId 
      WHERE (@hideInactiveCompany = 0 OR BU.IsActive = 1) -- Use the parameter!
      ORDER BY CASE WHEN @lang = 'cn' THEN BU.Lang END,
               CASE WHEN @lang = 'en' THEN BU.Lang END DESC, 
               EntityName