APP_NAME (Transact-SQL)APP_NAME (Transact-SQL)

適用於: 是SQL Server 是Azure SQL Database 否Azure Synapse Analytics (SQL DW) 否平行處理資料倉儲 APPLIES TO: yesSQL Server yesAzure SQL Database noAzure Synapse Analytics (SQL DW) noParallel Data Warehouse

此函式會傳回目前工作階段的應用程式名稱 (如果應用程式設定該名稱值)。This function returns the application name for the current session, if the application sets that name value.

重要

用戶端提供應用程式名稱,且 APP_NAME 未以任何方式驗證應用程式名稱。The client provides the application name, and APP_NAME does not verify the application name value in any way. 請勿在安全性檢查的任何環節中使用 APP_NAMEDo not use APP_NAME as part of a security check.

主題連結圖示 Transact-SQL 語法慣例Topic link icon Transact-SQL Syntax Conventions

語法Syntax

  
APP_NAME  ( )  

傳回類型Return Types

nvarchar(128)nvarchar(128)

RemarksRemarks

使用 APP_NAME 來區別不同的應用程式,作為針對那些應用程式執行不同動作的方法。Use APP_NAME to distinguish between different applications, as a way to perform different actions for those applications. 例如,APP_NAME 可以區別不同的應用程式,以允許每個應用程式使用不同的日期格式。For example, APP_NAME can distinguish between different applications, which allows for a different date format for each application. 它也可允許傳回到特定應用程式的參考資訊。It can also allow for the return of an informational message to certain applications.

若要在 Management StudioManagement Studio 中設定應用程式名稱,請在 [連線到資料庫引擎] 對話方塊中按一下 [選項] 。To set an application name in Management StudioManagement Studio, click Options in the Connect to Database Engine dialog box. 在 [Additional Connection Parameters] (其他連線參數) 索引標籤中,以 ;app='application_name' 格式提供 app 屬性On the Additional Connection Parameters tab, provide an app attribute in the format ;app='application_name'

範例Example

此範例會檢查起始這個處理序的用戶端應用程式是否為 SQL Server Management Studio 工作階段。This example checks whether the client application that initiated this process is a SQL Server Management Studio session. 然後,它會提供 US 或 ANSI 格式的日期值。It then provides a date value in either US or ANSI format.

USE AdventureWorks2012;  
GO  
IF APP_NAME() = 'Microsoft SQL Server Management Studio - Query'  
PRINT 'This process was started by ' + APP_NAME() + '. The date is ' + CONVERT ( varchar(100) , GETDATE(), 101) + '.';  
ELSE   
PRINT 'This process was started by ' + APP_NAME() + '. The date is ' + CONVERT ( varchar(100) , GETDATE(), 102) + '.';  
GO  

另請參閱See also

系統函數 (Transact-SQL)System Functions (Transact-SQL)
函數Functions