USE [MIOT_BI]
GO
/****** Object: StoredProcedure [dbo].[p_images_insert_scheck] Script Date: 2019/4/25 15:22:09 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[p_images_insert_scheck]
(
@ps_batch VARCHAR(30) ,
@data_starttime DATETIME ,
@data_endtime DATETIME
)
/***********************************************************
authour:zhouws
date:2019-04-19
hint:定制存储过程,从E盘读取图片的路径,存储到数据库
方便页面展示(该图片是设备直接上传的,未经过我们系统)
逻辑:根据传入的参数,按天遍历文件
************************************************************/
AS
BEGIN
declare @start_time datetime=@data_starttime , -- 数据抽取的开始时间
@end_time datetime=@data_endtime, -- 数据抽取的结束时间
@path_date varchar(200),
@shift_date varchar(200),
@Root_Path nvarchar(260) ='C:\XX现场EL图片\EL外观检\', -- E:\东磁现场EL图片\
@Path nvarchar(260),
@IP varchar(200) ='ftp://192.168.31.240/'
while cast(@start_time as date)<=cast(@end_time as date)
begin
SET @shift_date = CONVERT(varchar(100), @start_time, 23);
delete from images_insert_scheck where shift_date=@shift_date;
SET @path_date=substring(CONVERT(varchar(100), @start_time, 23),1,4)+substring(CONVERT(varchar(100), @start_time, 23),6,2)+substring(CONVERT(varchar(100), @start_time, 23),9,2);
SET @Path=@Root_Path+@path_date
IF RIGHT(@Path, 1) <> '/'
SET @Path = @Path + '/'
IF OBJECT_ID('tempdb..#') IS NOT NULL
DROP TABLE #
CREATE TABLE #(
id int IDENTITY,
directory nvarchar(260),
depth int,
IsFile bit)
INSERT # EXEC master.dbo.xp_dirtree
@path = @path,
@depth = 0,
@file = 1
DECLARE @depth int, @depthMax int
UPDATE # SET
directory = @Path + directory
WHERE depth = 1
SELECT
@depth = 2,
@depthMax = MAX(depth)
FROM #
WHILE @depth <= @depthMax
BEGIN
UPDATE A SET
directory = (
SELECT TOP 1
directory
FROM #
WHERE depth = @depth - 1
AND IsFile = 0
AND id < A.id
ORDER BY id DESC
) + N'/' + directory
FROM # A
WHERE depth = @depth
SET @depth= @depth + 1
END
insert into images_insert_scheck(shift_date,directory,barcode)
SELECT @shift_date,directory,directory FROM # where isfile=1
update images_insert_scheck
set directory = @IP+SUBSTRING(directory,len(@Root_Path)+1,len(directory)),
barcode=(case when charindex('_',SUBSTRING(barcode,34,100),1)>0
then substring(SUBSTRING(barcode,34,100),1,len(SUBSTRING(barcode,34,100))-6)
when charindex('_',SUBSTRING(barcode,34,100),1)=0
then substring(SUBSTRING(barcode,34,100),1,len(SUBSTRING(barcode,34,100))-4)
else SUBSTRING(barcode,34,100)
end)
where shift_date=@shift_date;
SET @start_time = DATEADD(dd, 1, @start_time); --开始下一轮循环
end;
END;
GO