This posting will show you some script tips about MS SQL Server. (2) The TIME keyword followed by a time to execute, which specifies completion of the WAITFOR statement. 2. Enable SQL Debugging 3. Execute a dynamically built string (2) SP_ExecuteSQL DECLARE @IntVariable INT /* Build the SQL string once. */ /* Execute the string with the first parameter value. */ 4. SP_HelpText
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
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
— The SP_SDIDEBUG stored procedure is used by SQL Server for debugging Transact-SQL statements
Use master
Grant Execute on SP_SDIDEBUG to Username
(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 = NSELECT * 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 = NSELECT * FROM pubs.dbo.employee WHERE job_lvl = +
CAST(@IntVariable AS NVARCHAR(10))
EXEC(@SQLString)
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 @SQLString NVARCHAR(500)
DECLARE @ParmDefinition NVARCHAR(500)
SET @SQLString =
NSELECT * FROM pubs.dbo.employee WHERE job_lvl = @level
/* Specify the parameter format once. */
SET @ParmDefinition = N@level tinyint
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
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 script tips for ms sql server_数据库技巧
版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com 特别注意:本站所有转载文章言论不代表本站观点! 本站所提供的图片等素材,版权归原作者所有,如需使用,请与原作者联系。未经允许不得转载:IDC资讯中心 » sql script tips for ms sql server_数据库技巧
相关推荐
-      sql语句中的判断功能的使用方法
-      sql语句中的判断功能的使用方法
-      SQL语言中去掉小数点有效数字后面的所有0
-      ASP连接各种数据库的代码
-      在sql语句中实现md5功能
-      给access数据库减肥
-      asp连接access数据库代码(2)
-      ASP连接access数据库代码