通过Sql Server创建EXCEL文件:master..xp_cmdshell

获取当前的一些需要的配置的值

打开配置

查询结果,生成Excel

发送数据

关闭选项

存储过程完整代码

USE HS_Suggestions_Complaints_System

GO

/****** Object: StoredProcedure dbo.CreateExcelReportAndSendSP Script Date: 11/13/2024 08:23:15 ******/

SET ANSI_NULLS ON

GO

SET QUOTED_IDENTIFIER ON

GO

-- =============================================

-- Author: <Author,,Name>

-- Create date: <Create Date,,>

-- Description: <Description,,>

-- =============================================

ALTER PROCEDURE dbo.CreateExcelReportAndSendSP

AS

BEGIN

-- SET NOCOUNT ON added to prevent extra result sets from

-- interfering with SELECT statements.

SET NOCOUNT ON;

-- Insert statements for procedure here

declare @sao as int,

@xpcmd as int

SELECT @sao=convert(int,value) FROM sys.configurations where name='show advanced options' ORDER BY name

SELECT@xpcmd=convert(int,value) FROM sys.configurations where name = 'xp_cmdshell' ORDER BY name

select@sao

select@xpcmd

if@sao=0

begin

EXECsp_configure 'show advanced options', 1

RECONFIGURE

end

if@xpcmd=0

begin

EXECsp_configure 'xp_cmdshell', 1

RECONFIGURE

end

DECLARE@ReturnValue INT;

DECLARE@Infor AS NVARCHAR(MAX);

EXEC@ReturnValue=master..xp_cmdshell 'bcp "Select Top 1 N''Id''as Id,N''Title''as Title,N''Fcmy''as Fcmy,N''My''as My,N''Yb''as Yb,N''Bmy''as Bmy,N''Fcbmy''as Fcbmy,N''Create_date''as Create_date,N''Record_date''as Record_date,N''Create_by''as Create_by FROM HS_Suggestions_Complaints_System.dbo.Canteen_questions

Union all SELECT Convert(varchar,Id),Title,Convert(varchar,Fcmy),Convert(varchar,My),Convert(varchar,Yb),Convert(varchar,Bmy),Convert(varchar,Fcbmy),Convert(nvarchar(100),Create_date,23)as Create_date,Convert(nvarchar(100),Record_date,23)as Record_date,Create_by FROM HS_Suggestions_Complaints_System.dbo.Canteen_questions" queryout "D:\HS_Suggestions_Complaints_System\HS_Suggestions_Complaints_System\ReportAttachment\Canteen_questions.csv" -w -S"localhost" -U"sa" -P"GDhs3225966"'

if@ReturnValue =0

begin

Set@Infor='创建EXCEL执行成功。'

EXEC@ReturnValue = dbo.SendWeChatAttachment @FilePath = N'D:\HS_Suggestions_Complaints_System\HS_Suggestions_Complaints_System\ReportAttachment\Canteen_questions.csv',@Infor = @Infor OUTPUT

IF@Infor = '{"Message":"出现错误。"}' Or @ReturnValue=1 --判断调用存储过程是否成功;

begin

Set@Infor='调用存储过程失败。'

end

end

else

begin

Set @Infor='创建EXCEL执行失败。'

end

EXECsp_configure 'xp_cmdshell', 0

RECONFIGURE

EXECsp_configure 'show advanced options', 0

RECONFIGURE

END

