0

私は基本的に、このSQLステートメントを、結果を複製することなく1日に複数回実行できるように変換しようとしています。私はコードを書きませんでした。いくつかの IF NOT EXISTS ステートメントを試しましたが、構文を正しくすることができませんでした。結果が現在テーブルに存在しない場合にのみ結果を挿入するには、何をする必要があるか教えてください。ご協力ありがとうございました。

INSERT INTO Product_SpecificationAttribute_Mapping
(ProductId, SpecificationAttributeOptionId, AllowFiltering, ShowOnProductPage,     DisplayOrder)

SELECT PD.Id  AS ProductId, 
   sao.Id AS SpecificationAttributeOptionId, 
   '1'    AS AllowFiltering, 
   '0'    AS ShowOnProductPage, 
   sao.DisplayOrder AS DisplayOrder
FROM Product as PD
join (
  select id, 
         name, 
         ymin as pcmin,       
         case when ymax < ymin then 99 else ymax end as pcmax,
         case when ymax < ymin then  0 else ymin end as ccmin,
         ymax as ccmax
  from (
        select id, 
               name, 
               convert(int, SUBSTRING(name, loc-2, 2)) as ymin,
               convert(int, SUBSTRING(name, loc+1, 2)) as ymax
        from (
              select id, 
                     name, 
                     loc
              from (
                    select id, 
                           name,
                           CHARINDEX('-', name) as loc
                    from PRODUCT
                    where CHARINDEX('-', name) > 0
                   ) as tbl
              where SUBSTRING(name, loc-3, 1) = ' ' 
                and SUBSTRING(name, loc+3, 1) = ' '
             ) as rng
       ) as yrs
 ) as PdRng
on PdRng.id = PD.id
join (
  select *
  from SpecificationAttributeOption 
  where isnumeric(Name) > 0
    and len(rtrim(Name)) = 4 AND SpecificationAttributeOption.SpecificationAttributeId = '7'
 ) as sao       
ON convert(int, Right(sao.Name, 2)) between PdRng.pcmin and PdRng.pcmax
OR convert(int, Right(sao.Name, 2)) between PdRng.ccmin and PdRng.ccmax
4

1 に答える 1