通过使用for循环或while循环解决问题 [英] solve the problem by using for loop or while loop

查看:79
本文介绍了通过使用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的初学者。

解决方案

我不确定varchar类型of yor identifier列。您可以尝试以下代码:

  DECLARE   @ EMP_ID   VARCHAR  50 ); 
- SET @EMP_ID = ... - 无论你想要什么

INSERT INTO empdetail(EMP_ID)
SELECT MergeResult.Emp_ID
FROM
MERGE PercentageTable AS 目标
使用 SELECT Emp_ID,Emp_Name,
CAST((现在/ CAST( CASE WHEN ISNULL(totdays, 0 )= 0
THEN -1
ELSE totdays END AS DECIMAL 10 2 )))* 100 AS DECIMAL 5 2 )) AS 百分比
FROM DetailedPerformanceMonthlyReport
WHERE Emp_ID< = @ EMP_ID AS 来源
ON 目标 .Emp_ID = 来源 .Emp_ID AND
目标 .Emp_Name = 来源 .Emp_Name
WHEN MATCH ED
THEN UPDATE SET 百分比= 来源 .Percentage
WHEN NOT MATCHED
那么 INSERT (Emp_ID,Emp_Name,Percentage)
VALUES 来源 .Emp_ID ,来源 .Emp_Name,来源 .Percentage)
OUTPUT


action AS 操作,已插入。*) AS MergeResult
WHERE MergeResult.action = INSERT';



您可以将-1更改为任何数字以捕获除以零的异常


  truncate   table  empdetail; 
truncate table percentagetable;
DECLARE @ index int
@ Count int
设置 @ index = 1;
设置 @ Count = 0
开始
插入 empdetail(Emp_Id,Emp_name)(选择 Emp_id, Emp_Name 来自 DetailedPerformanceMonthlyReport);
选择 @ Count = COUNT(ID)来自 empdetail;
插入 PercentageTable(EMP_ID,EMP_NAME)( SELECT Emp_Id,Emp_name 来自 empdetail);

while (@ index< = @ Count)
begin
选择 @Emp_id = EMP_ID 来自 empdetail 其中 ID = @ index;
UPDATE PercentageTable set PERCENTAGE =( SELECT CAST((Present / CAST(totdays AS DECIMAl ))* 100 AS DECIMAl From DetailedPerformanceMonthlyReport 其中 Emp_ID = @Emp_id 其中​​ Emp_ID = @ EMP_ID ;
SET @ index = @ index + 1;


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.

解决方案

I'm not sure about varchar type of yor identifier column. You could try this code:

DECLARE @EMP_ID VARCHAR(50);
--SET @EMP_ID = ... -- Whatever you wish

INSERT INTO empdetail(EMP_ID)
SELECT MergeResult.Emp_ID
  FROM (
 MERGE PercentageTable AS Target
 USING (SELECT Emp_ID, Emp_Name,
               CAST((Present/CAST(CASE WHEN ISNULL(totdays, 0) = 0
                                       THEN -1
                                  ELSE totdays END AS DECIMAL(10,2)))*100 AS DECIMAL(5,2)) AS Percentage
          FROM DetailedPerformanceMonthlyReport
         WHERE Emp_ID <= @EMP_ID) AS Source
    ON Target.Emp_ID = Source.Emp_ID AND
       Target.Emp_Name = Source.Emp_Name
  WHEN MATCHED
       THEN UPDATE SET Percentage = Source.Percentage
  WHEN NOT MATCHED
       THEN INSERT (Emp_ID, Emp_Name, Percentage)
            VALUES (Source.Emp_ID, Source.Emp_Name, Source.Percentage)
            OUTPUT 


action AS action, Inserted.*) AS MergeResult WHERE MergeResult.action = 'INSERT';


You could change -1 to any number to catch the division by zero exception


truncate table empdetail;
truncate table percentagetable;
DECLARE @index int,
@Count int
Set @index=1;
Set @Count=0
begin
Insert into empdetail(Emp_Id,Emp_name) (Select Emp_id,Emp_Name from DetailedPerformanceMonthlyReport);
Select @Count=COUNT(ID) From empdetail;
Insert into PercentageTable (EMP_ID,EMP_NAME)(SELECT Emp_Id,Emp_name from empdetail);

while(@index<=@Count)
begin
Select @Emp_id=EMP_ID from empdetail where ID=@index;
UPDATE PercentageTable set PERCENTAGE=(SELECT CAST((Present/CAST(totdays AS DECIMAl))*100 AS DECIMAl) From DetailedPerformanceMonthlyReport where Emp_ID = @Emp_id)where Emp_ID = @Emp_id;
SET @index = @index+1;


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

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