当前位置:首页 » 编程语言 » 游标sql

游标sql

发布时间: 2022-02-02 14:25:05

A. sql 中的 游标有什么作用

因为我们做的数据量大,而且系统上跑的不只我们一个业务。所以,我们都要求尽量避免使用游标,游标使用时会对行加锁,可能会影响其他业务的正常进行。而且,数据量大时其效率也较低效。另外,内存也是其中一个限制。因为游标其实是相当于把磁盘数据整体放入了内存中,如果游标数据量大则会造成内存不足,内存不足带来的影响大家都知道了。所以,在数据量小时才使用游标。

B. sql 中游标的作用及使用方法

游标可以从数据库中查询出一个结果集,在你关闭它之前,你可以反复使用这个结果集,读取这个结果集中的任意行任意字段的内容,一般在存储过程或前台程序中常见。

C. sql 游标 是什么意思

declare cr_cursor cursor --1.定义游标
for select name from dbo.sysobjects where xtype='U' and status>0
--?????? 这里是获取记录
fetch next From cr_cursor into @Table --??这里是用变量@Table保存获取到的select 【name】 from dbo.sysobjects where xtype='U' and status>0
name的值
fetch next From cr_cursor into @Table--这句话的完整意思是
将游标移动到下一条记录并将获取到是name值赋值给变量@Table
----------------------------------------------------------------------
给你一个例子 和说明 我看来几遍就学会游标了 下面是例子
---------------------------------------------------------------------
定义游标
Declare MyCursor Cursor For
Select Field1,Field2
From MyTable
Where (Field1 Like '%123%') And (Field2 = 'qqq') And (Field3 Is Not Null) And ......
Group By Field1,Field2
For Read Only
Open MyCursor

移动游标
fetch first from 游标 into 变量列表

取下一条
fetch next from 游标 into 变量列表

取第n条
fetch absolute n from 游标 into 变量列表

例子

日前,因工作需要累加某表里面的某字段的全部值,
比如有个表,内容如下
id,text
1,春花秋月何时了
2,往事知多少
3,小楼昨夜又春风
4,古国不堪回首月明中
......
其中id为系列号,text为文本内容,我想使用个sql语句,达到如下效果:
查询text列,查询的值累加,即查询结果显示如下:
春花秋月何时了 往事知多少 小楼昨夜又春风 古国不堪回首月明中 ...
用存储过程+游标实现,示例如下
ALTER PROCEDURE [dbo].[abc]
-- Add the parameters for the stored procere here
@p1 int

AS
BEGIN
-- SET NOCOUNT ON added to prevent extra result sets from
-- interfering with SELECT statements.
SET NOCOUNT ON;
declare @ttt varchar(100);
declare @bbb varchar(10);
set @ttt=''
set @bbb=''
declare mycur cursor for
select meno from test where gid= @p1
open mycur
fetch next from mycur into @bbb
WHILE @@FETCH_STATUS = 0
BEGIN
set @ttt=@ttt+@bbb
fetch next from mycur into @bbb
end
close mycur
select @ttt

D. Sql中的游标是干嘛的

游标(cursor)是结果集的逻辑扩展,可以看做指向结果集的一个指针,通过使用游标,应用程序可以逐行访问并处理结果集。

ResultSet对象用于接收查询结果,next()方法用于判断结果集是否为空,相当于指针,指向结果集下一个数据。

(4)游标sql扩展阅读:

游标的生命周期包含有五个阶段:声明游标、打开游标、读取游标数据、关闭游标、释放游标。

1、声明游标语法

DECLARE cursor_name CURSOR [ LOCAL | GLOBAL ]

[ FORWARD_ONLY | SCROLL ]
[ STATIC | KEYSET | DYNAMIC | FAST_FORWARD ]
[ READ_ONLY | SCROLL_LOCKS | OPTIMISTIC ]
[ TYPE_WARNING ]
FOR select_statement
[ FOR UPDATE [ OF column_name [ ,...n ] ] ]

2、打开游标语法

open [ Global ] cursor_name | cursor_variable_name

3、读取游标数据语法

Fetch[ [Next|prior|Frist|Last|Absoute n|Relative n ]from ][Global] cursor_name[into @variable_name[,....]]

4、关闭游标语法

close [ Global ] cursor_name | cursor_variable_name

5、释放游标语法

deallocate cursor_name

E. sql中的游标是什么怎样用呢

