需要帮助SQL Server INSERT IF NOT EXISTS语句

时间:2013-08-06 03:34:05

标签: sql-server

我基本上试图将这个sql语句转换成可以一天多次运行它而不重复结果。我没有编写代码,我已经尝试了一些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

1 个答案:

答案 0 :(得分:0)

WITH T AS (
  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
)
INSERT INTO Product_SpecificationAttribute_Mapping(
  ProductId, SpecificationAttributeOptionId, AllowFiltering, ShowOnProductPage, DisplayOrder
)
SELECT 
  ProductId, SpecificationAttributeOptionId, AllowFiltering, ShowOnProductPage, DisplayOrder
FROM T
WHERE
  NOT EXISTS (
    SELECT * FROM Product_SpecificationAttribute_Mapping M 
    WHERE
      T.ProductId = M.ProductId 
      AND T.SpecificationAttributeOptionId = M.SpecificationAttributeOptionId
      AND T.AllowFiltering = M.AllowFiltering
      AND T.ShowOnProductPage = M.ShowOnProductPage
      AND T.DisplayOrder = M.DisplayOrder
  )