结果:
新建函数:
GO /****** 对象: UserDefinedFunction [dbo].[fun_get_class_nam] 脚本日期: 07/28/2012 07:26:23 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO /***************************************************** ** PROCEDURE : fun_get_class_nam ** DECRIPTION: 根据班级id获取班级名称 ** DATE : 2010-07-28 ** VERSION AUTH DATE DESC ** -------- ------------ ------------ ----------------- ** V000.0.1 pukuimin 2010-07-28 新建程序 ** -------- ------------ ------------ ----------------- *******************************************************/ alter function [dbo].[fun_get_class_nam]( @class_id bigint ) returns varchar(200) as begin declare @class_nam varchar(200) set @class_nam = null select @class_nam = className from classinfo where classID = @class_id AND stat='1' return @class_nam END
查询语句中使用函数:
SELECT id, username, userpwd, classID, dbo.fun_get_class_nam(classID) AS classname, score, age FROM stuinfo
下面是游标的简单用法:
DECLARE cur CURSOR --声明游标 FOR SELECT score,age FROM stuinfo Open cur --打开游标 DECLARE @score int --接收游标数据的变量,与上面的声明游标查询语句的个数相同 DECLARE @age int fetch Next from cur into @score,@age --查询一次数据到变量 WHILE (@@FETCH_STATUS = 0 ) --如果不为空就循环读取 BEGIN IF (@score<85 AND @age <18) --对符合条件的进行操作 UPDATE stuinfo SET score = 888 WHERE CURRENT OF cur FETCH Next FROM cur INTO @score,@age --查询下一条 END CLOSE cur --关闭游标 DEALLOCATE cur --释放游标
时间: 2024-10-25 09:34:10