用户在SSMS中定义的sql业务规则

时间:2018-07-26 13:13:53

标签: sql-server tsql stored-procedures

我有我的业务规则,我想验证我的领域:afmeldingsdato是否大于3个月。如果是这样,则数据不正确,这将导致验证失败,但是我很努力,无法在SSMS中执行规则。这是我的SQL脚本:

 CREATE PROCEDURE [usr].[olddates]
 (@MemberIdList  mdm.[MemberId] READONLY -- memberID is a user defined table data type.
            -- This parameter will pass along a list of members for which the action needs to run.
 ,@ModelName   NVARCHAR(MAX)
 ,@VersionName  NVARCHAR(MAX)
 ,@EntityName  NVARCHAR(MAX)
 ,@BusinessRuleName NVARCHAR(MAX)

)
AS
BEGIN

INSERT INTO [stg].[Emballageudbyder_master_statuskode_Leaf]
 (ImportType
 ,BatchTag
 ,Code
 ,ValidFrom)
SELECT
  0   -- import type 0 = inserting or updating members
 ,N'Business Rule Extension Test'
 ,Code  -- code of the member, supplied by @MemberIdList
 , where [Afmeldingsdato] > DATEADD(month, -3, getdate()) -- set ValidFrom to the current date
FROM @MemberIdList;

-- run the staging batch job to process the staging records
EXEC [stg].[Emballageudbyder_master_statuskode_Leaf]
   @VersionName = @VersionName
  ,@BatchTag  = N'Business Rule Extension Test';
END 

但是我无法执行此操作?有人可以帮忙吗? ql

1 个答案:

答案 0 :(得分:1)

因此,您正在尝试仅插入带有“有效”日期的记录...

必须在语法上将选择语句的WHERE子句放在FROM子句后面。

SELECT
  0   -- import type 0 = inserting or updating members
 ,N'Business Rule Extension Test'
 ,Code  -- code of the member, supplied by @MemberIdList
 , [Afmeldingsdato] -- set ValidFrom to the current date <<- ???
FROM @MemberIdList
where [Afmeldingsdato] > DATEADD(month, -3, getdate());

修改后的版本

假设您要插入违反规则的记录,则过程可能如下所示:

CREATE PROCEDURE usr.olddates (
    @MemberIdList  mdm.MemberId READONLY -- list of members for which the action needs to run.
  , @ModelName   NVARCHAR(MAX)
  , @VersionName  NVARCHAR(MAX)
  , @EntityName  NVARCHAR(MAX)
  , @BusinessRuleName NVARCHAR(MAX)
)
AS
BEGIN

  -- insert the records with Afmeldingsdato older than 3 months
  INSERT INTO stg.Emballageudbyder_master_statuskode_Leaf(ImportType, BatchTag, Code, ValidFrom)
  SELECT 0, N'Business Rule Extension Test', [Code], [Afmeldingsdato]
  FROM @MemberIdList
  WHERE Afmeldingsdato < DATEADD(month, -3, GETDATE());

  -- run the staging batch job to process the staging records
  EXEC stg.Emballageudbyder_master_statuskode_Leaf
    @VersionName = @VersionName
  , @BatchTag  = N'Business Rule Extension Test';

END