所有由雷老虎发布的文章

修改docker及网桥的IP地址

docker默认的网卡地址创建的网桥地址一般是 172.18,172.17这样的,但是有时候我们的VPN地址也是这些,如果VPN的地址不能改,那么就只能改docker的地址了。

docker主机地址的修改方法:

/etc/docker/daemon.json -- 如果不存在就创建一个

{
"bip": "172.27.0.1/16"
}

重启docker服务后,地址就会修改成功

如果之前已经创建了和vpn地址冲突的网桥,需要删除重建,删除前先停掉原有容器
docker stop 容器名或者ID
docker network rm 网桥名  # 如果不记得可以 docker network ls 查看
# 新建网桥, 因为原来的已经删除了,还可以用原来的名字
docker network create --subnet=172.28.0.0/16 testnet
# 即便名字相同,原来的容器如果用到这个网桥,也会无法启动,需要断开重新连接
docker network disconnect old_bridge_name container_name_or_id
docker network connect new_bridge_name container_name_or_id

再次启动容器,就会使用新的地址了。

但是如果你的程序中使用具体的IP地址,需要修改。

如何把一个父子结构的表拉成平面表

有个MSSQL表结构如下:

org_dept:部门(pk_dept:部门ID, name:部门名称, pk_fatherorg:上级部门ID)

根部门的上级部门ID是’~’

有两个方案:

方案一:用嵌套表方式生成视图;方案二:用函数实现传入ID传出各级部门,然后再展开

方案一代码:

alter view v_dept_flat
as
WITH Reordered_Dept AS (
    SELECT 
        pk_dept, 
        name,
        pk_fatherorg,
        CAST(name AS VARCHAR(8000)) AS name_1,
        CAST('' AS VARCHAR(8000)) AS name_2,
        CAST('' AS VARCHAR(8000)) AS name_3,
        CAST('' AS VARCHAR(8000)) AS name_4,
        CAST('' AS VARCHAR(8000)) AS name_5,
        CAST('' AS VARCHAR(8000)) AS name_6,
        CAST('' AS VARCHAR(8000)) AS name_7,
        CAST('' AS VARCHAR(8000)) AS name_8,
        CAST('' AS VARCHAR(8000)) AS name_9,
        CAST('' AS VARCHAR(8000)) AS name_10,
        CAST(pk_dept AS VARCHAR(20)) AS id_1,
        CAST('' AS VARCHAR(20)) AS id_2,
        CAST('' AS VARCHAR(20)) AS id_3,
        CAST('' AS VARCHAR(20)) AS id_4,
        CAST('' AS VARCHAR(20)) AS id_5,
        CAST('' AS VARCHAR(20)) AS id_6,
        CAST('' AS VARCHAR(20)) AS id_7,
        CAST('' AS VARCHAR(20)) AS id_8,
        CAST('' AS VARCHAR(20)) AS id_9,
        CAST('' AS VARCHAR(20)) AS id_10,
        1 AS level
    FROM org_dept
    WHERE pk_fatherorg = '~'
	and enablestate = 2 and hrcanceled = 'N'

    UNION ALL

    SELECT 
        d.pk_dept, 
        d.name,
        d.pk_fatherorg,
        r.name_1,
        CASE WHEN r.level = 1 THEN CAST(d.name as VARCHAR(8000)) ELSE r.name_2 END,
        CASE WHEN r.level = 2 THEN CAST(d.name as VARCHAR(8000)) ELSE r.name_3 END,        
        CASE WHEN r.level = 3 THEN CAST(d.name as VARCHAR(8000)) ELSE r.name_4 END,        
        CASE WHEN r.level = 4 THEN CAST(d.name as VARCHAR(8000)) ELSE r.name_5 END,        
        CASE WHEN r.level = 5 THEN CAST(d.name as VARCHAR(8000)) ELSE r.name_6 END,        
        CASE WHEN r.level = 6 THEN CAST(d.name as VARCHAR(8000)) ELSE r.name_7 END,        
        CASE WHEN r.level = 7 THEN CAST(d.name as VARCHAR(8000)) ELSE r.name_8 END,        
        CASE WHEN r.level = 8 THEN CAST(d.name as VARCHAR(8000)) ELSE r.name_9 END,        
        CASE WHEN r.level = 9 THEN CAST(d.name as VARCHAR(8000)) ELSE r.name_10 END,        
        r.id_1,
        CASE WHEN r.level = 1 THEN CAST(d.pk_dept as VARCHAR(20)) ELSE r.id_2 END,
        CASE WHEN r.level = 2 THEN CAST(d.pk_dept as VARCHAR(20)) ELSE r.id_3 END,        
        CASE WHEN r.level = 3 THEN CAST(d.pk_dept as VARCHAR(20)) ELSE r.id_4 END,        
        CASE WHEN r.level = 4 THEN CAST(d.pk_dept as VARCHAR(20)) ELSE r.id_5 END,        
        CASE WHEN r.level = 5 THEN CAST(d.pk_dept as VARCHAR(20)) ELSE r.id_6 END,        
        CASE WHEN r.level = 6 THEN CAST(d.pk_dept as VARCHAR(20)) ELSE r.id_7 END,        
        CASE WHEN r.level = 7 THEN CAST(d.pk_dept as VARCHAR(20)) ELSE r.id_8 END,        
        CASE WHEN r.level = 8 THEN CAST(d.pk_dept as VARCHAR(20)) ELSE r.id_9 END,        
        CASE WHEN r.level = 9 THEN CAST(d.pk_dept as VARCHAR(20)) ELSE r.id_10 END,        
        r.level + 1
    FROM org_dept d
    INNER JOIN Reordered_Dept r ON d.pk_fatherorg = r.pk_dept
	where enablestate = 2 and hrcanceled = 'N'

)

