首页 > 数据库 >Sql Server 存储过程

Sql Server 存储过程

时间:2023-08-05 11:23:08浏览次数:35  
标签:存储 LIKE sp WHEN Server Sql 800 nvarchar configure

---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

Access文件导入数据

---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

USE [Usp]
GO
/****** Object: StoredProcedure [dbo].[usp_DaoRuAccess] Script Date: 2023/8/5 11:02:51 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER PROCEDURE [dbo].[usp_DaoRuAccess]
@Url_A nvarchar(800),
@Path_A nvarchar(800)=0,
@Tb_A nvarchar(800),
@Sheet_A nvarchar(800),
@InsertSQL_A nvarchar(1000)=0
AS
BEGIN
SET NOCOUNT ON;
---#开启服务#---
exec sp_configure 'show advanced options',1
reconfigure
exec sp_configure 'Ad Hoc Distributed Queries',1
reconfigure

---#拼接Access路径与传入参数#---
set @Path_A = '''Microsoft.ACE.OLEDB.12.0'',''Data Source=' + cast(@URL_A as varchar(800)) + ';User ID=Admin;Password='''
set @InsertSQL_A =N'SELECT * into ##'+@Tb_A+' FROM OpenDataSource('+@Path_A+')...['+@Sheet_A+']'
exec (@InsertSQL_A)

---#关闭服务#---
exec sp_configure 'Ad Hoc Distributed Queries',0
reconfigure
exec sp_configure 'show advanced options',0
reconfigure

END

---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

Access文件导入数据 批量导入

---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

USE [Usp]
GO
/****** Object: StoredProcedure [dbo].[usp_DaoRuAccess_YuanBan] Script Date: 2023/8/5 11:06:39 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER PROCEDURE [dbo].[usp_DaoRuAccess_YuanBan]
@Url_A nvarchar(800),
@Path_A nvarchar(800)=0,
@Tb_A nvarchar(800),
@Sheet_A nvarchar(800),
@InsertSQL_A nvarchar(1000)=0,
@ZhuanYun_1 nvarchar(800)=0,
@ZhuanYun_2 nvarchar(800)=0,
@ZhuanYun_3 nvarchar(800)=0
AS
BEGIN
SET NOCOUNT ON;
---#开启服务#---
exec sp_configure 'show advanced options',1
reconfigure
exec sp_configure 'Ad Hoc Distributed Queries',1
reconfigure

---#拼接Access路径与传入参数#---
set @Path_A = '''Microsoft.ACE.OLEDB.12.0'',''Data Source=' + cast(@URL_A as varchar(800)) + ';User ID=Admin;Password='''
set @InsertSQL_A =N'SELECT * into ##'+@Tb_A+' FROM OpenDataSource('+@Path_A+')...['+@Sheet_A+']WHERE 始发转运中心='+@ZhuanYun_1+' or 始发转运中心='+@ZhuanYun_2+' or 始发转运中心='+@ZhuanYun_3
exec (@InsertSQL_A)

---#关闭服务#---
exec sp_configure 'Ad Hoc Distributed Queries',0
reconfigure
exec sp_configure 'show advanced options',0
reconfigure

END

---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

Access文件导入数据 批量导入

---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

USE [Usp]
GO
/****** Object: StoredProcedure [dbo].[usp_DaoRuAccess_DongBeiDaQu] Script Date: 2023/8/5 11:05:46 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================

ALTER PROCEDURE [dbo].[usp_DaoRuAccess_DongBeiDaQu]
@Url_A nvarchar(800),
@DLQ_A_LN nvarchar(800),
@DLQ_A_JL nvarchar(800),
@DLQ_A_HLJ nvarchar(800),
@Day_A nvarchar(800),
@Path_A_LN nvarchar(800)=0,
@Path_A_JL nvarchar(800)=0,
@Path_A_HLJ nvarchar(800)=0,
@Sheet_A nvarchar(800),
@InsertSQL_A nvarchar(2000)=0

AS
BEGIN
SET NOCOUNT ON;
---#开启服务#---
exec sp_configure 'show advanced options',1
reconfigure
exec sp_configure 'Ad Hoc Distributed Queries',1
reconfigure

---#拼接Access路径与传入参数#---
set @Path_A_LN = '''Microsoft.ACE.OLEDB.12.0'',''Data Source=' + cast(@URL_A as varchar(800)) + @DLQ_A_LN + @Day_A + '.accdb;User ID=Admin;Password='''
set @Path_A_JL = '''Microsoft.ACE.OLEDB.12.0'',''Data Source=' + cast(@URL_A as varchar(800)) + @DLQ_A_JL + @Day_A + '.accdb;User ID=Admin;Password='''
set @Path_A_HLJ = '''Microsoft.ACE.OLEDB.12.0'',''Data Source=' + cast(@URL_A as varchar(800)) + @DLQ_A_HLJ + @Day_A + '.accdb;User ID=Admin;Password='''

 


set @InsertSQL_A =N'

SELECT DISTINCT
CONVERT(CHAR(10),签收时间,120) AS 日期,
(CASE
WHEN 运单号 LIKE ''55%'' THEN ''七星潭''
WHEN 运单号 LIKE ''JT3%'' THEN ''七星潭''
WHEN 运单号 LIKE ''JT5%'' THEN ''桃花岛''
WHEN 运单号 LIKE ''JT290%'' THEN ''紫金山''
WHEN 运单号 LIKE ''JT291%'' THEN ''紫金山''
WHEN 运单号 LIKE ''JT292%'' THEN ''紫金山''
WHEN 运单号 LIKE ''JT293%'' THEN ''紫金山''
WHEN 运单号 LIKE ''JT294%'' THEN ''紫金山''
WHEN 运单号 LIKE ''JT295%'' THEN ''逍遥峰''
WHEN 运单号 LIKE ''JT296%'' THEN ''逍遥峰''
WHEN 运单号 LIKE ''JT297%'' THEN ''逍遥峰''
WHEN 运单号 LIKE ''JT298%'' THEN ''逍遥峰''
WHEN 运单号 LIKE ''JT299%'' THEN ''逍遥峰''
ELSE ''其他'' END) AS 订单来源,
(寄件代理区+''-''+签收代理区) AS 代理区对,
(始发转运中心+''-''+目的转运中心) AS 中心对,
(寄件市+''-''+签收市) AS 城市对,
(寄件市+寄件区县+''-''+签收市+签收区县) AS 区县对,
DATEDIFF(DD,ISNULL(揽收时间,入仓时间) ,签收时间) AS 揽签日期差,
* ,
ISNULL((始发转运中心+干线任务单号),'''') AS 始发干线辅助列,
ISNULL(
( CASE
WHEN (目的转运中心+到车任务编号) LIKE ''%ZX%'' THEN ''''
ELSE (目的转运中心+到车任务编号) END
),'''') AS 目的干线辅助列,
ISNULL(
(CASE
WHEN 到车任务编号 LIKE ''%ZX%'' THEN ''支线''
WHEN 到车任务编号 LIKE ''%GX%'' THEN ''干线''
WHEN 到车任务编号 LIKE ''DBGX%'' THEN ''干线''
WHEN 到车任务编号 LIKE ''DKGX%'' THEN ''干线''
WHEN 到车任务编号 LIKE ''WFGX%'' THEN ''干线''
WHEN 到车任务编号 LIKE ''DKDD%'' THEN ''干线''
WHEN 到车任务编号 LIKE ''JBGX%'' THEN ''干线''
WHEN 到车任务编号 LIKE ''DBDD%'' THEN ''干线''
WHEN 到车任务编号 LIKE ''WFDD%'' THEN ''干线''
WHEN 到车任务编号 LIKE ''JBDD%'' THEN ''干线''
ELSE ''其他'' END ), '''') AS 到车任务判断

INTO ##ShiXiaoMingXi

FROM (

SELECT * FROM OpenDataSource('+@Path_A_LN+')...['+@Sheet_A+']
union all
SELECT * FROM OpenDataSource('+@Path_A_JL+')...['+@Sheet_A+']
union all
SELECT * FROM OpenDataSource('+@Path_A_HLJ+')...['+@Sheet_A+'] ) a
'
exec (@InsertSQL_A)

 

---#关闭服务#---
exec sp_configure 'Ad Hoc Distributed Queries',0
reconfigure
exec sp_configure 'show advanced options',0
reconfigure


END

---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

Excel文件导入数据

---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

USE [Usp]
GO
/****** Object: StoredProcedure [dbo].[usp_DaoRuExcel] Script Date: 2023/8/5 11:11:02 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER PROCEDURE [dbo].[usp_DaoRuExcel]
@Url nvarchar(800),
@Path nvarchar(800)=0,
@Tb nvarchar(800),
@Sheet nvarchar(800),
@InsertSQL nvarchar(800)=0
AS
BEGIN
SET NOCOUNT ON;
---#开启服务#---
exec sp_configure 'show advanced options',1
reconfigure
exec sp_configure 'Ad Hoc Distributed Queries',1
reconfigure

---#拼接Excel路径与传入参数#---
set @Path = '''Microsoft.ACE.OLEDB.12.0'',''Data Source=' + cast(@URL as varchar(800)) + ';Extended Properties=Excel 12.0'''
set @InsertSQL =N'SELECT * into ##'+@Tb+' FROM OpenDataSource('+@Path+')...['+@Sheet+'$]'
exec (@InsertSQL)

---#关闭服务#---
exec sp_configure 'Ad Hoc Distributed Queries',0
reconfigure
exec sp_configure 'show advanced options',0
reconfigure


END

---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

Excel文件导入数据 批量导入

---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

USE [Usp]
GO
/****** Object: StoredProcedure [dbo].[usp_PiLiangDaoRuExcel] Script Date: 2023/8/5 11:11:54 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO

ALTER PROCEDURE [dbo].[usp_PiLiangDaoRuExcel]

 

@SQL nvarchar(4000)=0,
@Zzz_ZhongZhuan nvarchar(1000)=0,
@Url nvarchar(1000), ----------文件路径
@Name nvarchar(1000), ----------文件名(不包含尾部数字)
@Start int, ----------起始表
@Num int , ----------循环次数
@Tb nvarchar(1000)=0, ----------全局临时表名称
@Sheet nvarchar(1000) ----------Excel的Sheet页名称

AS
BEGIN
SET NOCOUNT ON;

---#开启服务#---
exec sp_configure 'show advanced options',1
reconfigure
exec sp_configure 'Ad Hoc Distributed Queries',1
reconfigure

set @Zzz_ZhongZhuan = '''Microsoft.ACE.OLEDB.12.0'',''Data Source="'+ cast(@Url as varchar(1000))+ cast(@Name as varchar(1000)) + cast(@Start as varchar(1000)) + ').xlsx";
Extended properties=Excel 12.0'''

set @SQL = N'SELECT * into ##'+@Tb+' FROM OpenDataSource('+@Zzz_ZhongZhuan+')...['+@Sheet+'$]'
exec sp_executesql @SQL

Set @Start = @Start + 1

While @Start <= @Num
begin
---导入表格数据
set @Zzz_ZhongZhuan = '''Microsoft.ACE.OLEDB.12.0'',''Data Source="'+ cast(@Url as varchar(1000))+ cast(@Name as varchar(1000)) + cast(@Start as varchar(1000)) + ').xlsx";
Extended properties=Excel 12.0'''

set @SQL = 'insert into ##'+@Tb+' SELECT * FROM OpenDataSource('+@Zzz_ZhongZhuan+')...['+@Sheet+'$]'

exec sp_executesql @SQL

Set @Start = @Start + 1
End

---#关闭服务#---
exec sp_configure 'Ad Hoc Distributed Queries',0
reconfigure
exec sp_configure 'show advanced options',0
reconfigure

 

END

---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

 

---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

标签:存储,LIKE,sp,WHEN,Server,Sql,800,nvarchar,configure
From: https://www.cnblogs.com/-Tiko/p/17607675.html

相关文章

  • MySQL内存使用排查
    MySQL使用内存上升90%!在运维过程中50%的几率,会碰到这样的问题。算是比较普遍的现象。MySQL内存使用率过高,有诸多原因。普遍原因是使用不当,还有MySQL本身缺陷导致的。到底是哪方面的问题,那就需要一个一个进行排查。下面介绍排查思路:1.参数配置需要确认,内存是否设置合理MySQL内......
  • vmware workstation pro17 安装 windows server 2022
    本文实验所需环境vmwareworkstationpro17windowserver2022镜像文件:zh-cn_windows_server_2022_updated_april_2023_x64_dvd_644d5669.iso镜像文件下载:NEXT,ITELLYOU一、vmware创建windowserver2022虚拟机安装步骤,打开vmwareworkstationpro17,新建虚拟机,选......
  • mysql 命令安装
    1.  mysql 下载安装好压缩文件,下面我们进入正题,少废话。09:39:112023-08-05先到mysql官方网站下载:https://dev.mysql.com/downloads/mysql/ 解压后为这样 执行这个代码mysqld--initialize--console后面出现密码自己保存一下  然后继续安装m......
  • SQL SERVER数据库日志清理
    新建查询,选择master数据库USE[master]GOALTERDATABASEblogSETRECOVERYSIMPLEWITHNO_WAIT----blog为需要清除日志的数据库名称GOALTERDATABASEblogSETRECOVERYSIMPLE--简单模式----blog为需要清除日志的数据库名称GOUSEblo......
  • CentOS7 systemctl 管理MySQL服务
    #在/usr/lib/systemd/system/路径下创建mysql.service]#vim/usr/lib/systemd/system/mysql.service[Unit]Description=MySQLserverAfter=syslog.targetnetwork.target[Service]User=mysqlGroup=mysqlType=forkingTimeoutSec=0#PermissionsStartOnly=trueExecStar......
  • sql 中延迟执行
    SQL有定时执行的语句WaitFor。语法格式:waitfor{delay'time'|time'time'}delay后面的时间是需要延迟多长时间后执行。time后面的时间是指定何时执行,格式为'HH:MM:SS',不支持日期例:``--指定10:00执行Beginwaitfortime'10:00'select*fromtablenameend--指定等待1......
  • sql注入发现
    1、异常YouhaveanerrorinyourSQLsyntax;checkthemanualthatcorrespondstoyourMySQLserverversionfortherightsyntaxtousenear''1'''atline1一般适用于查询输出的场景,而对于盲注场景和其它sql语句类型,基本不会输出sql异常。......
  • ERP-SQL查询
    --查询垫布的颜色/尺码--工作单物料需求-物料需求明细selectm.OrderNo,m.MatrClass,m.MatrCode,m.Color,m.Sizxfrommrjmdtlmwherem.MatrClass='DB'intodsResult1;--查询出料单中已出库且无单价的物料信息--出料单(每一笔出库数据)/物料名称/收货方/出库金额=0--......
  • 常见的 NoSQL 数据库有哪些?
    前言今天我们来介绍一下工作开发中常见的一些NoSQL数据库及其基本特点。欢迎在评论区留下文章中没有介绍且好用的​NOSQL数据库......
  • sqlfluff sql linter 以及自动格式化工具
    sqlfluffsqllinter以及自动格式化工具,支持多数据库方言,以及模版代码,比较适合ELT类型的数据分析应用,同时对于dbt也是比较友好的特性多种数据库方言支持支持模版(jinja,dbt)可以很好的集成ci/cddbt参考效果因为dbt使用了jinja模版引擎,同时sqlfluff提供了dbt的模版包......