gpt4 book ai didi

sql-server - 我如何使用光标从表中删除记录

转载 作者:行者123 更新时间:2023-12-02 19:11:18 27 4
gpt4 key购买 nike

我想使用游标从表中删除记录。我该怎么做?

我使用 MSSQL 2008 Express 此代码不会从 #temp 中删除任何内容。我还尝试了当前的cursor_name不起作用的地方。

这是我的示例代码:

use AdventureWorks

drop table #temp
select * into #temp from HumanResources.Employee;

declare @eid as int;
declare @nid as varchar(15);


DECLARE Employee_Cursor CURSOR FOR
SELECT A.EmployeeID, A.NationalIDNumber FROM #temp AS A
OPEN Employee_Cursor;
FETCH NEXT FROM Employee_Cursor INTO @eid , @nid ;
WHILE @@FETCH_STATUS = 0
BEGIN
IF (@eid > 10)
BEGIN
delete from #temp where #temp.EmployeeID = @eid;
END
FETCH NEXT FROM Employee_Cursor;
END;
CLOSE Employee_Cursor;
DEALLOCATE Employee_Cursor;
GO

select * from #temp

提前致谢

最佳答案

use AdventureWorks

select * into #temp from HumanResources.Employee;

declare @eid as int;
declare @nid as varchar(15);

DECLARE Employee_Cursor CURSOR FOR
SELECT A.EmployeeID, A.NationalIDNumber FROM #temp AS A
OPEN Employee_Cursor;
FETCH NEXT FROM Employee_Cursor INTO @eid , @nid ;
WHILE @@FETCH_STATUS = 0
BEGIN
IF (@eid > 10)
BEGIN
delete from #temp where current of Employee_Cursor
END
FETCH NEXT FROM Employee_Cursor INTO @eid , @nid ;
END;
CLOSE Employee_Cursor;
DEALLOCATE Employee_Cursor;

select * from #temp

drop table #temp

这对我有用

关于sql-server - 我如何使用光标从表中删除记录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8813961/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com