首 页 | 精品电影 | 音乐天堂 | 在线游戏 | Flash MTV | 三湘书屋 | 幽默笑话 | 三湘图库 | 美女写真 | IT知识库 | QQ贴图 | 加入书签

网页制作网络编程图形图象操作系统冲浪宝典软件教学网络安全认证考试通信技术电子商务业内动态书籍教程原码

最近更新 文章分类 多媒体类 精品软件

本站搜索:
您的位置:三湘时空 -> IT知识库 -> 文章分类 -> 数据库技巧 -> SQL Script tips for MS SQL Server
SQL Script tips for MS SQL Server


文章类别:数据库技巧 来源: 作者: 发表日期:2006-4-19 字体:[ ]

小游戏 | 在线影院 | 幽默笑话 | 源码下载 | Flash MTV | 音乐试听 | 书屋 | 美女写真

This posting will show you some script tips about MS SQL Server.
1. Waitfor
The WAITFOR statement is specified with one of two clauses:
(1) The DELAY keyword followed by an amount of time to pass before completing the WAITFOR statement. The time to wait before completing the WAITFOR statement can be up to 24 hours. For example,
-- Wait for ten secondes before perforing a select statement
WAITFOR DELAY '00:00:10'
Select EmployeeID From Northwind.dbo.Employees

(2) The TIME keyword followed by a time to execute, which specifies completion of the WAITFOR statement.
For example,
-- Wait until 10:00 PM to perform a check of the pubs database to make sure that all pages are correctly allocalted and used.
Use pubs
BEGIN
 WAITFOR TIME '22:00'
 DBCC CHECKALLOC
END

2. Enable SQL Debugging
-- The SP_SDIDEBUG stored procedure is used by SQL Server for debugging Transact-SQL statements
Use master
Grant Execute on SP_SDIDEBUG to Username

3. Execute a dynamically built string
(1) EXECUTE statement
With the EXECUTE statement, all parameter values must be converted to character or Unicode and made a part of Transact-SQL string. For example,
DECLARE @IntVariable INT
DECLARE @SQLString NVARCHAR(500)
/* Build and execute a string with one parameter value. */
SET @IntVariable = 35
SET @SQLString = N'SELECT * FROM pubs.dbo.employee WHERE job_lvl = ' +
                 CAST(@IntVariable AS NVARCHAR(10))
EXEC(@SQLString)
/* Build and execute a string with a second parameter value. */
SET @IntVariable = 201
SET @SQLString = N'SELECT * FROM pubs.dbo.employee WHERE job_lvl = ' +
                 CAST(@IntVariable AS NVARCHAR(10))
EXEC(@SQLString)

(2) SP_ExecuteSQL
Using sp_executesql is recommended over using the EXECUTE statement to execute a string. Not only does the support for parameter substitution make sp_executesql more versatile than EXECUTE, it also makes sp_executesql more efficient because it generates execution plans that are more likely to be reused by SQL Server.
sp_executesql supports the setting of parameter values separately from the Transact-SQL string:

DECLARE @IntVariable INT
DECLARE @SQLString NVARCHAR(500)
DECLARE @ParmDefinition NVARCHAR(500)

/* Build the SQL string once. */
SET @SQLString =
     N'SELECT * FROM pubs.dbo.employee WHERE job_lvl = @level'
/* Specify the parameter format once. */
SET @ParmDefinition = N'@level tinyint'

/* Execute the string with the first parameter value. */
SET @IntVariable = 35
EXECUTE sp_executesql @SQLString, @ParmDefinition,
                      @level = @IntVariable
/* Execute the same string with the second parameter value. */
SET @IntVariable = 32
EXECUTE sp_executesql @SQLString, @ParmDefinition,
                      @level = @IntVariable

4. SP_HelpText
Prints the text of a rule, a default, or an unencrypted stored procedure, user-defined function, trigger, or view.
-- This example displays the text of the employee_insupd trigger, which is in the pubs database
Use Pubs
Exec sp_helptext 'employee_insupd'

上一篇:如何将SQL Server表驻留内存和检测 下一篇:五种提高SQL性能的方法
本栏目热门文章
·Windows XP下安装SQL2000企业版 2005-10-4
·在SQL Server 2000里设置和使用数据库复制 2005-11-13
·SQL Server 数据库管理常用的SQL和T-SQL语句 2005-11-13
·SQL SERVER实用技巧 2005-11-3
·配置SQL Server 2000选项 2005-10-8
·如何使Microsoft SQL Server的日志文件不会增大? 2005-10-4
·SQL中通配符、转义符与"["号的使用(downmoon) 2005-11-3
·Sql Server下数据库链接的使用方法 2005-11-21
·使用SQL Server导入和索引 Microsoft Word 文 2005-10-4
·如何快速杀死占用过多资源(CPU,内存)的数据库进程 2005-11-13
新近更新文章
·Oracle:一条SQL实现将多行数据并为一行显示 2006-6-20
·Link Server的语法 2006-6-20
·通过查询分析器对比SQL语句的执行效率 2006-6-15
·问题“未于信任SQL Server连接相关联”的解决 2006-6-15
·数据库死锁导致网站站点访问不了之解决方案 2006-6-15
·在Access中模拟sql server存储过程翻页 2006-6-12
·SQL 存储过程&算法 2006-6-12
·SQL Server 2005 Express附加(Attach)的 2006-6-9
·在SQL Server 2005中实现表的行列转换 2006-6-6
·枚举SQL Server的实例 2006-6-6
首 页 | 软件发布 | 广告联系 | 下载帮助 | 意见反馈 | 网站地图
  CopyRight? 2002-2004 WWW.SXSKY.NET? All Rights Reserved
三湘时空 站长QQ:82675303 Email: