通过使用for循环或while循环解决问题

通过使用for循环或while循环解决问题

本文介绍了通过使用for循环或while循环解决问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  DECLARE   @ count   int ,
@ index int ,
@ EMP_ID varchar ( 50 ),
@Emp_Name varchar ( 50 ),
@ ID int
SET @ index = 0;

truncate table PercentageTable;

insert into empdetail(EMP_ID) SELECT (Emp_ID)来自 DetailedPerformanceMonthlyReport;
插入 进入 PercentageTable(EMP_ID,EMP_NAME) SELECT (Emp_ID,EMP_NAME)来自 DetailedPerformanceMonthlyReport;
while ( @ index < @EMP_ID)
BEGIN
SET @ index = @ index + 1;

INSERT INTO PercentageTable(PERCENTAGE)
(选择 CAST((现在/ CAST(totdays AS DECIMAL ( 10 , 2 )))* 100 AS DECIMAL ( 5 , 2 ) )来自 DetailedPerformanceMonthlyReport);
( SELECT EMP_NAME 来自 DetailedPerformanceMonthlyReport 其中 EMP_NAME = Emp_Name));


插入 进入 PercentageTable(EMP_NAME) SELECT (Emp_Name)来自 DetailedPerformanceMonthlyReport;

END





错误:

消息102,级别15,状态1,行16 
','附近的语法不正确。
消息102,等级15,状态1,行23
')'附近的语法不正确。
消息102,等级15,状态1,行26
';'附近的语法不正确。





empdetail由两列组成 ID EMP_ID

百分表包含三列 EMP_ID EMP_NAME PERCENTAGE



我想将三列数据推入百分比表,但是当iam尝试执行时,只有一列正在执行,剩下的表在表中显示为NULL。



任何人都可以通过使用for循环或while循环帮助我解决问题因为我是SQL的初学者。

解决方案




DECLARE @count int,
@index int,
@EMP_ID varchar(50),
@Emp_Name varchar(50),
@ID int
SET @index=0;

truncate table PercentageTable;

 insert into empdetail(EMP_ID) SELECT (Emp_ID) from DetailedPerformanceMonthlyReport;
 insert into PercentageTable(EMP_ID,EMP_NAME) SELECT (Emp_ID,EMP_NAME)from DetailedPerformanceMonthlyReport;
 while(@index <@EMP_ID)
BEGIN
SET @index= @index+1;

INSERT INTO PercentageTable(PERCENTAGE)
  (select CAST((Present/CAST(totdays AS DECIMAL(10,2)))*100 AS DECIMAL(5,2)) from DetailedPerformanceMonthlyReport);
 (SELECT EMP_NAME from DetailedPerformanceMonthlyReport where EMP_NAME=Emp_Name));


 insert into PercentageTable(EMP_NAME) SELECT (Emp_Name)  from DetailedPerformanceMonthlyReport;

 END



ERRORS:

Msg 102, Level 15, State 1, Line 16
Incorrect syntax near ','.
Msg 102, Level 15, State 1, Line 23
Incorrect syntax near ')'.
Msg 102, Level 15, State 1, Line 26
Incorrect syntax near ';'.



empdetail consists of two columns ID, EMP_ID
percentagetable consists of three columns EMP_ID, EMP_NAME, PERCENTAGE

I want to push the three columns data in to percentage table but when iam trying to execute only one column is executing and remaining shows NULL in the table.

Can anyone help me to solve the problem by using for loop or while loop because iam a begineer to SQL.

解决方案




这篇关于通过使用for循环或while循环解决问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-14 17:03