Tag: database

GOTO Label in SQL Server

There are times when you want to jump to a specific section (Label) in the code when a condition satisfies. You can create a label and then use the GOTO statement to branch directly to the code. To create a label , simply specify the label name followed by the colon. Label1: Here’s a query demonstrating the usage of the Label named “RecordExistsLabel” and GOTO…

NULLIF function in SQL Server

The NULLIF function in SQL Server returns a NULL value if the two input expressions return the same value else the first value (expression) is returned. For example , assume that you want to compare two columns (Address1 and Address2) in the Address table and want to return NULL if both are same to indicate there is NO change and if the addresses are different…

How to return the SHA1 hash value of a string in SQL Server ?

SQL Server provides a function HashBytes function that returns the SHA1 , MD2 , MD4 and few other hash value of the specified input. How to return the SHA1 hash value of a string in SQL Server ? Just pass the ‘SHA1’ as the first parameter and the input as second parameter value to the HashBytes function to get the SHA1 hash value.

SQL Server – Saving changes is not permitted. The changes you have made require the following tables to be dropped...

There are times when you get an error stating “Saving changes is not permitted. The changes you have made require the following tables to be dropped and re-created. You have either made changes to a table that can’t be re-created or enabled the option Prevent saving changes that require the table to be re-created.” You can easily resolve this issue by following the steps mentioned…

Exit the Current Scope without returning a value in SQL Server

You can use the RETURN statement to discontinue the execution of a the T-SQL batch statement. For example , you want to display the Employees whose MaritalStatus is Divorced. You donot want the SQL Statements following it to be executed if this condition doesn’t match. You can use the IF NOT EXISTS and use the RETURN statement as shown in this example. The second statement…

How to ALTER a Column from Null to NOT Null in SQL Server ?

Assume a scenario where you have a table that contains columns which are Nullable integer column. You might want to update them to NOT NULL column (adding NOT NULL constraint) and set its value to zero. How to ALTER a Column from Null to NOT Null in SQL Server ? This would be a two step process where you should first update the column values…

Inner Join in SQL Server

You can use inner join to get the result of two tables that match the criteria. For example , assume that you have a Person table and a Phone Number table where every person would have 0 or more phone number associated to him and you wish to get the person records who have at least one phone number. Here’s the query on how to…

Sleep Command in SQL Server

If you are looking at writing a query to sleep for or WAIT for a certain amount of time before the query execution can begin , you can use the WAITFOR command. For example , if you want to wait for 1 minute , the query or command for the same will be this.

Specify a port number when connecting to SQL Server in Management Studio

There are times when you want to connect to a SQL Server instance that is running on a different port number than default port (1433). How to specify a port number when connecting to a SQL Server instance in SQL Management Studio ? Just specify the SQL Server instance that you are connecting to along with the port number with comma as delimiter. For example…

What is RDBMS ?

RDBMS stands for Relational Database Management Systems . It is a database management system that helps to manage records in rows and columns in the database tables. RDBMS enables the SQL developers or administrators to create and maintain relationship between tables . Some of the typical examples of RDBMS include SQL Server Oracle MySQL PostgreSQL and more…

Top Database Visualization Tools for Database Developers

There are plenty of databases available which includes SQL Server, Oracle, and MySQL etc. The developers would sometimes want to understand the scheme or the database structures etc. What are the different database visualization tools available for the developers? In this article, we list out some of the database visualization tools for database developers. Some of them are free and some are commercial products. Database…

Sorting the Data in a Query in SQL Server

You want the results of the query to be displayed in a sorted order based on some query. In this case , you use the ORDER BY clause in the query by specifying the column on which the sort to work. For example , you want to sort the Departments table in the database by the column DepartmentName. Here’s how the query looks like This…

How to Declare a variable in SQL Server ?

You will most likely declare a variable in the SQL Server and use it in the SQL Statements. You can use the DECLARE statement and specify the variable and datatype and optionally provide a default value. For example , You can declare an NewEmployeeJobTitle variable using the DECLARE statement and provide a default value as ‘Chief Executive Officer’ DECLARE @NewEmployeeJobTitle nvarchar(50) = ‘Chief Executive Officer’;…

How to Check if a login exists in SQL Server ?

If you need to check if a login exists in SQL Server , you can use query the master.dbo.syslogins for it. How to Check if a login exists in SQL Server ? Here’s a sample query demonstrating how you can query master.dbo.syslogins to find out if the login exists in SQL Server. This would retreive all the details of the loginid ‘sa’.