PRINT (Transact-SQL)PRINT (Transact-SQL)
適用対象:Applies to: SQL ServerSQL Server (サポートされているすべてのバージョン)
SQL ServerSQL Server (all supported versions)
Azure SQL データベースAzure SQL Database
Azure SQL データベースAzure SQL Database
Azure SQL Managed InstanceAzure SQL Managed Instance
Azure SQL Managed InstanceAzure SQL Managed Instance
Azure Synapse AnalyticsAzure Synapse Analytics
Azure Synapse AnalyticsAzure Synapse Analytics
Parallel Data WarehouseParallel Data Warehouse
Parallel Data WarehouseParallel Data Warehouse
SQL ServerSQL Server (サポートされているすべてのバージョン)
SQL ServerSQL Server (all supported versions)
Azure SQL データベースAzure SQL Database
Azure SQL データベースAzure SQL Database
Azure SQL Managed InstanceAzure SQL Managed Instance
Azure SQL Managed InstanceAzure SQL Managed Instance
Azure Synapse AnalyticsAzure Synapse Analytics
Azure Synapse AnalyticsAzure Synapse Analytics
Parallel Data WarehouseParallel Data Warehouse
Parallel Data WarehouseParallel Data Warehouse
ユーザー定義メッセージをクライアントに返します。Returns a user-defined message to the client.
Transact-SQL 構文表記規則
Transact-SQL Syntax Conventions
構文Syntax
PRINT msg_str | @local_variable | string_expr
注意
SQL Server 2014 以前の Transact-SQL 構文を確認するには、以前のバージョンのドキュメントを参照してください。To view Transact-SQL syntax for SQL Server 2014 and earlier, see Previous versions documentation.
引数Arguments
msg_strmsg_str
文字列または Unicode 文字列の定数です。Is a character string or Unicode string constant. 詳細については、「定数 (Transact-SQL)」を参照してください。For more information, see Constants (Transact-SQL).
@ local_variable@ local_variable
任意の有効な文字型の変数を指定します。Is a variable of any valid character data type. @ local_variable は、char、nchar、varchar、または nvarchar であるか、これらのデータ型に暗黙的に変換できる必要があります。@local_variable must be char, nchar, varchar, or nvarchar, or it must be able to be implicitly converted to those data types.
string_exprstring_expr
文字列を返す式を指定します。Is an expression that returns a string. 連結したリテラル値、関数、および変数を含むことができます。Can include concatenated literal values, functions, and variables. 詳細については、「式 (Transact-SQL)」を参照してください。For more information, see Expressions (Transact-SQL).
注釈Remarks
メッセージ文字列は、Unicode 以外の文字列の場合は 8,000 バイトまで指定でき、Unicode 文字列の場合は 4,000 バイトまで指定できます。A message string can be up to 8,000 characters long if it is a non-Unicode string, and 4,000 characters long if it is a Unicode string. これより長い文字列は切り詰められます。Longer strings are truncated. Varchar (max) と nvarchar (max) データ型は、varchar(8000) および nvarchar(4000) ではなくなったデータ型に切り詰められます。The varchar(max) and nvarchar(max) data types are truncated to data types that are no larger than varchar(8000) and nvarchar(4000).
RAISERROR を使用してメッセージを返すこともできます。RAISERROR can also be used to return messages. RAISERROR には、PRINT にはない次のような利点があります。RAISERROR has these advantages over PRINT:
RAISERROR は、エラー メッセージ文字列への引数の置き換えをサポートします。この置き換えでは、C 言語標準ライブラリの printf 関数をモデルにしたメカニズムを使用します。RAISERROR supports substituting arguments into an error message string using a mechanism modeled on the printf function of the C language standard library.
RAISERROR では、テキスト メッセージに加えて、一意のエラー番号、重大度、および状態コードを指定できます。RAISERROR can specify a unique error number, a severity, and a state code in addition to the text message.
RAISERROR は、sp_addmessage システム ストアド プロシージャを使用して作成したユーザー定義メッセージを返すために使用できます。RAISERROR can be used to return user-defined messages created using the sp_addmessage system stored procedure.
例Examples
A.A. 条件付きで PRINT を実行する (IF EXISTS)Conditionally executing print (IF EXISTS)
次の例では、PRINT
ステートメントを使用して条件に応じてメッセージを返します。The following example uses the PRINT
statement to conditionally return a message.
IF @@OPTIONS & 512 <> 0
PRINT N'This user has SET NOCOUNT turned ON.';
ELSE
PRINT N'This user has SET NOCOUNT turned OFF.';
GO
B.B. 文字列を構築して表示するBuilding and displaying a string
次の例では、GETDATE
関数の結果を nvarchar
データ型に変換し、リテラル テキストと連結して PRINT
で返します。The following example converts the results of the GETDATE
function to a nvarchar
data type and concatenates it with literal text to be returned by PRINT
.
-- Build the message text by concatenating
-- strings and expressions.
PRINT N'This message was printed on '
+ RTRIM(CAST(GETDATE() AS NVARCHAR(30)))
+ N'.';
GO
-- This example shows building the message text
-- in a variable and then passing it to PRINT.
-- This was required in SQL Server 7.0 or earlier.
DECLARE @PrintMessage NVARCHAR(50);
SET @PrintMessage = N'This message was printed on '
+ RTRIM(CAST(GETDATE() AS NVARCHAR(30)))
+ N'.';
PRINT @PrintMessage;
GO
例: Azure Synapse AnalyticsAzure Synapse Analytics、Parallel Data WarehouseParallel Data WarehouseExamples: Azure Synapse AnalyticsAzure Synapse Analytics and Parallel Data WarehouseParallel Data Warehouse
C.C. 条件付きで PRINT を実行するConditionally executing print
次の例では、PRINT
ステートメントを使用して条件に応じてメッセージを返します。The following example uses the PRINT
statement to conditionally return a message.
IF DB_ID() = 1
PRINT N'The current database is master.';
ELSE
PRINT N'The current database is not master.';
GO
参照See Also
データ型 (Transact-SQL) Data Types (Transact-SQL)
DECLARE @local_variable (Transact-SQL) DECLARE @local_variable (Transact-SQL)
RAISERROR (Transact-SQL)RAISERROR (Transact-SQL)