Dear readers of our blog, we'd like to recommend you to visit the main page of our website, where you can learn about our product SQLS*Plus and its advantages.
 
SQLS*Plus - best SQL Server command line reporting and automation tool! SQLS*Plus is several orders of magnitude better than SQL Server sqlcmd and osql command line tools.
 

REQUEST COMPLIMENTARY SQLS*PLUS LICENCE

Enteros UpBeat offers a patented database performance management SaaS platform. It proactively identifies root causes of complex revenue-impacting database performance issues across a growing number of RDBMS, NoSQL, and deep/machine learning database platforms. We support Oracle, SQL Server, IBM DB2, MongoDB, Casandra, MySQL, Amazon Aurora, and other database systems.

Chapter 3: Passing parameters as script arguments and Use of variables

2 July 2020

Chapter 3: Passing parameters as script arguments and Use of variables

SQLS*Plus: Passing parameters as script arguments

You can skip the hints for values that are associated with the substitution of variables by giving them values of parameters in the script, typing the command START / @ command.

The ampersand (&) in the script is placed together with the following figure to replace the substitution variable. At all script executions, the value “&<N>” after @filename will be replaced with the corresponding command-line argument.

Use of variables

& and && indicate substitution variables in SQLS*Plus scripts or commands

When SQLS*Plus encounters a variable defined with &&, it prompts you for the value and then uses this value for every subsequent occurrence of that variable it encounters. The variable and its value are stored.

When you define a variable with &, however, SQLS*Plus discards the variable and its value immediately after use, so that repeated use of &<variablename> results in repeated prompts for the value of <variablename>.

Bind variables

Bind variables are variables created in SQLS*Plus and then used in T-SQL or SQL.

Bind variables can be displayed in SQLS*Plus or referenced in T-SQL subprograms that run in SQLS*Plus.

Creating bind variables

Bind variables created in SQLS*Plus with the VARIABLE command. For example:

VARIABLE v_table_name VARCHAR(50) –s “MY_TABLE”

This command creates a bind variable named v_table_name with a data type of VARCHAR and initial value of “MY_TABLE”.

For more information, see the VARIABLE command. (To list session bind variables, type VARIABLE without arguments.)

Referencing bind variables

Bind variables in T-SQL referenced by typing a colon (:) followed immediately by the name of
the variable. For example

SET @Table_Name = :v_table_name;
0:sa@192.168.1.160\SQLSERVER2008> var name varchar(20) -s "This is a variable"
0:sa@192.168.1.160\SQLSERVER2008> /
begin
DECLARE @Name VARCHAR(20)
SET @Name = :name
print @Name
end

This is a variable

Displaying bind variables

To display the value of a bind variable in SQLS*Plus, use the SQLS*Plus PRINTVAR command. For example:

PRINTVAR name
0:sa@192.168.1.160\SQLSERVER2008> PRINTVAR name
:name
------------------
This is a variable

Setting bind variables values directly

To set the value of a bind variable directly in SQLS*Plus, use the SQLS*Plus SETVAR command. For example:

0:sa@192.168.1.160\SQLSERVER2008> SETVAR name "NEW_ORDERS"
0:sa@192.168.1.160\SQLSERVER2008> PRINTVAR name
:name
----------
NEW_ORDERS

Using bind variables values in non-SQL/TSQL report elements

Bind variable can be used on TTITLE and BTITLE. For example:

0:sa@192.168.1.160\SQLSERVER2008> setvar v3 @@servername;
0:sa@192.168.1.160\SQLSERVER2008>
0:sa@192.168.1.160\SQLSERVER2008> var v2 varchar(10) -s "Title Header"
0:sa@192.168.1.160\SQLSERVER2008>
0:sa@192.168.1.160\SQLSERVER2008> ttitle ':v2 :v3'

0:sa@192.168.1.160\SQLSERVER2008> select top 5 name from sysobjects;

'Title Header ADMIN-PC\SQLSERVER2008
name
--------------------------------------------------------------------------------
sysrscols
sysrowsets
sysallocunits
sysfiles1
syspriorities
0:sa@192.168.1.160\SQLSERVER2008> var

