循环处理--sqlserver

alter PROCEDURE [dbo].[sp_gongzi] @gongzi_yf varchar(7) as
DECLARE  @input_id varchar(20)
DECLARE  @sk_sum decimal(18, 3)
DECLARE  @sk_sum_yk decimal(18, 3)
BEGIN 
    DECLARE cur_input CURSOR  FOR
            select docno from  tc_tab1 where yf = @gongzi_yf    
 
 OPEN cur_input
 
    FETCH  cur_input INTO @input_id
 WHILE @@FETCH_STATUS = 0
      BEGIN
         set @sk_sum = 0    
         set @sk_sum_yk  = 0
         select @sk_sum =  sum(je * case when (hl2 is null) then hl else hl2 end )         
             from sk_dan where docno [email protected]_id;
        
         select @sk_sum_yk =  sum(je * case when (hl2 is null) then hl else hl2 end )         
             from sk_dan_yk where docno [email protected]_id;       
               
        set @sk_sum = case when (@sk_sum is null) then 0 else @sk_sum end;       
        set @sk_sum_yk =  case when (@sk_sum_yk is null) then 0 else @sk_sum_yk end;       
       
        update    tc_tab1 set dke2 =  @sk_sum   +   @sk_sum_yk
     where docno = @input_id; 
       
   FETCH  cur_input INTO @input_id
      END
    CLOSE cur_input
    DEALLOCATE cur_input 
    --*/
END

时间: 2024-11-05 19:35:21

循环处理--sqlserver的相关文章

SqlServer中循环查询结果集

declare @id int,@value nvarchar(100); begin declare c_test_main cursor fast_forward for select hps_hpId,hps_time from hospitalPermSupp where hps_usId=1; open c_test_main;--打开游标 while 1=1 --开始循环 begin fetch next from c_test_main into @id,@value; --赋值到

SqlServer循环 和 批量倒数据

SqlServer循环语句 declare @i int set @i =1 while(@i<5) begin  set @i = @i+1  insert into text(id,name) values('1','王麻子') end SqlServer批量倒数据 如果两张表字段相同的话: insert into text1 select * from text 如果两张表字段不同的话: insert into text1(fid,fname,fage...) select fid,fna

SqlServer中循环和条件语句示例!

--                                  ╔════════╗ -- ===============================  ║ if语句使用示例 ║ --                                  ╚════════╝ declare @a int set @a=12 if @a>100 begin print @a end else begin print 'no' end --                        

sqlserver循环

sql循环,除了游标还有如下 -- --创建临时表 -- DECLARE @tradeID INT; --if not object_id('Tempdb..#A') is null -- drop table #A --Create table #A([TradeID] nvarchar(100)) --Insert #A SELECT TradeID FROM dbo.CRM_AntiFakeQRec GROUP BY TradeID --WHILE EXISTS(SELECT TradeI

20.SqlServer中if跟循环语句

--if语句declare @i int set @i=1 if @i<0 begin set @[email protected]+1 print @i end else --循环语句 declare @i int set @i=0 while @i<5 begin set @i= @i+1 insert into grade(classname) values(@i) end

sqlserver中的循环遍历(普通循环和游标循环)

sql 经常用到循环,下面介绍一下普通循环和游标循环 1.首先需要一个测试表数据Student 2.普通循环 1)循环5次来修改学生表信息 --循环遍历修改记录--declare @i int   set @i=0while @i<5begin    update Student set demo = @i+5 where [email protected]    set @[email protected] +1 end--查看结果--select * from Student 2)执行后的查

SQLServer索引循环删除

declare qc_cursor cursor SCROLL OPTIMISTIC Forselect siteName from tb_vhostcheck open qc_cursordeclare @siteName nvarchar(50) while @@fetch_status=0begin fetch next from qc_cursor into @siteName--print @siteName if not exists(select 1 from vhhostlist

Sqlserver 循环表

CREATE TABLE dbo.[User] ( UID BIGINT IDENTITY ,Name VARCHAR(50) NOT NULL ,Pwd VARCHAR(50) NOT NULL ,CONSTRAINT PK_User PRIMARY KEY CLUSTERED (UID) ) ON [PRIMARY] -- 方法2:使用表变量 -- 声明表变量 DECLARE @temp TABLE ( UID BIGINT, Name VARCHAR(50), Pwd VARCHAR(50

sqlserver 循环加类型转换拼接

declare @i int  set @i=1;  while @i<50  begin  insert [GCSDB].[dbo].[ORGANIZATION_UNIT] (      [ORG_UNIT_ID]      ,[ORG_UNTI_NAME]      ,[ORG_UNTI_REMARK]      ,[UNIT_IS_SIMULATE])      values (@i,cast(@i as varchar(10))+'号','',1)   set @i = @i + 1