最新更新 sitemap 网站制作设计本站搜索
网页设计
国外网站 韩国网站 个人主页 手提袋设计 CSS 网页特效 平面设计 网站设计 Flash CMS技巧 服装网站 php教程 photoshop 画册 服务器选用 数据库 Office
虚拟主机 域名注册 云主机 网页设计 客服QQ:8208442
当前位置:首页 > 编程开发 > 数据库

SQLServer存储过程参数及例子

日期:08-01    来源:中国设计秀    作者:cnwebshow.com

Microsoft included several hundred stored procedures in the various versions of Microsoft SQL Server and it has documented a good percentage of them. But many stored procedures remain undocumented. Some are used within the Enterprise Manager GUI in SQL 2000 and were not intended to be used by other processes. Microsoft has slated some of these stored procedures to be removed (or they have been removed) from future versions of SQL Server. While these stored procedures can be very useful and save you lots of time, they can be changed at any time in their function or they can simply be removed.xFW中国设计秀

The chart below shows that while many of the procedures have been carried through from one version of Microsoft SQL Server to another, new stored procedures have been introduced, and some have been removed from the install package. Most, if not all, of the procedures require the user to be a member of the sysadmin fixed server role in order to execute the procedures. The stored procedures that interact with the file system also require that the user executing the procedure (as well as SQL Server's service account) have access to the file/folder.xFW中国设计秀

Procedure Name SQL 2000 SQL 2005 SQL 2008 xFW中国设计秀
sp_executeresultset X     xFW中国设计秀
sp_MSforeachdb X X X xFW中国设计秀
sp_MSforeachtable X X X xFW中国设计秀
sp_readerrorlog X X X xFW中国设计秀
xp_create_subdir   X X xFW中国设计秀
Xp_delete_file   X X xFW中国设计秀
xp_dirtree X X X xFW中国设计秀
xp_fileexist X X X xFW中国设计秀
xp_fixeddrives X X X xFW中国设计秀
xp_getfiledetails X     xFW中国设计秀
xp_getnetname X X X xFW中国设计秀
xp_loginconfig X X X xFW中国设计秀
xp_makecab X     xFW中国设计秀
xp_msver X X X xFW中国设计秀
xp_get_mapi_profiles X X X xFW中国设计秀
xp_subdirs X X X xFW中国设计秀
xp_test_mapi_profile X X X xFW中国设计秀
xp_unpackcab X    xFW中国设计秀

xFW中国设计秀
sp_executeresultsetxFW中国设计秀

Microsoft removed this handy little procedure called sp_executeresultset from SQL Server in SQL Server 2005. It allows you to generate dynamic SQL code on the fly by using a SELECT query. Then, the resulting SQL commands will be executed against the database. It permits you to create a single piece of code that can, in a single step, find the number of records in every table in your database (as the example shows). This is an undocumented stored procedure and there is no way of knowing why it was removed. But, alas, this handy utility is gone.xFW中国设计秀

exec sp_execresultset 'SELECT ''SELECT '''''' + name + '''''', xFW中国设计秀
count(*) FROM '' + namexFW中国设计秀
from sysobjectsxFW中国设计秀
where xtype = ''U'''xFW中国设计秀

sp_MSforeachdb / sp_MSforeachtablexFW中国设计秀

Two procedures, sp_MSforeachdb and sp_MSforeachtable, are wrappers around a cursor. They allow you to execute T-SQL code against each database on your SQL Server and each table within the current database, respectively. You cannot, however, use an sp_MSforeachtable command within an sp_MSforeachdb command in SQL 2000 and prior. The cursor name that was used within those procedures was the same (hCForEach) and would therefore return an error saying that the cursor name was already in use for each execution of the sp_MSforeachtable. In SQL Server 2005, Microsoft resolved this issue. In order to "next" the command, you must tell one of the procedures it will be using a different replacement character other than the default question mark. I change the replacement character in the database command because it's easier.xFW中国设计秀

Print each table name in the current database.xFW中国设计秀

exec sp_MSforeachtable 'print ''?'''xFW中国设计秀

Print each database on the current server.xFW中国设计秀

exec sp_MSforeachdb 'print ''?'''xFW中国设计秀

Print each table on the current server.xFW中国设计秀

exec sp_MSforeachdb 'use [@] exec sp_MSforeachtable ''printxFW中国设计秀
''''@.?''''''', '@'xFW中国设计秀

sp_readerrorlog / xp_readerrorlogxFW中国设计秀

The stored procedure sp_readerrorlog actually comes in two forms. Each works the same; one is simply a wrapper for the second. The wrapper stored procedure is sp_readerrorlog and it calls xp_readerrorlog. Both have four input parameters, but only the first two are useful to us. The first parameter establishes the file number that you wish to view. The second is the log to view (1 or null for ERRORLOG, 2 for SQL Agent Log). This allows you to view your error logs quickly and easily instead of having to look at the bloated log viewer that now comes with SQL Server 2005 and SQL 2008.xFW中国设计秀

View the current SQL ERRORLOG file.xFW中国设计秀

exec sp_readerrorlogxFW中国设计秀

exec sp_readerrorlog 0, 1xFW中国设计秀

View the Prior SQL Agent Log file.xFW中国设计秀

exec sp_readerrorlog 1, 2xFW中国设计秀

xp_create_subdirxFW中国设计秀

Introduced in SQL Server 2005, the xp_create_subdir stored procedure is very handy because you can use it to create folders on SQL Server's hard drive or on a network share from within T-SQL.xFW中国设计秀

exec xp_create_subdir 'c:MSSQLData'xFW中国设计秀

xp_delete_filexFW中国设计秀

Use the xp_delete_file stored procedure introduced in SQL Server 2005 to delete files from SQL Server's hard drive or a network share from within T-SQL.xFW中国设计秀

xp_dirtreexFW中国设计秀

The xp_dirtree procedure allows you to view the folder tree and/or file list beneath a folder. This procedure has several parameters that control how deep the procedure searches and whether it returns files and folders or folders only. The first parameter establishes the folder to look in. (Recommendation: Do not run this procedure against the root of the drive that Windows is installed on because it will take some time to generate the tree and return the data.) The second parameter limits the number of recursive levels that the procedure will dig through. The default is zero or all levels. The third parameter tells the procedure to include files. The default is zero or folders only, a value of 1 includes files in the result set. Specifying a third value not equal to zero will add an additional column to the output called file which is a bit field showing the entry in a folder or file.xFW中国设计秀

Get the full directory tree.xFW中国设计秀

exec xp_dirtree 'd:mssql'xFW中国设计秀

Get the first two levels of the directory tree.xFW中国设计秀

exec xp_dirtree 'd:mssql', 2xFW中国设计秀

Get the first three levels of the directory tree, including files.xFW中国设计秀

exec xp_dirtree 'd:mssql', 3, 1xFW中国设计秀

xp_fileexistxFW中国设计秀

This SQL Server stored procedure, xp_fileexist, is used to determine if a file exists on SQL Server's hard drive or on a network share. It is extremely useful in stored procedures that load data from flat files. It allows you to check and see if the file exists before attempting to blindly load the file. The procedure has two parameters. Use the first parameter to determine if the file or folder you want exists. The second is an output parameter, which when specified, returns a 1 or 0 if the file exists or does not.xFW中国设计秀

Without the parameter.xFW中国设计秀

exec xp_fileexist 'c:importfile.csv'xFW中国设计秀

With the parameter.xFW中国设计秀

DECLARE @file_exists intxFW中国设计秀
exec xp_fileexist 'c:importfile.csv', @file_exists OUTPUTxFW中国设计秀
SELECT @file_existsxFW中国设计秀

xFW中国设计秀
xp_fixeddrivesxFW中国设计秀

The procedure xp_fixeddrives is one of the most useful procedures. It presents a list of all drive letters and the amount of free space each drive has. The parameter has a single optional input parameter that can filter the results by drive type. A value of 3 will return all mass storage devices (CD-ROM, DVD, etc.); a value of 4 will return the hard drives; while a value of 2 will return removable media (USB thumb drives, flash drives, etc.).xFW中国设计秀

Return all drives.xFW中国设计秀

exec xp_fixeddrivesxFW中国设计秀

Return hard drives only.xFW中国设计秀

exec xp_fixeddrives 2xFW中国设计秀

xp_getfiledetailsxFW中国设计秀

The procedure xp_getfiledetails is another extremely useful procedure, which was last available in SQL Server 2000. This procedure returns size, date and attribute information about the file specified, including date and times created, accessed and modified.xFW中国设计秀

exec xp_getfiledetails 'c:filetoload.csv'xFW中国设计秀

xp_getnetnamexFW中国设计秀

The procedure xp_getnetname returns the name of the physical machine where Microsoft SQL Server is installed. You can have the machine name returned as a record set or as a variable.xFW中国设计秀

Without the parameter.xFW中国设计秀

exec xp_getnetnamexFW中国设计秀

Using the parameter.xFW中国设计秀

DECLARE @machinename sysnamexFW中国设计秀
exec xp_getnetname @machinename OUTPUTxFW中国设计秀
select @machinenamexFW中国设计秀

xFW中国设计秀
xp_loginconfigxFW中国设计秀

This SQL Server stored procedure will tell you some basic authentication information about the user executing it. It tells you the authentication method (Windows versus SQL Login), the default domain of the server, the audit level, as well as some internal separator information.xFW中国设计秀

exec xp_loginconfigxFW中国设计秀

xp_makecabxFW中国设计秀

Back in SQL Server 2000, Microsoft gave us the ability to compress OS files directly from T-SQL without having to shell out to DOS via xp_cmdshell and run third-party software, like pkzip or winzip. That command was xp_makecab. It allows you to specify a list of files you want to compress as well as the cab file you want to put them in. It even lets you select default compression, MSZIP compression (akin to the .zip file format) or no compression. The first parameter gives the path to the cab file in which you want to create or add files to. The second parameter is the compression level. The third parameter applies if you want to use verbose logging. Starting with the fourth parameter and on down are the names of the files you want to compress. In my testing, I was able to pass 45 file names to be compressed to the extended stored procedure, which means that it is a very flexible solution to your data compression requirements.xFW中国设计秀

exec xp_makecab 'c:test.cab', 'mszip', 1, 'c:test.txt' , 'c:test1.txt'xFW中国设计秀

xp_msver

本文引用地址:/bc/article_60171.html
网站地图 | 关于我们 | 联系我们 | 网站建设 | 广告服务 | 版权声明 | 免责声明