国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 數據庫 > SQL Server > 正文

SQL Script tips for MS SQL Server

2024-08-31 00:51:00
字體:
來源:轉載
供稿:網友

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'


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 卓尼县| 延边| 曲阳县| 山东| 尤溪县| 乌拉特前旗| 融水| 嘉禾县| 扎赉特旗| 宝清县| 嘉鱼县| 股票| 兴国县| 广汉市| 东兰县| 永平县| 黑水县| 乌什县| 山丹县| 新津县| 鹿邑县| 罗源县| 临洮县| 驻马店市| 闻喜县| 文山县| 辽阳市| 西充县| 苏尼特左旗| 平凉市| 肃南| 景宁| 正阳县| 如皋市| 调兵山市| 乾安县| 高唐县| 桓台县| 蒙自县| 拉孜县| 务川|