如何获取 SQL 更新的主键?
How to get Primary Key on SQL Update?
如果找到 "searchedWord",我将更新 table。如果找到,我想知道我刚刚更新的记录的主键是什么。我该怎么做?下面是我的代码。另外,如果找不到 "searchedWord",我会插入它并使用 scope_identity() 来获取它。我的问题是如何对更新做同样的事情?我想我可以先做一个 select 语句,但那是一个额外的查询。
BEGIN TRAN
UPDATE COBC_searchedWord
SET searchCount=searchCount+1,
resultsReturned=#resultsReturned#,
lastSearched=getDate()
WHERE searchedWord='#searchedWord#';
IF @@ROWCOUNT = 0
BEGIN
INSERT INTO COBC_searchedWord
([searchedWord] ,[searchCount] ,[resultsReturned] ,[lastSearched])
VALUES
('#searchedWord#',1,'#resultsReturned#',getDate())
SELECT scope_identity() AS searchedWordID
END
COMMIT TRAN
更新:这就是我最终得到的,它似乎有效。感谢您的输入。
DECLARE @tmpTable TABLE (ID int);
UPDATE COBC_searchedWord
SET searchCount=searchCount+1,
resultsReturned=#resultsReturned#,
lastSearched=getDate()
OUTPUT inserted.searchedWordID into @tmpTable
WHERE searchedWord='#searchedWord#';
IF @@ROWCOUNT = 0
BEGIN
INSERT INTO COBC_searchedWord
([searchedWord])
OUTPUT inserted.searchedWordID into @tmpTable
VALUES
('#searchedWord#');
END
SELECT ID from @tmpTable;
您应该使用 OUTPUT 子句。在线书籍将解释如何。
试试这个,如果你正在更新你的记录,并且那个搜索词 focund 在 table 然后 select 来自 table
的 searchedWordID
BEGIN TRAN
UPDATE COBC_searchedWord
SET searchCount=searchCount+1,
resultsReturned=#resultsReturned#,
lastSearched=getDate()
WHERE searchedWord='#searchedWord#';
IF @@ROWCOUNT = 0
BEGIN
INSERT INTO COBC_searchedWord
([searchedWord] ,[searchCount] ,[resultsReturned] ,[lastSearched])
VALUES
('#searchedWord#',1,'#resultsReturned#',getDate())
SELECT scope_identity() AS searchedWordID
END
ELSE
BEGIN
SELECT searchedWordID FROM COBC_searchedWord
WHERE searchedWord='#searchedWord#';
END
COMMIT TRAN
如果找到 "searchedWord",我将更新 table。如果找到,我想知道我刚刚更新的记录的主键是什么。我该怎么做?下面是我的代码。另外,如果找不到 "searchedWord",我会插入它并使用 scope_identity() 来获取它。我的问题是如何对更新做同样的事情?我想我可以先做一个 select 语句,但那是一个额外的查询。
BEGIN TRAN
UPDATE COBC_searchedWord
SET searchCount=searchCount+1,
resultsReturned=#resultsReturned#,
lastSearched=getDate()
WHERE searchedWord='#searchedWord#';
IF @@ROWCOUNT = 0
BEGIN
INSERT INTO COBC_searchedWord
([searchedWord] ,[searchCount] ,[resultsReturned] ,[lastSearched])
VALUES
('#searchedWord#',1,'#resultsReturned#',getDate())
SELECT scope_identity() AS searchedWordID
END
COMMIT TRAN
更新:这就是我最终得到的,它似乎有效。感谢您的输入。
DECLARE @tmpTable TABLE (ID int);
UPDATE COBC_searchedWord
SET searchCount=searchCount+1,
resultsReturned=#resultsReturned#,
lastSearched=getDate()
OUTPUT inserted.searchedWordID into @tmpTable
WHERE searchedWord='#searchedWord#';
IF @@ROWCOUNT = 0
BEGIN
INSERT INTO COBC_searchedWord
([searchedWord])
OUTPUT inserted.searchedWordID into @tmpTable
VALUES
('#searchedWord#');
END
SELECT ID from @tmpTable;
您应该使用 OUTPUT 子句。在线书籍将解释如何。
试试这个,如果你正在更新你的记录,并且那个搜索词 focund 在 table 然后 select 来自 table
的 searchedWordIDBEGIN TRAN
UPDATE COBC_searchedWord
SET searchCount=searchCount+1,
resultsReturned=#resultsReturned#,
lastSearched=getDate()
WHERE searchedWord='#searchedWord#';
IF @@ROWCOUNT = 0
BEGIN
INSERT INTO COBC_searchedWord
([searchedWord] ,[searchCount] ,[resultsReturned] ,[lastSearched])
VALUES
('#searchedWord#',1,'#resultsReturned#',getDate())
SELECT scope_identity() AS searchedWordID
END
ELSE
BEGIN
SELECT searchedWordID FROM COBC_searchedWord
WHERE searchedWord='#searchedWord#';
END
COMMIT TRAN