Declare myCursor cursor For
Select * from table1 open myCursor Fetch next From myCursor into @变量 While @@FETCH_STATUS =0 begin --处理单条记录 Fetch next From myCursor end Close myCursor Deallocate myCursor三、流程控制语句(if else | select case | while ) Select ... CASE 实例 DECLARE @iRet INT, @PKDisp VARCHAR(20) SET @iRet = '1' Select @iRet = CASE WHEN @PKDisp = '一' THEN 1 WHEN @PKDisp = '二' THEN 2 WHEN @PKDisp = '三' THEN 3 WHEN @PKDisp = '四' THEN 4 WHEN @PKDisp = '五' THEN 5 ELSE 100 END
四、存储过程格式
创建存储过程 Create Proc dbo.存储过程名 存储过程参数 AS 执行语句 RETURN 执行存储过程 GO*********************************************************/ -- 变量的声明,sql里面声明变量时必须在变量前加@符号 DECLARE @I INT-- 变量的赋值,变量赋值时变量前必须加set
SET @I = 30-- 声明多个变量
DECLARE @s varchar(10),@a INT-- Sql 里if语句
IF 条件 BEGIN 执行语句 END ELSE BEGIN 执行语句 END DECLARE @d INT set @d = 1IF @d = 1 BEGIN
-- 打印
PRINT '正确' END ELSE BEGIN PRINT '错误' END -- Sql 里的多条件选择语句. DECLARE @iRet INT, @PKDisp VARCHAR(20) SET @iRet = 1 Select @iRet = CASE WHEN @PKDisp = '一' THEN 1 WHEN @PKDisp = '二' THEN 2 WHEN @PKDisp = '三' THEN 3 WHEN @PKDisp = '四' THEN 4 WHEN @PKDisp = '五' THEN 5 ELSE 100 END-- 循环语句
WHILE 条件 BEGIN 执行语句 ENDDECLARE @i INT
SET @i = 1 WHILE @i<1000000 BEGIN set @i=@i+1 END -- 打印 PRINT @i