------------- Check database if already exists then drop ------------------- Show
IF EXISTS (SELECT [name] FROM sys.databases WHERE [name] = 'db_Product' ) ------------- Create Database------------- CREATE DATABASE db_Product -------------Switch database ------------- ------------ Check table if already exists then drop ------------------- IF EXISTS (SELECT [name] FROM sys.tables WHERE [name] = 'tbl_Product' ) ------------ Create tbl_Product table ---------------------------------- ----------------Insert data into tbl_Product table------------------------- INSERT INTO tbl_Product VALUES (1, 'Mobile', 5200, 15); INSERT INTO tbl_Product VALUES (2, 'Laptop', 25452, 25); INSERT INTO tbl_Product VALUES (3, 'Desktop', 18451, 8); ----------------Count total inseted data -------------------------------- SELECT COUNT(*) AS 'TotalRecords' FROM tbl_Product This section provides an introduction to SQL Command Line (SQL*Plus), an interactive and batch command-line query tool that is installed with Oracle Database Express Edition. Overview of SQL Command LineSQL Command Line (SQL*Plus) is a command-line tool for accessing Oracle Database XE. It enables you to enter and run SQL, PL/SQL, and SQL*Plus commands and statements to:
You can use SQL Command Line to generate reports interactively, to generate reports as batch processes, and to write the results to a text file, to a screen, or to an HTML file for browsing on the Internet. This section describes SQL Command Line (SQL*Plus), a command-line utility to run SQL and PL/SQL. This contains the following topics: Note: Before starting SQL Command Line, make sure that the necessary environmental variables have been set up properly. See for information about setting environmental variables for SQL Command Line.Starting and Exiting SQL Command LineTo start SQL Command Line from the operating-system command prompt, enter the following:
When prompted, enter the username and password of the user account (schema) that you want to access in the local database. For example, enter You can also include the username and password when you start SQL Command Line. For example:
If you want to connect to a database running on a remote system, you need to include a connect string when starting SQL Command Line. For example:
-- prompt for a column name, such as job_id, which is then substituted in the -- remaining identical substitution variables prefixed with && SELECT employee_id, last_name, &&column_name FROM employees ORDER BY &&column_name;0 -- prompt for a column name, such as job_id, which is then substituted in the -- remaining identical substitution variables prefixed with && SELECT employee_id, last_name, &&column_name FROM employees ORDER BY &&column_name;1 After you have started SQL Command Line, the -- prompt for a column name, such as job_id, which is then substituted in the -- remaining identical substitution variables prefixed with && SELECT employee_id, last_name, &&column_name FROM employees ORDER BY &&column_name;2 prompt displays as follows: -- prompt for a column name, such as job_id, which is then substituted in the -- remaining identical substitution variables prefixed with && SELECT employee_id, last_name, &&column_name FROM employees ORDER BY &&column_name;2 At the -- prompt for a column name, such as job_id, which is then substituted in the -- remaining identical substitution variables prefixed with && SELECT employee_id, last_name, &&column_name FROM employees ORDER BY &&column_name;2 prompt, you can enter SQL statements. When you want to exit SQL Command Line, enter -- prompt for a column name, such as job_id, which is then substituted in the -- remaining identical substitution variables prefixed with && SELECT employee_id, last_name, &&column_name FROM employees ORDER BY &&column_name;5 at the SQL prompt, as follows: -- prompt for a column name, such as job_id, which is then substituted in the -- remaining identical substitution variables prefixed with && SELECT employee_id, last_name, &&column_name FROM employees ORDER BY &&column_name;6 Displaying Help With SQL Command LineTo display a list of Help topics for SQL Command Line, enter -- prompt for a column name, such as job_id, which is then substituted in the -- remaining identical substitution variables prefixed with && SELECT employee_id, last_name, &&column_name FROM employees ORDER BY &&column_name;7 -- prompt for a column name, such as job_id, which is then substituted in the -- remaining identical substitution variables prefixed with && SELECT employee_id, last_name, &&column_name FROM employees ORDER BY &&column_name;8 at the SQL prompt as follows: -- prompt for a column name, such as job_id, which is then substituted in the -- remaining identical substitution variables prefixed with && SELECT employee_id, last_name, &&column_name FROM employees ORDER BY &&column_name;9 From the list of SQL Command Line Help topics, you can display Help about an individual topic by entering -- prompt for a column name, such as job_id, which is then substituted in the -- remaining identical substitution variables prefixed with && SELECT employee_id, last_name, &&column_name FROM employees ORDER BY &&column_name;7 with a topic name. For example, the following displays Help about the SQL Command Line -- define a variable value for a query as follows DEFINE job_id = "ST_CLERK" -- run a query using the defined value for job_id (ST_CLERK) SELECT employee_id, last_name FROM employees WHERE job_id = '&job_id';1 command, which enables you to format column output: -- define a variable value for a query as follows DEFINE job_id = "ST_CLERK" -- run a query using the defined value for job_id (ST_CLERK) SELECT employee_id, last_name FROM employees WHERE job_id = '&job_id';2 Entering and Executing SQL Statements and CommandsTo enter and execute SQL statements or commands, enter the statement or command at the SQL prompt. At the end of a SQL statement, put a semi-colon (;) and then press the Enter key to execute the statement. For example: -- define a variable value for a query as follows DEFINE job_id = "ST_CLERK" -- run a query using the defined value for job_id (ST_CLERK) SELECT employee_id, last_name FROM employees WHERE job_id = '&job_id';3 If the statement does not fit on one line, enter the first line and press the Enter key. Continue entering lines, and terminate the last line with a semi-colon (;). For example: -- define a variable value for a query as follows DEFINE job_id = "ST_CLERK" -- run a query using the defined value for job_id (ST_CLERK) SELECT employee_id, last_name FROM employees WHERE job_id = '&job_id';4 -- define a variable value for a query as follows DEFINE job_id = "ST_CLERK" -- run a query using the defined value for job_id (ST_CLERK) SELECT employee_id, last_name FROM employees WHERE job_id = '&job_id';5 -- define a variable value for a query as follows DEFINE job_id = "ST_CLERK" -- run a query using the defined value for job_id (ST_CLERK) SELECT employee_id, last_name FROM employees WHERE job_id = '&job_id';6 The output from the previous -- define a variable value for a query as follows DEFINE job_id = "ST_CLERK" -- run a query using the defined value for job_id (ST_CLERK) SELECT employee_id, last_name FROM employees WHERE job_id = '&job_id';7 statement is similar to: -- define a variable value for a query as follows DEFINE job_id = "ST_CLERK" -- run a query using the defined value for job_id (ST_CLERK) SELECT employee_id, last_name FROM employees WHERE job_id = '&job_id';8 -- define a variable value for a query as follows DEFINE job_id = "ST_CLERK" -- run a query using the defined value for job_id (ST_CLERK) SELECT employee_id, last_name FROM employees WHERE job_id = '&job_id';9 sqlplus 0sqlplus 1sqlplus 2sqlplus 3sqlplus 4sqlplus 5sqlplus 6Note that a terminating semi-colon (;) is optional with SQL Command Line commands, such as SQL Command Line DESCRIBE CommandSQL Command Line provides the HR 2HR 3HR 4HR 5HR 6HR 7HR 8HR 9my_hr_password 0my_hr_password 1my_hr_password 2my_hr_password 3my_hr_password 4my_hr_password 5SQL Command Line SET CommandsThe SQL Command Line -- define a variable value for a query as follows DEFINE job_id = "ST_CLERK" -- run a query using the defined value for job_id (ST_CLERK) SELECT employee_id, last_name FROM employees WHERE job_id = '&job_id';7 statements. For example, the following sqlplus 8 commands specify the number of lines for each page and the number of characters for each line in the output:my_hr_password 9sqlplus hr/ 0To enable output from PL/SQL blocks with
To view all the settings, enter the following at the SQL prompt:
For information about the SQL Command Line See Also: for information about setting up the SQL Command Line environment with a login fileRunning Scripts From SQL Command LineYou can use a text editor to create SQL Command Line script files that contain SQL*Plus, SQL, and PL/SQL statements. For consistency, use the A SQL script file is executed with a -- prompt for a column name, such as job_id, which is then substituted in the -- remaining identical substitution variables prefixed with && SELECT employee_id, last_name, &&column_name FROM employees ORDER BY &&column_name;0 command. For example, in a Windows environment, you can execute a SQL script as follows:
A SQL script file can be executed in a Linux environment as follows:
You can use When running a script, you need to include the full path name unless the script is located in the directory from which SQL Command Line was started, or the script is located in the default script location specified by the See Also:
Spooling From SQL Command LineThe To start spooling the output to an operating system file, you enter the
If you want to append the output to an existing file:
To stop spooling and close a file, enter the following:
Using Variables With SQL Command LineYou can create queries that use variables to make -- define a variable value for a query as follows DEFINE job_id = "ST_CLERK" -- run a query using the defined value for job_id (ST_CLERK) SELECT employee_id, last_name FROM employees WHERE job_id = '&job_id';7 statements more flexible. You can define the variable before running a SQL statement, or you specify that the statement prompts for a variable value at the time that the SQL statement is run. When using a variable in a SQL statement, the variable name must be begin with an ampersand (&). This section contains the following topics: For information about using bind variables in PL/SQL code, see . Prompting for a Variable Value in a QueryYou can use Example A-1 Prompting for a Variable Value in SQL Command Line -- prompt for employee_id in a query, you need to enter a valid ID such as 125 SELECT employee_id, last_name, job_id FROM employees WHERE employee_id = &employee_id; When you run the previous -- define a variable value for a query as follows DEFINE job_id = "ST_CLERK" -- run a query using the defined value for job_id (ST_CLERK) SELECT employee_id, last_name FROM employees WHERE job_id = '&job_id';7 statement, the output is similar to: my_hr_password 2my_hr_password 3... my_hr_password 4my_hr_password 5my_hr_password 6Reusing a Variable Value in a QueryYou can use Example A-2 Reusing a Variable Value in SQL Command Line -- prompt for a column name, such as job_id, which is then substituted in the -- remaining identical substitution variables prefixed with && SELECT employee_id, last_name, &&column_name FROM employees ORDER BY &&column_name; Defining a Variable Value for a QueryIn , the -- prompt for a column name, such as job_id, which is then substituted in the -- remaining identical substitution variables prefixed with && SELECT employee_id, last_name, &&column_name FROM employees ORDER BY &&column_name;00 variable is defined before running the SQL statement with the -- prompt for a column name, such as job_id, which is then substituted in the -- remaining identical substitution variables prefixed with && SELECT employee_id, last_name, &&column_name FROM employees ORDER BY &&column_name;01 command, and the defined value is substituted for the variable when the statement is executed. Because the variable has already been defined, you are not prompted to enter a value. How to run SQL script from cmd?Run the script file. Open a command prompt window.. In the Command Prompt window, type: sqlcmd -S myServer\instanceName -i C:\myScript.sql.. Press ENTER.. How to run MySQL script from command line?use the MySQL command line client: mysql -h hostname -u user database < path/to/test. sql. Install the MySQL GUI tools and open your SQL file, then execute it. Use phpmysql if the database is available via your webserver.
How do I run a .SQL script?Click Query > Connection > Connect to connect to the server that contains the database you want to access. Select the appropriate StarTeam Server database. Open the tuning script, by choosing File > Open > foldername\scriptname. Execute the script, by clicking the Execute button on the toolbar or by pressing F5.
How to run SQL script from command line Linux?Create a sample database. On your Linux machine, open a bash terminal session.. Use sqlcmd to run a Transact-SQL CREATE DATABASE command. Bash Copy. /opt/mssql-tools/bin/sqlcmd -S localhost -U SA -Q 'CREATE DATABASE SampleDB'. Verify the database is created by listing the databases on your server. Bash Copy.. |