Tag: SQL Server
Getting all the Foreign Key constraints list of a table in SQL Server
You might need to write a T-SQL Query to retrieve all the foreign key constraints of a given table in SQL Server. For these cases , you can use the sp_fkeys stored procedure. How to get all the Foreign Key constraints list of a table in SQL Server ?
How to Get a value to a Variable in SQL Server ?
When you use the declare a variable using the DECLARE statement. The same variable can be used to store a value retrieved from the database and later can be used in your SQL Query. How to Get a value to a Variable in SQL Server ? For example , you want to retreive the value of the column JobTitle and store it in the variable…
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.
How to Check if the table exists in SQL Server ?
Do you want to know if the table exists in a in SQL Server ? . Below is a sample query that lets you know if the table exists or not. How to Check if the table exists in SQL Server ? Assume that the table name is Employee , run the below query to know if the table exists in the SQL Server or…
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…
How to Enable All Constraints back in the Database in SQL Server ?
In one of the articles , we explained How to Disable All Constraints in the Database temporarily in SQL Server ? , let’s look at the option of how to enable back the constraints in the SQL Server Database. How to Enable All Constraints back in the Database in SQL Server ? Below is a sample query that enables all the constraints back to the…
using IN clause to provide a list of values for Search Criteria
In SQL Server , you can use the IN clause to providing the list of values for a search criteria. For example , you want to display the employee records who’s job title is one of the following : ‘Design Engineer’,’Tool Designer’. The above query uses the IN clause to filter the employee records using the arbitrary list of values. Alternatively , you can also…
What is Normalization ?
Normalization in database is a set of technique applied to the data structures (tables) based on some predefined rules in building the database. The Normalization involves the process of organizing data in order to minimize redundancy. The Normalization generally involves splitting the database tables into multiple related tables .
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…
How to check if a column exists in a table in SQL Server ?
If you want to find out if a column exists in a SQL Server table , you can use the sys.columns table and specify the name of the column and table as parameter. How to check if a column exists in a table in SQL Server ? For example , you want to find out if the GroupName column exists in the table “HumanResources.Department” table…
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…
How to Disable All Constraints in the Database temporarily in SQL Server ?
There are times when you may want to disable all the constraints in the your SQL Server Database tables when importing the data from the external data source . How to Disable All Constraints in the Database temporarily in SQL Server ? Below is a sample query that lets you to quickly disable the constraints like foreign keys , unique keys etc for the database…
SQL Server 2014 Tutorial – Personalize SQL Management Studio
Problem Statement You need to personalize SQL Server Management Studio. Solution There are various personalization options available for the SQL Server users in the SQL Management Studio. For example, if you want the Object explorer to be place on a different position instead of showing it in the left side of the SSMS, you can just move it by clicking it on the title bar…
How to get the list of all tables from a SQL Server database ?
If you need to get the list of all tables from a SQL Server database , you can query the information_schema.tables by specifying the TABLE_TYPE parameter search criteria to “BASE TABLE’. How to get the list of all tables from a SQL Server 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 do you check if an index exists in a table?
You can find out if the index exists for a table by querying the sys.indexes table as shown in this blog post’s code sample. How do you check if an index exists in a table? This will list out all the indexes of the table.
Using NOT operator to inverse the condition in SQL Server
Imagine a scenario where you want to retreive records that you dont want. You can use the NOT operator to reverse the search condition. For example , You want to get all the departments having the GroupName other than ‘Research and Development’. The other way of getting the same result is using the != in every condition.