sqlserver游标概念与实例
我们先不讲游标的什么概念,步骤及语法,先来看一个例子: 表一 OriginSalary 表二 AddSalary 现在有2张表,一张是OriginSalary表--工资表,有三个字段0_ID 员工号(NVARCHAR)、O_Name员工姓名(NVARCHAR)、O_Salary工资(FLOAT)。 另一张表AddSalary表—
我们先不讲游标的什么概念,步骤及语法,先来看一个例子:
表一 OriginSalary 表二 AddSalary
现在有2张表,一张是OriginSalary表--工资表,有三个字段0_ID 员工号(NVARCHAR)、O_Name员工姓名(NVARCHAR)、O_Salary工资(FLOAT)。
另一张表AddSalary表—加薪表。有2个字段,O_ID员工号、A_Salary增加工资。两张表的O_ID是一一对应的,现在求将加薪的工资+原来的工资=现在的工资,也就是O_Salary=O_Salary+A_Salary,修改表OriginSalary的工资字段。
对于一些不熟悉游标的程序员来说,这个并不是什么很难的问题,这个问题用程序来实现可能也很简单。我先说说,用ASP.NET程序解决这个问题的思路:
1. 先获得表OriginSalary的记录数,写个循环。
2. 写SQL语句“select * from dbo.OriginSalary as A left join dbo.AddSalary as B on A.O_ID=B.O_ID”获得视图。
3. 使用Dataset获得O_Salary=O_Salary+A_Salary。
4. 写UPDATE语句“update OriginSalary set O_Salary=”相加的值” where O_ID=”获得值”
5. 循环3次,完成此功能。
还有一种方法就是写存储过程,在这里我就不列出来了。
我想大家在学习游标之前好好想想这个问题,及一些批量处理的例子。可能有的人会说:“难道数据库不能一行一行的处理数据吗?将表AddSalary的数据逐行的取出,然后表 OriginSalary数据逐行的修改?”答案当然是肯定。这就是游标概念。接下来的一章我们会好好的讲讲什么是游标?我会用游标来解决刚才留给大家的问题。
1.1游标的概念
游标(Cursor)它使用户可逐行访问由SQL Server返回的结果集。使用游标(cursor)的一个主要的原因就是把集合操作转换成单个记录处理方式。用SQL语言从数据库中检索数据后,结果放在内存的一块区域中,且结果往往是一个含有多个记录的集合。游标机制允许用户在SQL server内逐行地访问这些记录,按照用户自己的意愿来显示和处理这些记录。
1.2 游标的优点
从游标定义可以得到游标的如下优点,这些优点使游标在实际应用中发挥了重要作用:
1)允许程序对由查询语句select返回的行集合中的每一行执行相同或不同的操作,而不是对整个行集合执行同一个操作。
2)提供对基于游标位置的表中的行进行删除和更新的能力。
3)游标实际上作为面向集合的数据库管理系统(RDBMS)和面向行的程序设计之间的桥梁,使这两种处理方式通过游标沟通起来。
1.3 游标的使用
讲了这个多游标的优点,现在我们就亲自来揭开游标的神秘的面纱。
使用游标的顺序: 声名游标、打开游标、读取数据、关闭游标、删除游标。
1.3.1声明游标
最简单游标声明:DECLARE CURSOR FOR
让我看看效果吧(如图)
通过实例我们可以看到游标逐行逐行都把值都取出来了。那么我请大家先不看下面的答案,在引言部分我刚才留个大家的问题试一下能不能解决?
现在我们写一个存储过程解决刚才我留下来的问题吧
CREATE PROCEDURE PK_SalaryAdd
AS
declare @O_ID nvarchar(20),@A_Salary float
declare mycursor cursor for select O_ID,A_Salary from AddSalary
open mycursor
fetch next from mycursor into @O_ID,@A_Salary
while(@@fetch_status = 0)
begin
Update OriginSalary set O_Salary=O_Salary+@A_Salary where O_ID=@O_ID
fetch next from mycursor into @O_ID,@A_Salary
end
close mycursor
deallocate mycursor
GO
按照老方法,我们用查询分析器来执行我们的存储过程,看看结果是怎么样的?
Exec PK_SalaryAdd
让我看看效果吧(如图)
执行存储过程,看到我们影响了3行数据
用sql语句,看看表OriginSalary现在的结果:
http://www.cnblogs.com/wudiwushen/archive/2010/03/30/1700925.html

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



For objects with the same name that already exist in the SQL Server database, the following steps need to be taken: Confirm the object type (table, view, stored procedure). IF NOT EXISTS can be used to skip creation if the object is empty. If the object has data, use a different name or modify the structure. Use DROP to delete existing objects (use caution, backup recommended). Check for schema changes to make sure there are no references to deleted or renamed objects.

The import steps are as follows: Copy the MDF file to SQL Server's data directory (usually C:\Program Files\Microsoft SQL Server\MSSQL\DATA). In SQL Server Management Studio (SSMS), open the database and select Attach. Click the Add button and select the MDF file. Confirm the database name and click the OK button.

When the SQL Server service fails to start, here are some steps to resolve: Check the error log to determine the root cause. Make sure the service account has permission to start the service. Check whether dependency services are running. Disable antivirus software. Repair SQL Server installation. If the repair does not work, reinstall SQL Server.

To view the SQL Server port number: Open SSMS and connect to the server. Find the server name in Object Explorer, right-click it and select Properties. In the Connection tab, view the TCP Port field.

SQL Server database files are usually stored in the following default location: Windows: C:\Program Files\Microsoft SQL Server\MSSQL\DATALinux: /var/opt/mssql/data The database file location can be customized by modifying the database file path setting.

If you accidentally delete a SQL Server database, you can take the following steps to recover: stop database activity; back up log files; check database logs; recovery options: restore from backup; restore from transaction log; use DBCC CHECKDB; use third-party tools. Please back up your database regularly and enable transaction logging to prevent data loss.

If the SQL Server installation fails, you can clean it up by following these steps: Uninstall SQL Server Delete registry keys Delete files and folders Restart the computer

SQL Server English installation can be changed to Chinese by following the following steps: download the corresponding language pack; stop the SQL Server service; install the language pack; change the instance language; change the user interface language; restart the application.
