SSBI Trainings

MSSQL @@ROWCOUNT Tutorial

MSSQL @@ROWCOUNT Tutorial

The @@ROW COUNT variable returns the number of rows read by the last executed statement. If any statement does not return any rows, then value of @@ROWCOUNT variable is set to zero.

MSSQL @@ROWCOUNT VARIABLE SYNTAX

@@ROWCOUNT

Using @@ implies that it is a global variable. Also @@ROWCOUNT returns the value of int type i.e. the maximum no of rows @@ ROWCOUNT can return is 231 (2,147,483,647). For returning rows greater than this limit, ROWCOUNT_BIG function is used.

More Information visit: www.sqlschool.com

SQL GRANT REVOKE Commands

SQL GRANT REVOKE Commands

DCL commands are used to enforce database security in a multiple user database environment. Two types of DCL commands are GRANT and REVOKE. Only Database Administrator’s or owner’s of the database object can provide/remove privileges on a database object.

SQL GRANT Command

SQL GRANT is a command used to provide access or privileges on the database objects to the users.

The Syntax for the GRANT command is:

GRANT privilege_name
ON object_name
TO {user_name |PUBLIC |role_name}
[WITH GRANT OPTION];

  • privilege_name is the access right or privilege granted to the user. Some of the access rights are ALL, EXECUTE, and SELECT.
  • object_name is the name of an database object like TABLE, VIEW, STORED PROC and SEQUENCE.
  • user_name is the name of the user to whom an access right is being granted.
  • user_name is the name of the user to whom an access right is being granted.
  • PUBLIC is used to grant access rights to all users.
  • ROLES are a set of privileges grouped together.
  • WITH GRANT OPTION – allows a user to grant access rights to other users.

For Example:

GRANT SELECT ON employee TO user1;

This command grants a SELECT permission on employee table to user1.

You should use the WITH GRANT option carefully because for example if you GRANT SELECT privilege on employee table to user1 using the WITH GRANT option, then user1 can GRANT SELECT privilege on employee table to another user, such as user2 etc.

Later, if you REVOKE the SELECT privilege on employee from user1, still user2 will have SELECT privilege on employee table.

Create a Database by Script

SQL Server accepts Transact-SQL (which is an extended version of the SQL standard), so you could create the database by running the following SQL script.

USE master;
GO
CREATE DATABASE Music;
GO

To do this, open a new query by clicking New Query in the toolbar and run an SQL CREATE DATABASE statement.

Just as you can specify certain properties when creating a database via the GUI, you can include those same properties when creating a database by script. Here’s an example of specifying settings for the data and log files.

USE master ;
GO
CREATE DATABASE Music
ON
( NAME = Music_dat,
FILENAME = ‘C:\Program Files\Microsoft SQL Server\MSSQL13.MSSQLSERVER\MSSQL\DATA\Music.mdf’,
SIZE = 10,
MAXSIZE = 50,
FILEGROWTH = 5 )
LOG ON
( NAME = Music_log,
FILENAME = ‘C:\Program Files\Microsoft SQL Server\MSSQL13.MSSQLSERVER\MSSQL\DATA\Music_log.ldf’,
SIZE = 5MB,
MAXSIZE = 25MB,
FILEGROWTH = 5MB ) ;
GO

What is a NULL Value?

A field with a NULL value is a field with no value.

If a field in a table is optional, it is possible to insert a new record or update a record without adding a value to this field. Then, the field will be saved with a NULL value.

Note: It is very important to understand that a NULL value is different from a zero value or a field that contains spaces. A field with a NULL value is one that has been left blank during record creation!

What is the difference between various types of joins? For instance, there’s inner join, left join, right join, full join, cross join, natural join, equi-join, and semi-join?

Different types of joins are required depending on the type of data wrangling one needs to do. Let’s work with an example here. In this toy example, we have 2 tables-

  1. Married- This contains the name and social security number ( SSN) of the people who are married.
  2. Home Owners- This contains the name and social security number ( SSN) of the people who own a home