Currently defined bind variables:

var length value
-------------------------------------------------
:v2 10 Title Header
:v3 22 ADMIN-PC\SQLSERVER2008

Assigning SQL Server global variables to bind variables

SQL Server global variable value can be assigned to bind variable duing the time of creation of later using VARIABLE and SETVAR commands. For Example:

VARIABLE v3 varchar(40) –s @@ servername

or

SETVAR v3 @@servername;

Define Variables

Define variables contain either pre-defined value, such as database use or connection string or can be set by user manually or programmatically using COLUMNS and NEW_VALUE option of the columns.

Defining and manually assigning values to define variables

DEFINE Variable = 'value’

Example:

DEFINE LastName = 'Jackson’

Programmatically assigning values to define variables:

1) Define variable <define>

2) Define column with new_value <define>

3) Select data into column from table

Example:

DEFINE LName = 'Jackson’

COLUMN LastName new_value LName

select ‘Olson’ LastName;

Pre-defined variables

_CONNECT_IDENTIFIER
Connection identifier used to make connection.

_CONNECT_DATABASE
Database used to make connection, where available.

_DATE
Current date in default system format

_EDITOR
Editor used by the EDIT command.

_LANGUAGE
Language set in database (as “select @@language”)

_LOGON
Database or OS logon user name used to make connection.

_PRIVILEGE
Privilege level of the current connection (SYSADMIN or not)

_S_EDITION
Database edition of the connected SQL Server Database

_S_VERSION
Version of the connected SQL Server Database.

_S_LEVEL
Level of the connected SQL Server Database.

_USER
Database schema name used to make connection.

Use of define variables in SQLS*Plus command prompt

Define variables can be used to customize SQLS*Plus command prompt.

Example:

0:sa@192.168.1.160\SQLSERVER2008> set sqlprompt
"_USER@_CONNECT_DATABASE>"

sa@AdventureWorksLT2008>set sqlprompt reset

0:sa@192.168.1.160\SQLSERVER2008>


Download SQLS*Plus manual in PDF format


 
Tags: , , , , ,

MORE NEWS

 

Preamble​​NoSql is not a replacement for SQL databases but is a valid alternative for many situations where standard SQL is not the best approach for...

Preamble​​MongoDB Conditional operators specify a condition to which the value of the document field shall correspond.Comparison Query Operators $eq...

5 Database management trends impacting database administrationIn the realm of database management systems, moreover half (52%) of your competitors feel...

The data type is defined as the type of data that any column or variable can store in MS SQL Server. What is the data type? When you create any table or...

Preamble​​MS SQL Server is a client-server architecture. MS SQL Server process starts with the client application sending a query.SQL Server accepts,...

First the basics: what is the master/slave?One database server (“master”) responds and can do anything. A lot of other database servers store copies of all...

Preamble​​Atom Hopper (based on Apache Abdera) for those who may not know is an open-source project sponsored by Rackspace. Today we will figure out how to...

Preamble​​MongoDB recently introduced its new aggregation structure. This structure provides a simpler solution for calculating aggregated values rather...

FlexibilityOne of the most advertised features of MongoDB is its flexibility.  Flexibility, however, is a double-edged sword. More flexibility means more...

Preamble​​SQLShell is a cross-platform command-line tool for SQL, similar to psql for PostgreSQL or MySQL command-line tool for MySQL.Why use it?If you...

Preamble​​Writing an application on top of the framework on top of the driver on top of the database is a bit like a game on the phone: you say “insert...

Preamble​​Oracle Coherence is a distributed cache that is functionally comparable with Memcached. In addition to the basic function of the API cache, it...

Preamble​​IBM pureXML, a proprietary XML database built on a relational mechanism (designed for puns) that offers both relational ( SQL / XML ) and...

  What is PostgreSQL array? In PostgreSQL we can define a column as an array of valid data types. The data type can be built-in, custom or enumerated....

Preamble​​If you are a Linux sysadmin or developer, there comes a time when you need to manage an Oracle database that can work in your environment.In this...

Preamble​​Starting with Microsoft SQL Server 2008, by default, the group of local administrators is no longer added to SQL Server administrators during the...