sql 复制代码
USE [HS_Suggestions_Complaints_System]
GO
/****** Object:  StoredProcedure [dbo].[CreateExcelReportAndSendSP]    Script Date: 11/13/2024 08:23:15 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author:		<Author,,Name>
-- Create date: <Create Date,,>
-- Description:	<Description,,>
-- =============================================
ALTER PROCEDURE [dbo].[CreateExcelReportAndSendSP] 
AS
BEGIN
	-- SET NOCOUNT ON added to prevent extra result sets from
	-- interfering with SELECT statements.
	SET NOCOUNT ON;

       -- Insert statements for procedure here
	declare @sao   as int,
	        @xpcmd as int
	SELECT @sao=convert(int,value) FROM sys.configurations where name='show advanced options' ORDER BY name
	SELECT @xpcmd=convert(int,value) FROM sys.configurations where name = 'xp_cmdshell' ORDER BY name
	select @sao
	select @xpcmd
	if @sao=0
	   begin
	    EXEC sp_configure 'show advanced options', 1
	    RECONFIGURE
	   end
	if @xpcmd=0
	   begin
	    EXEC sp_configure 'xp_cmdshell', 1
	    RECONFIGURE
	   end
	DECLARE @ReturnValue INT;
	DECLARE @Infor AS NVARCHAR(MAX);
	EXEC @ReturnValue=master..xp_cmdshell 'bcp "Select Top 1 N''Id''as Id,N''Title''as Title,N''Fcmy''as Fcmy,N''My''as My,N''Yb''as Yb,N''Bmy''as Bmy,N''Fcbmy''as Fcbmy,N''Create_date''as Create_date,N''Record_date''as Record_date,N''Create_by''as Create_by FROM HS_Suggestions_Complaints_System.dbo.Canteen_questions 
	Union all SELECT Convert(varchar,Id),Title,Convert(varchar,Fcmy),Convert(varchar,My),Convert(varchar,Yb),Convert(varchar,Bmy),Convert(varchar,Fcbmy),Convert(nvarchar(100),Create_date,23)as Create_date,Convert(nvarchar(100),Record_date,23)as Record_date,Create_by FROM HS_Suggestions_Complaints_System.dbo.Canteen_questions" queryout "D:\HS_Suggestions_Complaints_System\HS_Suggestions_Complaints_System\ReportAttachment\Canteen_questions.csv" -w -S"localhost" -U"sa" -P"GDhs3225966"'
	if @ReturnValue =0
	   begin
	     Set @Infor='创建EXCEL执行成功。'
	     EXEC @ReturnValue = [dbo].[SendWeChatAttachment] @FilePath = N'D:\HS_Suggestions_Complaints_System\HS_Suggestions_Complaints_System\ReportAttachment\Canteen_questions.csv',@Infor = @Infor OUTPUT
	     IF @Infor = '{"Message":"出现错误。"}' Or @ReturnValue=1 --判断调用存储过程是否成功;
	        begin
	          Set @Infor='调用存储过程失败。'
	        end
	   end
	else
	   begin
	     Set @Infor='创建EXCEL执行失败。'
	   end
	EXEC sp_configure 'xp_cmdshell', 0
	RECONFIGURE
	EXEC sp_configure 'show advanced options', 0
	RECONFIGURE
END
相关推荐
zxcvb1539 分钟前
运维实践里把备份mysql数据库做稳的一些思路
运维·数据库·mysql
优选资讯2 小时前
合格证套打怎么落地:Luck标签软件把 Excel 字段、编号和打印预览一次做顺
excel·标签打印·luck标签软件
Logic1012 小时前
Excel定义名称2,跨表汇总求最值
excel·indirect·sumif·excel函数·定义名称·跨表查询
一级新生2 小时前
Dify 迭代节点:从输入输出到九步实操
数据库·人工智能·mysql·低代码·工作流·dify
夜雪一千3 小时前
PostgreSQL 中哪些操作可以写成自定义函数
数据库·postgresql
科力锐品牌君4 小时前
信创进入核心系统深水区:X86 与国产架构长期并存,如何落地统一灾备?
运维·网络·数据库·数据安全·灾备
在角落发呆4 小时前
sqlserver 如何备份数据库:从手动备份到自动定时、异地容灾的完整方案
数据库·windows·备份·数据备份
xixiaoyunya5 小时前
MySQL 全库备份:用 mysqldump 打好基线,再补上自动化与异地能力
数据库·mysql·自动化
你看我名字5 小时前
OpenAI 兼容接口能不能直接接入:数眼智能大模型 API 迁移实战与兼容性检查
android·java·数据库·大模型平台选择
箓维6 小时前
MySQL_数据库基础与库表操作
数据库·mysql·oracle