cara mencetak kueri SQL

# The SQL PRINT statement serves to display the user-defined message. For example, you are developing a script with T-SQL Loops, and you want to display some specific message on each iteration of a loop. Then you can use the PRINT statement. Or, you can use it when developing a script with conditional statements. In that case, you ensure that the condition evaluated by the statement performs the correct step or generates the correct output. The PRINT statement can also be used to track the process of a T-SQL query or a stored procedure or to return the customized message.

The current article focuses on the following use cases:

1. Printing a string or int value using the PRINT Statement.
2. Using PRINT in the IF…ELSE statement.
3. Using PRINT in the WHILE Loop.

The syntax of the PRINT statement is the following:

Print string | @variable | str_expression

# string: The value can be a character or Unicode string.
# @variable: The character data type must be character or Unicode character data types.
# str_expression: the value can be an expression return a string. It can be a literal value, string function, and variable.

# When we use the PRINT statement to display the specific value, the output returns in the SQL Server Management studio’s messages pane.

Note: The PRINT statement can display 8000 characters long string or 4000 characters long Unicode string. If the length exceeds 8000, the remaining string will be truncated.

# Limitations of the PRINT statement:

1. The PRINT function returns a character string or UNICODE character string. In other words, when we concatenate the string and Integer data types, we must explicitly convert the INT value to char or varchar data type.

2. The SQL Server profiler does not capture PRINT statements.

 # Example: Print the String value

Suppose you want to write a statement that prints Hello, World to the screen. The T-SQL should be as follows:

Print 'Hello World'

# To print the value stored in the @string variable, we need the following code:

declare @String varchar(30)
set @String='Hello World'
Print @String

# Print an Integer value using the PRINT statement:

# Use the following T-SQL script:

Print 10

# To print the value of the @intvalue parameter, use the following T-SQL script. The data type of @intvalue is an integer.

declare @IntValue Int
set @IntValue = 10
Print @IntValue


Imaginathan