在数据库中,游标提供了一种对从表中检索出的数据进行操作的灵活手段。就本质而言,游标实际上是一种能从包括多条数据记录的结果集中每次提取一条记录的机制。
游标总是与一条SQL
选择语句相关联因为游标由结果集(可以是零条、一条或由相关的选择语句检索出的多条记录)和结果集中指向特定记录的游标位置组成。
游标关于数据库中的操作会对整个行集产生影响。由 SELECT 语句返回的行集包括所有满足该语句 WHERE 子句中条件的行。由语句所返回的这一完整的行集被称为结果集。
应用程序,特别是交互式联机应用程序,并不总能将整个结果集作为一个单元来有效地处理。这些应用程序需要一种机制以便每次处理一行或一部分行。游标就是提供这种机制的结果集扩展。
(5)游标sql扩展阅读:
游标通过以下方式扩展结果处理:
1.允许定位在结果集的特定行。
2.从结果集的当前位置检索一行或多行。
3.支持对结果集中当前位置的行进行数据修改。
4.为由其他用户对显示在结果集中的数据库数据所做的更改提供不同级别的可见性支持。
5.提供脚本、存储过程和触发器中使用的访问结果集中的数据的 Transact-SQL 语句。
参考资料来源:搜狗网络—游标

F. SQL中游标是指什么怎么用的又什么作用

SQL语言是面向集合的,是运用关系进行运算,最擅长于集合运算。
有些功能要求也各一个地取出记录,进行运算,正规的关系语言SQL实现不了,于是衍生出过程化的
SQL游标,来逐个的取出记录。

G. sql游标查询(急)

declare @专业代码名称 varchar(50)
declare cur_test cursor for select 专业名称 from dbo.数据总表 where 条件 group by 专业名称
open cur_test
fetch cur_test into @专业代码名称
while(@@fetch_status = 0)
begin
--这里是你自己要做的操作内容。比如查询变量值
select @专业代码名称

fetch next from cur_test into @专业代码名称
end
close cur_test
deallocate cur_test供xuanhao2016参考。如有帮助你采纳。可到IT实验室,天天件测试网论坛上进一步交流。

H. sql游标的写法

给你一个游标的写法。
此demo的目地是将游标行的name更新到另一张表的字段中条件是2张表的ID相同

Declare@Idvarchar(20)
Declare@Namevarchar(20)
DeclareCurCursorFor
selectid,namefromtemp1
OpenCur
FetchnextFromCurInto@Id,@Name
While@@fetch_status=0
Begin
UpdatetempSet[c3]=@Namewhere[id]=@Id
FetchNextFromCurInto@Id,@Name
End
CloseCur
DeallocateCur

I. SQL游标怎么用

例子
table1结构如下
id int
name varchar(50)

declare @id int
declare @name varchar(50)
declare cursor1 cursor for --定义游标cursor1
select * from table1 --使用游标的对象(跟据需要填入select文)
open cursor1 --打开游标

fetch next from cursor1 into @id,@name --将游标向下移1行,获取的数据放入之前定义的变量@id,@name中

while @@fetch_status=0 --判断是否成功获取数据
begin
update table1 set name=name+'1'
where id=@id --进行相应处理(跟据需要填入SQL文)

fetch next from cursor1 into @id,@name --将游标向下移1行
end

close cursor1 --关闭游标
deallocate cursor1

J. SQL游标如何使用

A. 在简单的游标中使用 FETCH
下例为 authors 表中姓以字母 B 开头的行声明了一个简单的游标,并使用 FETCH NEXT 逐个提取这些行。FETCH 语句以单行结果集形式返回由 DECLARE CURSOR 指定的列的值。

USE pubs
GO
DECLARE authors_cursor CURSOR FOR
SELECT au_lname FROM authors
WHERE au_lname LIKE "B%"
ORDER BY au_lname

OPEN authors_cursor

-- Perform the first fetch.
FETCH NEXT FROM authors_cursor

-- Check @@FETCH_STATUS to see if there are any more rows to fetch.
WHILE @@FETCH_STATUS = 0
BEGIN
-- This is executed as long as the previous fetch succeeds.
FETCH NEXT FROM authors_cursor
END

CLOSE authors_cursor
DEALLOCATE authors_cursor
GO

au_lname
----------------------------------------
Bennet
au_lname
----------------------------------------
Blotchet-Halls
au_lname
----------------------------------------

