当前位置:网站首页>BI-SQL丨WHILE
BI-SQL丨WHILE
2022-07-30 14:11:00 【PowerBI 丨 white tea】
WHILE
WHILE,when it means.在SQL的语法中,Often used to repeat a sectionSQL脚本.
通俗来说,就是当XXX时,执行某某操作,意为循环.
看到这里,小伙伴可能会有疑问,PowerBIIs there a similar operation in ?
答案是有的,But we usually do this kind of thingPowerQuery中进行的,在DAXThere are fewer scenarios in which loop processing is required.
而在SQL中,There are many scenarios that need to be handled with a loop statement,Such as data update or incremental calculation.
语法
WHILE 条件判断
BEGIN
执行操作
SET @[email protected]+1 --The parameters are incremented cyclically
END
注意:
If two or more are nested WHILE 循环,All statements up to the end of the inner loop are run first,然后下一个外层循环重新开始执行.
使用实例
案例数据:
在白茶本机的数据库中存在名为“CaseData”的数据库.
“Dim_Date“日期表、"Dim_Product"产品表、"Fact_Sales"销售事实表.
例子1:
循环打印数字,从1到9.
DECLARE @NUM INT;
SET @NUM = 1;
WHILE @NUM<= 9
BEGIN
PRINT @NUM
SET @NUM = @NUM + 1;
END
结果如下:
注:This operation is not availablePowerBI中执行,会有报错.
例子2:
创建一张表,and loop through the table insertsPrice小于等于10的ProductName和Price.
USE CaseData
CREATE TABLE BaiCha
(Pname VARCHAR(50),
Prict INT
)
先创建一张表,结果如下:
执行以下语句:
USE CaseData
DECLARE @NUM INT;
SET @NUM=1
WHILE @NUM<= 10
BEGIN
INSERT INTO BaiCha(Pname,Prict)
SELECT ProductName AS T1,Price AS T2 FROM Dim_Product WHERE [email protected]
SET @NUM = @NUM + 1;
END
结果如下:
我们来查看一下数据:
可以看到,价格小于等于10The product information has been inserted into the target table.
例子3:
利用双层循环,计算1到9的平方.
USE CaseData
DECLARE @NUM1 INT,@NUM2 INT ,@NUM3 VARCHAR(10);
SET @NUM1=1;
WHILE @NUM1<= 9
BEGIN
SET @NUM2=1
WHILE @NUM2<[email protected]
BEGIN
SET @[email protected]*@NUM1
SET @[email protected]+1
END
PRINT @NUM3
SET @[email protected]+1
END
结果如下:
小Bug提示:There are often small partners who write and write in a loop and then get confused,The execution loop cannot end,Also no results are displayed,这种情况下,需要对BEGINIncrement insideSET进行检测,See if it is caused by not setting the increment.
这里是白茶,一个PowerBI的初学者.
边栏推荐
猜你喜欢
3 years of software testing experience, the interview requires a monthly salary of 22K, obviously he has memorized a lot of interview questions...
canvas彩虹桥动画js特效
js背景切换时钟js特效代码
数据中台建设(五):打破企业数据孤岛和提取数据价值
What is the level of Ali P7?
华为7年经验的软件测试总监,给所有想转行学软件测试的朋友几点建议
Conversion between pytorch and keras (the code takes LeNet-5 as an example)
Shell变量与赋值、变量运算、特殊变量、重定向与管渠
LeetCode二叉树系列——144.二叉树的最大深度
逻辑漏洞----权限类漏洞
随机推荐
创意loadingjs特效小点跳跃动画
SQL 改写系列七:谓词移动
LeetCode二叉树系列——199二叉树的右视图
业内人士真心话:只会测试没有前途的,我慌了......
Flask框架——Flask-Mail邮件
There is a risk of water ingress in the battery pack tray and there is a potential safety hazard. 52,928 Tang DMs are urgently recalled
(HR Interview) Most Common Interview Questions and Skilled Answers
以unity3d为例解读:游戏数据加密
cpu/CS and IP
No-code development platform application visible permission setting introductory tutorial
BUUCTF刷题十一道(06)
mongodb打破原则引入SQL,它到底想要干啥?
Conversion between pytorch and keras (the code takes LeNet-5 as an example)
简单理解精确率(Precision),召回率(Recall),准确率(Accuracy),TP,TN,FP,FN
LeetCode二叉树系列——145.二叉树的后序遍历
Six-faced ant financial clothing, resisting the bombardment of the interviewer, came to interview for review
无代码开发平台应用可见权限设置入门教程
Eleven BUUCTF questions (06)
Hello,World
LoRaWAN网关源码分析(V1.0.2)