SELECT * FROM Reordered_Dept

方案二代码:

CREATE function [dbo].[fn_get_full_dept_info](@dept nvarchar(20))
RETURNS nvarchar(MAX) as
begin
	-- declare @dept nvarchar(20)
	declare @pk nvarchar(20)
	declare @name nvarchar(200)
	declare @id int
	declare @row int
	declare @pid nvarchar(20)
	declare @result nvarchar(MAX)

	DECLARE @array TABLE (
	  id INT primary key identity(1,1)
	  ,pk nvarchar(20)
	  ,name nvarchar(200)
	);

	-- set @dept = '1001A11000000000201G'
	set @row = 1

	select @pk = pk_dept, @name = name, @pid = pk_fatherorg from org_dept where pk_dept = @dept
	insert into @array(pk,name) values(@pk, @name)
	while @pid <> '~' begin
		select @pk = pk_dept, @name = name, @pid = pk_fatherorg from org_dept where pk_dept = @pid
		insert into @array(pk,name) values(@pk, @name)
		set @row = @row + 1
	end
	
	set @result = ( select 1+@row - id as id,pk,name from @array order by id for json auto)

	return @result
end

函数的使用方法如下,由于函数返回的信息比较多,使用了JSON格式,在使用结果的时候,可以按需用JSON函数解析

select pk_dept,code,name
, JSON_VALUE(JSON_QUERY(dbo.fn_get_full_dept_info(pk_dept), CONCAT('$[', 0, ']')), '$.name') AS name
, JSON_VALUE(JSON_QUERY(dbo.fn_get_full_dept_info(pk_dept), CONCAT('$[', 1, ']')), '$.name') AS name
, JSON_VALUE(JSON_QUERY(dbo.fn_get_full_dept_info(pk_dept), CONCAT('$[', 2, ']')), '$.name') AS name
, JSON_VALUE(JSON_QUERY(dbo.fn_get_full_dept_info(pk_dept), CONCAT('$[', 3, ']')), '$.name') AS name
, JSON_VALUE(JSON_QUERY(dbo.fn_get_full_dept_info(pk_dept), CONCAT('$[', 4, ']')), '$.name') AS name
from org_dept