从T-SQL中创建Excel的XLS格式文件 T-SQL Excel XLS Create Excel XLS from T-SQLScript Rating Total number of votes [30] By: David A. Long This is a T-SQL script that uses OLE, ADO, Jet4 ISAM, and Linked Server to create and populate an Excel Wo
从T-SQL中创建Excel的XLS格式文件 T-SQL Excel XLSCreate Excel XLS from T-SQL Script Rating Total number of votes [30] By: David A. Long This is a T-SQL script that uses OLE, ADO, Jet4 ISAM, and Linked Server to create and populate an Excel Workbook (XLS) file from T-SQL query. If the Excel Worksheet exists, the query will append to the "table". The code is designed to be used by SQL Agent and to append to the step output with verbose and minimal detail. Code is pretty well commented, including some hard won knowledge about Jet4 ISAM, OLE, ADO, and usage of the Excel table from T-SQL -- Create XLS script DAL - 04/24/2003 -- -- Designed for Agent scheduling, turn on "Append output for step history" -- -- Search for %%% to find adjustable constants and other options -- -- Uses OLE for ADO and OLE DB to create the XLS file if it does not exist -- Linked server requires the XLS to exist before creation -- Uses OLE ADO to Create the XLS Worksheet for use as a table by T-SQL -- Uses Linked Server to allow T-SQL access to XLS table -- Uses T-SQL to populate te XLS worksheet, very fast -- PRINT 'Begin CreateXLS script at '+RTRIM(CONVERT(varchar(24),GETDATE(),121))+' ' PRINT '' GO SET NOCOUNT ON DECLARE @Conn int -- ADO Connection object to create XLS , @hr int -- OLE return value , @src varchar(255) -- OLE Error Source , @desc varchar(255) -- OLE Error Description , @Path varchar(255) -- Drive or UNC path for XLS , @Connect varchar(255) -- OLE DB Connection string for Jet 4 Excel ISAM , @WKS_Created bit -- Whether the XLS Worksheet exists , @WKS_Name varchar(128) -- Name of the XLS Worksheet (table) , @ServerName nvarchar(128) -- Linked Server name for XLS , @DDL varchar(8000) -- Jet4 DDL for the XLS WKS table creation , @SQL varchar(8000) -- INSERT INTO XLS T-SQL , @Recs int -- Number of records added to XLS , @Log bit -- Whether to log process detail -- Init variables SELECT @Recs = 0 -- %%% 1 = Verbose output detail, helps find problems, 0 = minimal output detail , @Log = 1 -- %%% assign the UNC or path and name for the XLS file, requires Read/Write access -- must be accessable from server via SQL Server service account -- & SQL Server Agent service account, if scheduled SET @Path = 'C:\TEMP\Test_'+CONVERT(varchar(10),GETDATE(),112)+'.xls' -- assign the ADO connection string for the XLS creation SET @Connect = 'Provider=Microsoft.Jet.OLEDB.4.0;Data Source='+@Path+';Extended Properties=Excel 8.0' -- %%% assign the Linked Server name for the XLS population SET @ServerName = 'EXCEL_TEST' -- %%% Rename Table as required, this will also be the XLS Worksheet name SET @WKS_Name = 'People' -- %%% Table creation DDL, uses Jet4 syntax, -- Text data type = varchar(255) when accessed from T-SQL SET @DDL = 'CREATE TABLE '+@WKS_Name+' (SSN Text, Name Text, Phone Text)' -- %%% T-SQL for table population, note the 4 part naming required by Jet4 OLE DB -- INSERT INTO SELECT, INSERT INTO valueS, and EXEC sp types are supported -- Linked Server does not support SELECT INTO types SET @SQL = 'INSERT INTO '+@ServerName+'...'+@WKS_Name+' (SSN, Name, Phone) ' SET @SQL = @SQL+'SELECT au_id AS SSN' SET @SQL = @SQL+', LTRIM(RTRIM(ISNULL(au_fname,'''')+'' ''+ISNULL(au_lname,''''))) AS Name' SET @SQL = @SQL+', phone AS Phone ' SET @SQL = @SQL+'FROM pubs.dbo.authors' IF @Log = 1 PRINT 'Created OLE ADODB.Connection object' -- Create the Conn object EXEC @hr = sp_OACreate 'ADODB.Connection', @Conn OUT IF @hr <> 0 -- have to use <> as OLE / ADO can return negative error numbers BEGIN -- Return OLE error EXEC sp_OAGetErrorInfo @Conn, @src OUT, @desc OUT SELECT Error=convert(varbinary(4),@hr), Source=@src, Description=@desc RETURN END IF @Log = 1 PRINT char(9)+'Assigned ConnectionString property' -- Set a the Conn object's ConnectionString property -- Work-around for error using a variable parameter on the Open method EXEC @hr = sp_OASetProperty @Conn, 'ConnectionString', @Connect IF @hr <> 0 BEGIN -- Return OLE error EXEC sp_OAGetErrorInfo @Conn, @src OUT, @desc OUT SELECT Error=convert(varbinary(4),@hr), Source=@src, Description=@desc RETURN END IF @Log = 1 PRINT char(9)+'Open Connection to XLS, for file Create or Append' -- Call the Open method to create the XLS if it does not exist, can't use parameters EXEC @hr = sp_OAMethod @Conn, 'Open' IF @hr <> 0 BEGIN -- Return OLE error EXEC sp_OAGetErrorInfo @Conn, @src OUT, @desc OUT SELECT Error=convert(varbinary(4),@hr), Source=@src, Description=@desc RETURN END -- %%% This section could be repeated for multiple Worksheets (Tables) IF @Log = 1 PRINT char(9)+'Execute DDL to create '''+@WKS_Name+''' worksheet' -- Call the Execute method to Create the work sheet with the @WKS_Name caption, -- which is also used as a Table reference in T-SQL -- Neat way to define column data types in Excel worksheet -- Sometimes converting to text is the only work-around for Excel's General -- Cell formatting, even though the Cell contains Text, Excel tries to format -- it in a "Smart" way, I have even had to use the single quote appended as the -- 1st character in T-SQL to force Excel to leave it alone EXEC @hr = sp_OAMethod @Conn, 'Execute', NULL, @DDL, NULL, 129 -- adCmdText + adExecuteNoRecords -- 0x80040E14 for table exists in ADO IF @hr = 0x80040E14 -- kludge, skip 0x80042732 for ADO Optional parameters (NULL) in SQL7 OR @hr = 0x80042732 BEGIN -- Trap these OLE Errors IF @hr = 0x80040E14 BEGIN PRINT char(9)+''''+@WKS_Name+''' Worksheet exists for append' SET @WKS_Created = 0 END SET @hr = 0 -- ignore these errors END IF @hr <> 0 BEGIN -- Return OLE error EXEC sp_OAGetErrorInfo @Conn, @src OUT, @desc OUT SELECT Error=convert(varbinary(4),@hr), Source=@src, Description=@desc RETURN END IF @Log = 1 PRINT 'Destroyed OLE ADODB.Connection object' -- Destroy the Conn object, +++ important to not leak memory +++ EXEC @hr = sp_OADestroy @Conn IF @hr <> 0 BEGIN -- Return OLE error EXEC sp_OAGetErrorInfo @Conn, @src OUT, @desc OUT SELECT Error=convert(varbinary(4),@hr), Source=@src, Description=@desc RETURN END -- Linked Server allows T-SQL to access the XLS worksheet (Table) -- This must be performed after the ADO stuff as the XLS must exist -- and contain the schema for the table, or worksheet IF NOT EXISTS(SELECT srvname from master.dbo.sysservers where srvname = @ServerName) BEGIN IF @Log = 1 PRINT 'Created Linked Server '''+@ServerName+''' and Login' EXEC sp_addlinkedserver @server = @ServerName , @srvproduct = 'Microsoft Excel Workbook' , @provider = 'Microsoft.Jet.OLEDB.4.0' , @datasrc = @Path , @provstr = 'Excel 8.0' -- no login name or password are required to connect to the Jet4 ISAM linked server EXEC sp_addlinkedsrvlogin @ServerName, 'false' END -- Have to EXEC the SQL, otherwise the SQL is evaluated -- for the linked server before it exists EXEC (@SQL) PRINT char(9)+'Populated '''+@WKS_Name+''' table with '+CONVERT(varchar,@@ROWCOUNT)+' Rows' -- %%% Optional you may leave the Linked Server for other XLS operations -- Remember that the Linked Server will not create the XLS, so remove it -- When you are done with it, especially if you delete or move the file IF EXISTS(SELECT srvname from master.dbo.sysservers where srvname = @ServerName) BEGIN IF @Log = 1 PRINT 'Deleted Linked Server '''+@ServerName+''' and Login' EXEC sp_dropserver @ServerName, 'droplogins' END GO SET NOCOUNT OFF PRINT '' PRINT 'Finished CreateXLS script at '+RTRIM(CONVERT(varchar(24),GETDATE(),121))+' ' GO

