我得到的關鍵字輸入像測試,檢查等..我插入這些關鍵字作爲單獨的行在我的數據庫檢查是否存在,如果存在更新或否則嵌件it.This keywordid和項目代表給這個ID應該插入橋table.On插入現有的關鍵字,它拋出的錯誤:SQL錯誤:子查詢返回多個值
Subquery returned more than 1 value. This is not permitted when the subquery follows =, !=, <, <= , >, >= or when the subquery is used as an expression.
Cannot insert the value NULL into column 'KeywordId', table 'dbo.Keywordsitems'; column does not allow nulls. INSERT fails.
我使用下面的存儲過程
ALTER PROCEDURE [dbo].[InsertKeywords]
(
@keywordName nvarchar(256),
@itemid uniqueidentifier
)
AS
begin
declare
@itemid uniqueidentifier,
@keywordid uniqueidentifier,
@id uniqueidentifier;
declare @keyworddata table (keywordid uniqueidentifier);
set @id = (select KeywordId from keywords where [email protected])
set @itemid =(select itemId from Items where ItemID = @itemid);
if not exists(select keyname from keywords where KeyName = @keywordName)
begin
insert into Keywords(KeywordId,KeyName)
output inserted.KeywordId into @keyworddata(keywordid)
values (newid(),@keywordName);
select @keywordid = keywordid from @keyworddata;
insert into Keywordsitems(KeywordId,ItemId)
values (@keywordid,@itemid);
end
else
begin
update keywords set [email protected] where KeywordName= @keywordName;
insert into KeywordsResources(KeywordId,itemId) values (@id,@itemid);
end
end
如果你有相同的多個行'KeyName'或'ItemID',你要哪一個?好像你在某處丟失了一個約束。 –
我需要檢查相同的關鍵字,如果存在更新,如果不插入它。我需要檢查我的關鍵字表,只有keywordid,關鍵字列。一旦更新或插入完成關鍵字表後我需要插入到橋表是我要插入keywordid和itemid的位置 – newuser1555
您的代碼難以遵循。你的'@ itemid'從一個'@ itemid'切換到'@ databaseid'?爲什麼?如果一個特定項目有多個'resourcedatabaseId',你想要做什麼?目前(隨着下面提供的修復)它將選擇一個任意的數據庫ID,而不是遍歷所有的數據庫。 '@ itemid'不會奇蹟般地成爲一個數組... –