Search
 
SCRIPT & CODE EXAMPLE
 

SQL

how to print sql query

# 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


Comment

PREVIOUS NEXT
Code Example
Sql :: sql create table with data 
Sql :: SQL Avoid Duplicates in INSERT INTO SELECT 
Sql :: reset keys in sql 
Sql :: Using GROUP BY in MySQL Join Table 
Sql :: sql alchemy or 
Sql :: select only one row sql 
Sql :: sql ending with vowels 
Sql :: sqlite insert if not exists 
Sql :: sql practice 
Sql :: sql online 
Sql :: timestamp difference sql 
Sql :: oracle enable chain 
Sql :: mysql timezone 
Sql :: oracle sql average 
Sql :: sqlalchemy bulk delete 
Sql :: restart mysql 
Sql :: sqlite commands 
Sql :: mysql command 
Sql :: sql rename table 
Sql :: oracle privileges users 
Sql :: union all query in sql 
Sql :: json not contains mysql 
Sql :: sql distinct 
Sql :: oracle get foreign keys on table 
Sql :: set column width in sqlplus 
Sql :: how to execute MySQL Stored Procedure in Python 
Sql :: mysql dump 
Sql :: postgres copy command 
Sql :: pgadmin postgres ERROR: database is being accessed by other users 
Sql :: mysql pad zeros 
ADD CONTENT
Topic
Content
Source link
Name
7+9 =