SSN is unique for each person and will be used as a primary key to merge two tables.

Let’s now look at the types of joins using the above 2 tables and which group they would provide the information on from both or either tables.

  1. Inner Join- If we want to find out people who are both married and home owners- Mia, Liz, Benjamin, Ava.
  2. Left Join- All married folks, whether or not home owners- John, James, Harper, Amy, Mia, Liz, Benjamin, Ava.
  3. Right Join– All home owners, whether or not married- Mia, Liz, Benjamin, Ava, Sophia, William, Jacob, Michael
  4. Full Join- Everyone from both tables- Mia, Liz, Benjamin, Ava, Sophia, William, Jacob, Michael, John, James, Harper, Amy.

left

Use keys from left frame only

right

Use keys from right frame only

outer

Use union of keys from both frames

inner

Use intersection of keys from both frames

How to Connect to SQL Server after Installation

This video explains on “How to Connect to SQL Server after Installation”

Applicable for SQL Server 2008, SQL Server 2008 R2, SQL Server 2012, SQL Server 2014 and SQL Server 2016

T0 resolve possible connection issues at basic level, ensure the following:

1. Verify and ensure correct server name is provided

2. Go to Run then services.msc then ensure SQL Server service for the respective instance is started.

Any clarifications: Please email to contact@sqlschool.com

For Real-time Trainings and Projects, Placements on SQL Server, please visit http://www.sqlschool.com

Or call us on +91 9666440801 (INDIA) or +1 510-400-4845 (USA/CANADA)

What is the difference between SQL Server and SQL?

SQL stands for Structured Query Language. SQL is used to communicate with a database. According to ANSI (American National Standards Institute), it is the standard language for relational database management systems. It is used to perform operations on the data present in database. SQL statements are used to perform tasks such as update data on a database, or retrieve data from a database. Although most database systems use SQL, most of them also have their own additional proprietary extensions that are usually only used on their system. However, the standard SQL commands such as “Select”, “Insert”, “Update”, “Delete”, “Create”, and “Drop” can be used to accomplish almost everything that one needs to do with a database. SQL commands are divided into several different types, among them data manipulation language (DML) and data definition language (DDL) statements, transaction controls and security measures.

Some common relational database management systems that use SQL are: Oracle, Sybase, Microsoft SQL Server, Access, Ingres, etc.

SQL Server is a relational database management system (RDBMS) developed by Microsoft. This product is built for the basic function of storing retrieving data as required by other applications. It can be run either on the same computer or on another across a network. It is platform dependent. It is both GUI and command based software.

SQL Server is available in various editions. Some of the editions are:

  • Enterprise
  • Standard
  • Workgroup
  • Web
  • Developer
  • Express
  • Compact
  • Datacenter
  • Business Intelligence
  • Enterprise Evaluation

So, different enterprises like web development companies, mobile app development companies, banks, outsourcing companies and many other uses RDBMS like (SQL server, Oracle) for smooth functioning of their business.

Use SQL Server Management Studio (SSMS) on Windows to manage SQL Server on Linux

This topic shows how to use SQL Server Management Studio (SSMS) to connect to SQL Server 2017 on Linux. SSMS is a Windows application, so use SSMS when you have a Windows machine that can connect to a remote SQL Server instance on Linux.

After successfully connecting, you run a simple Transact-SQL (T-SQL) query to verify communication with the database.

Install the newest version of SQL Server Management Studio

When working with SQL Server, you should always use the most recent version of SQL Server Management Studio (SSMS). The latest version of SSMS is continually updated and optimized and currently works with SQL Server 2017 on Linux. To download and install the latest version, see Download SQL Server Management Studio. To stay up-to-date, the latest version of SSMS prompts you when there is a new version available to download.

Connect to SQL Server on Linux

