【问题标题】:Adding Parameters to a Stored Procedure in SQL Server向 SQL Server 中的存储过程添加参数
【发布时间】:2019-03-17 06:54:58
【问题描述】:

我是K2SQL Server的新手。

我想为存储过程添加一个参数,该参数稍后将绑定到相应视图和表单的K2 智能对象。

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

我在同一个视图上添加了一个标签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

【问题讨论】:

    标签: sql sql-server stored-procedures k2


    【解决方案1】:

    听起来你想要这个参数和这个 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
    

    【讨论】:

    • Jon P 的方法Incorrect syntax near '@hideInactiveCompany'. 遇到了同样的错误。在我存储的产品中声明它之前,我是否必须在其他地方设置/创建新参数?
    • 否 - 帖子中的语法是正确的。确保在 @lang varchar(2) = null (第一个参数声明)之后添加了一个逗号
    • @gymcode 我编辑了我的帖子,因此您应该能够将其复制并粘贴到 SQL 管理工作室并执行它。你能试试这个吗?它应该工作......
    【解决方案2】:

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

    您只需在ALTER 语句中添加参数即可。

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

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

    【讨论】:

    • 我收到了错误Incorrect syntax near '@hideInactiveCompany'.。在我存储的产品中声明它之前,我是否必须在其他地方设置/创建新参数?
    • 注意第一个参数后面的逗号。参数实际上是一个逗号分隔的列表。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-06-17
    • 1970-01-01
    • 2013-05-06
    • 2015-11-20
    • 1970-01-01
    • 1970-01-01
    • 2015-04-24
    相关资源
    最近更新 更多