MySQL在数据库和编程中的地位非常重要,它是一个开源的关系型数据库管理系统,广泛应用于各种应用场景。1)MySQL提供高效的数据存储、组织和检索功能,支持Web、移动和企业级系统。2)它使用客户端-服务器架构,支持多种存储引擎和索引优化。3)基本用法包括创建表和插入数据,高级用法涉及多表JOIN和复杂查询。4)常见问题如SQL语法错误和性能问题可以通过EXPLAIN命令和慢查询日志调试。5)性能优化方法包括合理使用索引、优化查询和使用缓存,最佳实践包括使用事务和PreparedStatemen

MySQL适合小型和大型企业。1)小型企业可使用MySQL进行基本数据管理,如存储客户信息。2)大型企业可利用MySQL处理海量数据和复杂业务逻辑,优化查询性能和事务处理。

InnoDB通过Next-KeyLocking机制有效防止幻读。1)Next-KeyLocking结合行锁和间隙锁,锁定记录及其间隙,防止新记录插入。2)在实际应用中,通过优化查询和调整隔离级别,可以减少锁竞争,提高并发性能。

MySQL不是一门编程语言,但其查询语言SQL具备编程语言的特性:1.SQL支持条件判断、循环和变量操作;2.通过存储过程、触发器和函数,用户可以在数据库中执行复杂逻辑操作。