B. 使用 FETCH 将值存入变量
下例与上例相似,但 FETCH 语句的输出存储于局部变量而不是直接返回给客户端。PRINT 语句将变量组合成单一字符串并将其返回到客户端。

USE pubs
GO

-- Declare the variables to store the values returned by FETCH.
DECLARE @au_lname varchar(40), @au_fname varchar(20)

DECLARE authors_cursor CURSOR FOR
SELECT au_lname, au_fname FROM authors
WHERE au_lname LIKE "B%"
ORDER BY au_lname, au_fname

OPEN authors_cursor

-- Perform the first fetch and store the values in variables.
-- Note: The variables are in the same order as the columns
-- in the SELECT statement.

FETCH NEXT FROM authors_cursor
INTO @au_lname, @au_fname

-- Check @@FETCH_STATUS to see if there are any more rows to fetch.
WHILE @@FETCH_STATUS = 0
BEGIN

-- Concatenate and display the current values in the variables.
PRINT "Author: " + @au_fname + " " + @au_lname

-- This is executed as long as the previous fetch succeeds.
FETCH NEXT FROM authors_cursor
INTO @au_lname, @au_fname
END

CLOSE authors_cursor
DEALLOCATE authors_cursor
GO

Author: Abraham Bennet
Author: Reginald Blotchet-Halls

C. 声明 SCROLL 游标并使用其它 FETCH 选项
下例创建一个 SCROLL 游标,使其通过 LAST、PRIOR、RELATIVE 和 ABSOLUTE 选项支持所有滚动能力。

USE pubs
GO

-- Execute the SELECT statement alone to show the
-- full result set that is used by the cursor.
SELECT au_lname, au_fname FROM authors
ORDER BY au_lname, au_fname

-- Declare the cursor.
DECLARE authors_cursor SCROLL CURSOR FOR
SELECT au_lname, au_fname FROM authors
ORDER BY au_lname, au_fname

OPEN authors_cursor

-- Fetch the last row in the cursor.
FETCH LAST FROM authors_cursor

-- Fetch the row immediately prior to the current row in the cursor.
FETCH PRIOR FROM authors_cursor

-- Fetch the second row in the cursor.
FETCH ABSOLUTE 2 FROM authors_cursor

-- Fetch the row that is three rows after the current row.
FETCH RELATIVE 3 FROM authors_cursor

-- Fetch the row that is two rows prior to the current row.
FETCH RELATIVE -2 FROM authors_cursor

CLOSE authors_cursor
DEALLOCATE authors_cursor
GO

au_lname au_fname
---------------------------------------- --------------------
Bennet Abraham
Blotchet-Halls Reginald
Carson Cheryl
DeFrance Michel
del Castillo Innes
Dull Ann
Green Marjorie
Greene Morningstar
Gringlesby Burt
Hunter Sheryl
Karsen Livia
Locksley Charlene
MacFeather Stearns
McBadden Heather
O'Leary Michael
Panteley Sylvia
Ringer Albert
Ringer Anne
Smith Meander
Straight Dean
Stringer Dirk
White Johnson
Yokomoto Akiko

au_lname au_fname
---------------------------------------- --------------------
Yokomoto Akiko
au_lname au_fname
---------------------------------------- --------------------
White Johnson
au_lname au_fname
---------------------------------------- --------------------
Blotchet-Halls Reginald
au_lname au_fname
---------------------------------------- --------------------
del Castillo Innes
au_lname au_fname
---------------------------------------- --------------------
Carson Cheryl

热点内容
netgear远程访问 发布:2024-04-28 23:06:18 浏览:531
javaweb整合开发 发布:2024-04-28 23:03:49 浏览:456
福康中国服务器地址 发布:2024-04-28 22:47:20 浏览:745
mcryptphp 发布:2024-04-28 22:29:12 浏览:194
程序源代码加密 发布:2024-04-28 22:10:42 浏览:835
安卓转移到ios有什么变化 发布:2024-04-28 22:01:05 浏览:392
三洋电视wifi解锁密码是多少 发布:2024-04-28 21:59:36 浏览:628
东方财富登陆密码是什么 发布:2024-04-28 21:49:54 浏览:735
怎么看电脑wifi的密码 发布:2024-04-28 21:42:26 浏览:297
怎样在全民k歌上传照片 发布:2024-04-28 21:37:59 浏览:841