我在insert语句中收到错误

时间:2016-03-04 09:03:43

标签: sql sql-server sql-server-2008

我正在尝试插入KeywordSearchResults表。我在尝试插入 SearchResults 表时遇到错误。

Declare @UrlList SearchResultList READONLY
declare @KeywordID int

insert into Keyword(Keyword,Domain_ID,RunDate)values(@Keyword,@Domain_ID,@RunDate)
set @KeywordID=IDENT_CURRENT('Keyword')


insert into SearchResults(SearchedURL,Keyword_ID,IsClient)
select (select SearchedURL from @UrlList),@KeywordID as Keyword_ID,1 as IsClient

我收到的错误如下所示。

  

子查询返回的值超过1。当子查询遵循=,!=,<,< =,>,> =或子查询用作表达式时,不允许这样做。   声明已经终止。



<table><tbody><tr><th>SearchedURL_ID</th><th>SearchedURL</th><th>Keyword_ID</th><th>IsClient</th></tr><tr><td>8</td><td>http://www.revanadigital.com/</td><td>8</td><td>NULL</td></tr><tr><td>9</td><td>http://www.zoominfo.com/c/WebMetro-Inc/41713725</td><td>8</td><td>NULL</td></tr><tr><td>10</td><td>http://www.revanadigital.com/who-we-are/careers/</td><td>8</td><td>NULL</td></tr><tr><td>11</td><td>https://www.glassdoor.com/Reviews/WebMetro-California-Reviews-E259626.htm</td><td>8</td><td>NULL</td></tr><tr><td>12</td><td>http://www.yelp.com/biz/webmetro-san-dimas</td><td>8</td><td>NULL</td></tr><tr><td>13</td><td>https://www.crunchbase.com/organization/webmetro</td><td>8</td><td>NULL</td></tr></tbody></table>
&#13;
&#13;
&#13;

2 个答案:

答案 0 :(得分:2)

您的第二个插入查询应该是这样的 -

INSERT INTO SearchResults (SearchedURL, Keyword_ID, IsClient)
    SELECT SearchedURL ,@KeywordID ,1 FROM @UrlList

您可以参考此MSDN链接 - How to use TVP

答案 1 :(得分:2)

如果你想插入所有获得@keywordid的网址,那么你需要这个:

insert into SearchResults(SearchedURL,Keyword_ID,IsClient)
select SearchedURL,@KeywordID as Keyword_ID,1 as IsClient
from @UrlList

如果没有,那么你必须修改(select SearchedURL from @UrlList)子查询以确保它只返回一行。使用TOP 1或类似内容添加ORDER BY