向 SQL 服务器中的存储过程添加参数

Adding Parameters to a Stored Procedure in SQL Server

我是 K2SQL Server 的新手。

我想向存储过程添加一个参数,该存储过程稍后将绑定到 K2 智能对象以用于各自的视图和表单。

目前它接受 1 个参数,lang,这是来自 K2 Smartform View 标签的输入。

我在同一个视图上添加了一个标签 labelHideInactiveCompany,我想将该值传递到我的存储过程中,但我不知道该怎么做。

有人告诉我,我需要更改的第一件事是存储过程,然后更新 smart object

我可以知道我应该采取哪些步骤吗?谢谢。

下面是我的查询:

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

添加参数很简单,我使用了占位符,因为我们不知道您要调用什么参数或它是什么数据类型。

您需要做的就是在 ALTER 语句中添加参数。

ALTER procedure [Config].[usp_ListBusinessUnit] 
@lang varchar(2) = null,
@newParamName newParamType -- Your new stuff
as
-- the rest of your stored proc here

现在不要忘记对存储过程中的新参数做一些事情。

听起来你想要这个参数和这个 WHERE 子句:

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