MySQL是一种开源的关系型数据库管理系统,主要用于快速、可靠地存储和检索数据。其工作原理包括客户端请求、查询解析、执行查询和返回结果。使用示例包括创建表、插入和查询数据,以及高级功能如JOIN操作。常见错误涉及SQL语法、数据类型和权限问题,优化建议包括使用索引、优化查询和分表分区。

MySQL是一个开源的关系型数据库管理系统,适用于数据存储、管理、查询和安全。1.它支持多种操作系统,广泛应用于Web应用等领域。2.通过客户端-服务器架构和不同存储引擎,MySQL高效处理数据。3.基本用法包括创建数据库和表,插入、查询和更新数据。4.高级用法涉及复杂查询和存储过程。5.常见错误可通过EXPLAIN语句调试。6.性能优化包括合理使用索引和优化查询语句。

选择MySQL的原因是其性能、可靠性、易用性和社区支持。1.MySQL提供高效的数据存储和检索功能,支持多种数据类型和高级查询操作。2.采用客户端-服务器架构和多种存储引擎,支持事务和查询优化。3.易于使用,支持多种操作系统和编程语言。4.拥有强大的社区支持,提供丰富的资源和解决方案。

InnoDB的锁机制包括共享锁、排他锁、意向锁、记录锁、间隙锁和下一个键锁。1.共享锁允许事务读取数据而不阻止其他事务读取。2.排他锁阻止其他事务读取和修改数据。3.意向锁优化锁效率。4.记录锁锁定索引记录。5.间隙锁锁定索引记录间隙。6.下一个键锁是记录锁和间隙锁的组合,确保数据一致性。


热AI工具

Undresser.AI Undress
人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover
用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool
免费脱衣服图片

Clothoff.io
AI脱衣机

AI Hentai Generator
免费生成ai无尽的。

热门文章

热工具

WebStorm Mac版
好用的JavaScript开发工具

禅工作室 13.0.1
功能强大的PHP集成开发环境

DVWA
Damn Vulnerable Web App (DVWA) 是一个PHP/MySQL的Web应用程序,非常容易受到攻击。它的主要目标是成为安全专业人员在合法环境中测试自己的技能和工具的辅助工具,帮助Web开发人员更好地理解保护Web应用程序的过程,并帮助教师/学生在课堂环境中教授/学习Web应用程序安全。DVWA的目标是通过简单直接的界面练习一些最常见的Web漏洞,难度各不相同。请注意,该软件中

Atom编辑器mac版下载
最流行的的开源编辑器

Dreamweaver CS6
视觉化网页开发工具