Notice (8): file_put_contents(): Write of 270 bytes failed with errno=28 No space left on device [CORE/src/Log/Engine/FileLog.php, line 140]

Notice: file_put_contents() [function.file-put-contents]: Write of 1108 bytes failed with errno=28 No space left on device in /www/wwwroot/www.adminso.com/vendor/cakephp/cakephp/src/Log/Engine/FileLog.php on line 140
Notice (8): SplFileObject::fwrite() [<a href='https://secure.php.net/splfileobject.fwrite'>splfileobject.fwrite</a>]: Write of 5437 bytes failed with errno=28 No space left on device [CORE/src/Cache/Engine/FileEngine.php, line 141]

Notice: file_put_contents() [function.file-put-contents]: Write of 3152 bytes failed with errno=28 No space left on device in /www/wwwroot/www.adminso.com/vendor/cakephp/cakephp/src/Log/Engine/FileLog.php on line 140
Notice (8): unserialize() [<a href='https://secure.php.net/function.unserialize'>function.unserialize</a>]: Error at offset 4079 of 4085 bytes [APP/Controller/NewsController.php, line 5571]

Notice: file_put_contents() [function.file-put-contents]: Write of 2758 bytes failed with errno=28 No space left on device in /www/wwwroot/www.adminso.com/vendor/cakephp/cakephp/src/Log/Engine/FileLog.php on line 140
Warning (2): Undefined array key "nsort" [APP/Controller/NewsController.php, line 3613]

Notice: file_put_contents() [function.file-put-contents]: Write of 2074 bytes failed with errno=28 No space left on device in /www/wwwroot/www.adminso.com/vendor/cakephp/cakephp/src/Log/Engine/FileLog.php on line 140
Warning (2): Trying to access array offset on value of type null [APP/Controller/NewsController.php, line 3613]

Notice: file_put_contents() [function.file-put-contents]: Write of 2098 bytes failed with errno=28 No space left on device in /www/wwwroot/www.adminso.com/vendor/cakephp/cakephp/src/Log/Engine/FileLog.php on line 140
Warning (2): Undefined array key "nsort" [APP/Controller/NewsController.php, line 3613]

Notice: file_put_contents() [function.file-put-contents]: Write of 2074 bytes failed with errno=28 No space left on device in /www/wwwroot/www.adminso.com/vendor/cakephp/cakephp/src/Log/Engine/FileLog.php on line 140
Warning (2): Trying to access array offset on value of type null [APP/Controller/NewsController.php, line 3613]

Notice: file_put_contents() [function.file-put-contents]: Write of 2098 bytes failed with errno=28 No space left on device in /www/wwwroot/www.adminso.com/vendor/cakephp/cakephp/src/Log/Engine/FileLog.php on line 140
SQLServer遍历表中记录的2种方法(使用表变量和游标) - 站长搜索
首页 > 资讯列表 > 编程/数据库 >>

SQLServer遍历表中记录的2种方法(使用表变量和游标)

Warning (2): Undefined array key "nsort" [ROOT/plugins/Kuhuang/templates/Websites/view.php, line 430]
Notice: file_put_contents() [function.file-put-contents]: Write of 2422 bytes failed with errno=28 No space left on device in /www/wwwroot/www.adminso.com/vendor/cakephp/cakephp/src/Log/Engine/FileLog.php on line 140
Warning (2): Trying to access array offset on value of type null [ROOT/plugins/Kuhuang/templates/Websites/view.php, line 430]

Notice: file_put_contents() [function.file-put-contents]: Write of 2446 bytes failed with errno=28 No space left on device in /www/wwwroot/www.adminso.com/vendor/cakephp/cakephp/src/Log/Engine/FileLog.php on line 140
">
Warning (2): Undefined array key "nsort" [ROOT/plugins/Kuhuang/templates/Websites/view.php, line 430]

Notice: file_put_contents() [function.file-put-contents]: Write of 2422 bytes failed with errno=28 No space left on device in /www/wwwroot/www.adminso.com/vendor/cakephp/cakephp/src/Log/Engine/FileLog.php on line 140
Warning (2): Trying to access array offset on value of type null [ROOT/plugins/Kuhuang/templates/Websites/view.php, line 430]

