SQL - 运维篇
SQL笔记-While语法
MySQL while语法
### MySQL while语法
DELIMITER //
DROP PROCEDURE IF EXISTS Pgm_while_001;
CREATE PROCEDURE Pgm_while_001() BEGIN
DECLARE flag INT DEFAULT 0;
DECLARE tmphost VARCHAR(40);
DECLARE num int default 0;
DECLARE hostlist CURSOR FOR SELECT HostName FROM `sql_table1` WHERE IPv4Address != '';
DECLARE CONTINUE HANDLER FOR NOT FOUND SET flag=1;
OPEN hostlist;
FETCH hostlist INTO tmphost;
WHILE flag <> 1 DO
SET @newipv4=(SELECT IPv4Address FROM `sql_table1` WHERE IPv4Address != '' AND HostName = tmphost LIMIT 0,1);
SET @newmac=(SELECT MacAddress FROM `sql_table1` WHERE MacAddress != '' AND HostName = tmphost LIMIT 0,1);
UPDATE `sql_table1` SET IPv4Address=@newipv4,MacAddress=@newmac WHERE HostName = tmphost AND IPv4Address = '';
FETCH hostlist INTO tmphost;
END WHILE;
CLOSE hostlist;
END
//
DELIMITER ;
CALL Pgm_while_001();
MSSQL while语法-案例1
### MSSQL While语法
# 案例1
USE sql_database
DECLARE
@Id uniqueidentifier,
@UserCode nvarchar(50) ,
@UserAlias nvarchar(50) ,
@HostName nvarchar(50) ,
@IPv4Address nvarchar(50) ,
@MacAddress nvarchar(50) ,
@LoginName nvarchar(50) ,
@DeleteTime datetime
DECLARE id_cursor CURSOR
FOR (SELECT TOP 1000 Id,UserCode,UserAlias,HostName,IPv4Address,MacAddress,LoginName,DeleteTime FROM sql_table1)
OPEN id_cursor
FETCH NEXT FROM id_cursor INTO @Id,@UserCode,@UserAlias,@HostName,@IPv4Address,@MacAddress,@LoginName,@DeleteTime
WHILE @@FETCH_STATUS=0
BEGIN
SET @var001=CONVERT(nvarchar(36),@Id)+'.txt'
INSERT INTO sql_table2
VALUES (@Id,@UserCode,@UserAlias,@HostName,@IPv4Address,@MacAddress,@LoginName,@var001,@DeleteTime)
FETCH NEXT FROM id_cursor INTO @Id,@UserCode,@UserAlias,@HostName,@IPv4Address,@MacAddress,@LoginName,@DeleteTime
END
CLOSE id_cursor
DEALLOCATE id_cursor
MSSQL while语法-案例2文章来源:https://www.toymoban.com/news/detail-568359.html
### MSSQL While语法
# 案例2
USE sql_database
DECLARE
@HostName nvarchar(50)
DECLARE a_cursor CURSOR
FOR (SELECT HostName FROM sql_table1 WHERE IPv4Address='')
OPEN a_cursor
FETCH NEXT FROM a_cursor INTO @HostName
WHILE @@FETCH_STATUS=0
BEGIN
UPDATE sql_table1 SET IPv4Address=(SELECT TOP 1 IPv4Address FROM sql_table1 WHERE HostName=@HostName and IPv4Address <> ''),
MacAddress=(SELECT TOP 1 MacAddress FROM sql_table1 WHERE HostName=@HostName AND MacAddress <> '') WHERE HostName=@HostName AND IPv4Address = ''
FETCH NEXT FROM a_cursor INTO @HostName
END
CLOSE a_cursor
DEALLOCATE a_cursor
参考来源
无文章来源地址https://www.toymoban.com/news/detail-568359.html
到了这里,关于[个人笔记] SQL笔记-While语法的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!