通过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
相关推荐
Pniubi13 分钟前
MySQL靠什么实现事务的?事务隔离级别有哪些?
java·数据库·mysql
数据库小学妹40 分钟前
pgbouncer 事务池:prepared statement、临时表与会话状态
数据库·后端·postgresql
仓库日记2 小时前
Excel进销存何时该换?用3项检查判断是否需要软件
经验分享·excel·进销存·开单软件
Lucifer三思而后行2 小时前
最近折腾了一下金仓数据库的备份恢复
数据库·备份恢复
用户0332126663673 小时前
如何使用 Python 将 JSON 转换为 Excel
python·excel
草莓熊Lotso3 小时前
【C++ 基于微服务的即时通讯系统】文件存储子服务实战:UUID生成、文件IO封装与全接口单元测试
开发语言·网络·数据库·c++·微服务·架构·单元测试
进击的荆棘4 小时前
MySQL——库的操作
数据库·mysql
2601_967980114 小时前
“小程序·云开发”上线 助力开发者的高效利器
数据库·存储管理·小程序云开发·云函数·开发者工具
zhangjw344 小时前
1-9 SELECT 查询基础:where、group by、having、order by、limit offset
数据库·postgresql·pg数据库
唐老板5 小时前
Postgres 慢查询,先取证再动手
数据库·ai编程