SQL Server插入非重复项

时间:2016-02-06 12:53:09

标签: sql-server insert

带对象的表:

create table objects(
    ID int identity primary key,
    name varchar(50) unique
)

让我们说我们在表格中记录a,b,c。 插入使用字符串示例'd,e,f,c,b,d'给出的新行,使用将CSV字符串更改为表并返回它的函数。

我需要插入一些不在对象表中的字符串示例,它将是d,e,f。我该怎么做?我在考虑

insert into objects(names)
select distinct names from split('d,e,f,c,b,d')
EXCEPT
select names from objects

但我相信它有更好的溶解。

1 个答案:

答案 0 :(得分:0)

我使用merge objects using (select distinct name from split(@string,',')) as s on objects.name=s.name when not matched then insert(name) values (split.name); 并且它有效

 <div class="row">
    <div class="navbar" role="navigation">
        <div class="navbar-header">
            <button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#bs-example-navbar-collapse-1" aria-expanded="false">
                <span class="Sr-only"></span>
                <span class="icon-bar"></span>
                <span class="icon-bar"></span>
                <span class="icon-bar"></span>
            </button>
            <a class="navbar-brand" href="market.html"><img src="images/logo-inner.jpg" /> alt="B-Hive: Expand your business"></a>

        </div>
        <div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1">
            <div id="mainuldiv" class="navbar navbar-collapse collapse">
                <ul class="nav navbar-nav mynavbar" style="float:right;">
                    <li class="top-menu-links-active"><a href="#">MARKET</a></li>
                    <li><a class="top-menu-links" href="#">EXHIBITIONS</a></li>
                    <li><a class="top-menu-links" href="#">MESSAGES</a></li>
                    <li><a class="top-menu-links" href="#">DASHBOARD</a></li>
                    <li><a class="top-menu-links" href="#">CART</a></li>
                    <li class="profile-pic-padding"></li>
                    <li class="dropdown">
                        <a href="#" class="dropdown-toggle custom-dropdown-toggle" data-toggle="dropdown" role="button" aria-haspopup="true" aria-expanded="false"><img class="" src="images/profile-pic.fw.png" /> <span class="caret"></span></a>
                        <ul class="dropdown-menu">
                            <li><a href="#">Account</a></li>
                            <li><a href="#">Privacy</a></li>

                            <li role="separator" class="divider"></li>
                            <li><a href="#">Switch Accounts</a></li>
                            <li role="separator" class="divider"></li>

                            <li>
                                <a href="#">Language <span class="caret"></span></a>
                                <ul class="dropdown-menu">
                                    <li><a href="#">EN</a></li>
                                    <li><a href="#">ع</a></li>

                                </ul>
                                </li>
                            </ul>
                        </li>
                        </ul>
            </div>
        </div>
    </div>
</div>