The following steps show how to connect to SQL Server 2017 on Linux with SSMS.

  1. Start SSMS by typing Microsoft SQL Server Management Studioin the Windows search box, and then click the desktop app.
    1. In the Connect to Serverwindow, enter the following information (if SSMS is already running, click Connect > Database Engine to open the Connect to Server window):

     

    Setting Description
    Server type The default is database engine; do not change this value.
    Server name Enter the name of the target Linux SQL Server machine or its IP address.
    Authentication For SQL Server 2017 on Linux, use SQL Server Authentication.
    Login Enter the name of a user with access to a database on the server (for example, the default SA account created during setup).
    Password Enter the password for the specified user (for the SA account, you created this during setup).

  2. Click Connect.
  3. After successfully connecting to your SQL Sever, Object Explorer opens and you can now access your database to perform administrative tasks or query data.

    Run sample queries

    After you connect to your server, you can connect to a database and run a sample query. If you are new to writing queries, see Writing Transact-SQL Statements.

    1. Identify a database to use to run a query against. This could be a new database you created in the Transact-SQL tutorial. Or it could be the AdventureWorks sample database that you downloaded and restored.
    2. In Object Explorer, navigate to the target database on the server.
    3. Right-click the database and then select New Query:

    4. n the query window, write a Transact-SQL query to select data from one of the tables. The following example selects data from the Production.Product table of the AdventureWorks database.

       SELECT TOP 10 Name, ProductNumber
       FROM Production.Product
       ORDER BY Name ASC
    5. Click the Execute button:

First Normal Form (1NF)

First Normal Form (1NF)
A database table is said to be in 1NF if it contains no repeating fields/columns.

• Make a separate table for each set of related attributes and give each table a primary key. In simple words we can say it as
• There are no repeating or duplicate fields.
• Each cell contains only a single value.
• Each record is unique and identified by primary key

Example:

ITEM COLORS PRICES TAX
Pen Red,Blue 3.0 0.40
Scale Red,Yellow 3.0 0.40
Pen Red,Blue 3.0 0.40
Bag Blue,Black 120.0 12.52

This table is not in first normal form because:

• A. There are multiple fields in color lab.

• B. Records are repeating (Duplicate records) or no primary key. First Normal Form (1NF)

ITEM COLORS PRICES TAX
Pen Red 3.0 0.40
Pen Blue 3.0 0.40
Scale Red 3.0 0.40
Scale Yellow 3.0 0.40
Bag Blue 120.0 12.52
Bag Black 120.0 12.52

This table is now in first normal form.

SQL Server next version CTP 1.4

Microsoft is excited to announce a new preview for the next version of SQL Server (SQL Server v.Next). Community Technology Preview (CTP) 1.4 is available on both Windows and Linux. In this preview, they added the ability to schedule jobs using SQL Server Agent on Linux. We can try the preview in your choice of development and test environments now: www.sqlserveronlinux.com.

Key CTP 1.4 enhancements
The primary enhancement to SQL Server v.Next on Linux in this release is the ability to schedule jobs using SQL Server Agent. This functionality helps administrators automate maintenance jobs and other tasks, or run them in response to an event. Some SQL Server Agent functionality is not yet enabled for SQL Server on Linux. To learn more and see sample SQL Server Agent jobs, you can read detailed blog titled “SQL Server on Linux: Running scheduled jobs with SQL Server Agent” or attend an Engineering Town Hall about “SQL Server Agent and Full Text Search in SQL Server on Linux.”

The mssql-server-linux container image on Docker Hub now includes the sqlcmd and bcp command line utilities to make it easier to create and attach databases and automate other actions when working with containers. For additional detail on CTP 1.4, please visit What’s New in SQL Server v.Next, Release Notes and Linux
documentation.

In addition, SQL Server Analysis Services and SQL Server Reporting Services developer tools now support Visual Studio 2017. They are available for installation from the Visual Studio Marketplace providing the option for automatic updates going forward.

 

Get SQL Server v.Next CTP 1.4 today!
Try the preview of the next release of SQL Server today! Get started with the preview of SQL Server with our developer tutorials that show you how to install and use SQL Server v.Next on macOS, Docker, Windows, and Linux and quickly build an app in a programming language of your choice.