更新行并忽略导致主键约束的行?

时间:2016-08-04 17:35:05

标签: sql sql-server database

我有一个包含以下约束定义的表。

//function to find an image tag within a specific section if there is one
function getImageTag ($item,$tagName)
{
    //pull desired section from given item
    $section = $item->getElementsByTagName($tagName)->item(0);
    //reparse description as if it were a string, because for some reason  PHP woon't let you directly go to the source image with getElementsByTagName
    $decoded_section = htmlspecialchars_decode($section->nodeValue);
    $section_xml = new DOMDocument();
    @$section_xml->loadHTML($decoded_section); //the @ is to suppress a bunch of warnings about characters this parser doesn't like
    //pull image tag from section if there
    $image_tag = $section_xml->getElementsByTagName('img')->item(0);
    return $image_tag;
}

//function to get the image source URL from a given item
function getImageSrc ($item)
{
    $image_tag = getImageTag($item,'description');
    if (is_null($image_tag)) //if there was nothing with the tag name of  image in the description section
    {
        //check in content:encoded section, because that's the next most likely place
        $image_tag = getImageTag($item,'encoded');
        if (is_null($image_tag)) //if there was nothing with the tag name of image in the encoded content section
        {
            //if the program gets here,  it's probably because the feed is crap and doesn't include images,
            //or it's because this particular item doesn't have a comic image in it
            $image_src = '';
            //THIS EXCEPTION  WILL PROBABLY NEED TO BE HANDLED LATER TO AVOID POTENTIAL ERRORS
        } else
        {
            $image_src = $image_tag->getAttribute('src');
        }
    } else
    {
        $image_src = $image_tag->getAttribute('src');
    }
    return $image_src;
}

我正在尝试用值(大约9000多行)更新D列;由于更新值,约束错误到达,因为我想要更新为D。

  

违反PRIMARY KEY约束'PK_TempItemTable_1'。无法在对象'dbo.TempItemTable'中插入重复键。

有没有办法让我编写MS SQL语句,让我更新所有其他行并忽略会导致这个主键约束的行?

由于

1 个答案:

答案 0 :(得分:0)

两个选项..

1.使用IGNORE_DUP_KEY = ON创建约束,然后删除重复项并重新设置约束。

2.Primary键是从A到E的列的组合,检查,所以不确定如何仅检查d将有所帮助,但是如果不知道样本模式,我可以说,这将排除表中存在的所有值你要更新

update mt
set d=dup.d
from
maintable t
join
ninetythousandtable dup
on dup.d<>t.d