Notice: file_put_contents() [function.file-put-contents]: Write of 2446 bytes failed with errno=28 No space left on device in /www/wwwroot/www.adminso.com/vendor/cakephp/cakephp/src/Log/Engine/FileLog.php on line 140
2022-09-23 17:06:17 转载来源: 网络整理/侵权必删

SQLServer遍历表一般都要用到游标,SQLServer中可以很容易的用游标实现循环,实现SQLServer遍历表中记录。本文将介绍利用使用表变量和游标实现数据库中表的遍历

SQL Server遍历表一般都要用到游标,SQL Server中可以很容易的用游标实现循环,实现SQL Server遍历表中记录。本文将介绍利用使用变量和游标实现数据库中表的遍历。

表变量来实现表的遍历
以下代码中,代码块之间的差异已经用灰色的背景标记。
复制代码 代码如下:

DECLARE @temp TABLE
(
[id] INT IDENTITY(1, 1) ,
[Name] VARCHAR(10)
)
DECLARE @tempId INT ,
@tempName VARCHAR(10)
INSERT INTO @temp
VALUES ( 'a' )
INSERT INTO @temp
VALUES ( 'b' )
INSERT INTO @temp
VALUES ( 'c' )
INSERT INTO @temp
VALUES ( 'd' )
INSERT INTO @temp
VALUES ( 'e' )
WHILE EXISTS ( SELECT [id]
FROM @temp )
BEGIN
SET ROWCOUNT 1
SELECT @tempId = [id] ,
@tempName = [Name]
FROM @temp
SET ROWCOUNT 0
--delete from @temp where [id] = @tempId
PRINT 'Name:----' + @tempName
END

但是这种方法,必须借助ROWCOUNT。但是使用 SET ROWCOUNT 将可能会影响 DELETE、INSERT 和 UPDATE 语句。
所以修改上面WHILE循环,改用TOP来选出首条记录。
复制代码 代码如下:

WHILE EXISTS ( SELECT [id]
FROM @temp )
BEGIN
SELECT TOP 1
@tempId = [id] ,
@tempName = [Name]
FROM @temp
DELETE FROM @temp
WHERE [id] = @tempId
SELECT *
FROM @temp
EXEC('drop table '+)
PRINT 'Name:----' + @tempName
END

这种方法也存在一个问题,需要将遍历过的行删除,事实上,我们在实际应用中可能并不想要遍历完一行就删除一行。
利用游标来遍历表
  游标是非常邪恶的一种存在,使用游标经常会比使用面向集合的方法慢2-3倍,当游标定义在大数据量时,这个比例还会增加。如果可能,尽量使用while,子查询,临时表,函数,表变量等来替代游标,记住,游标永远只是你最后无奈之下的选择,而不是首选。
复制代码 代码如下:

--定义表变量
DECLARE @temp TABLE
(
[id] INT IDENTITY(1, 1) ,
[Name] VARCHAR(10)
)
DECLARE @tempId INT ,
@tempName VARCHAR(10)
DECLARE test_Cursor CURSOR LOCAL FOR
SELECT [id],[name] FROM @temp
--插入数据值
INSERT INTO @temp
VALUES ( 'a' )
INSERT INTO @temp
VALUES ( 'b' )
INSERT INTO @temp
VALUES ( 'c' )
INSERT INTO @temp
VALUES ( 'd' )
INSERT INTO @temp
VALUES ( 'e' )
--打开游标
OPEN test_Cursor
WHILE @@FETCH_STATUS = 0
BEGIN
FETCH NEXT FROM test_Cursor INTO @tempId,@tempname
PRINT 'Name:----' + @tempName
END
CLOSE test_Cursor
DEALLOCATE test_Cursor

标签: SQLServer 遍历 表中 记录 2种 方法 使用 变量 游标


声明:本文内容来源自网络,文字、图片等素材版权属于原作者,平台转载素材出于传递更多信息,文章内容仅供参考与学习,切勿作为商业目的使用。如果侵害了您的合法权益,请您及时与我们联系,我们会在第一时间进行处理!我们尊重版权,也致力于保护版权,站搜网感谢您的分享!

站长搜索

http://www.adminso.com

Copyright @ 2007~2025 All Rights Reserved.

Powered By 站长搜索

打开手机扫描上面的二维码打开手机版


使用手机软件扫描微信二维码

关注我们可获取更多热点资讯

站长搜索目录系统技术支持