JSON functions and examples in SQL Server

In this blog, we will see the use of JSON functions in SQL Server. We will also see how to query and modify a JSON data.As you already know JSON stands for JavaScript Object Notation, which is a lightweight computer data interchange format.We can use JSON data in SQL Server tables as well. There is no … Read more

Differences between Stored Procedure and Functions

Very frequent question asked in interview is Differences between Stored Procedure and Functions.Below are the differences between these 2. 1) Functions must return a value. Stored procedure need not be.2) Stored procedures can be called using EXEC command where as Functions can be called from another SQL Statement.3) Stored Procedures are precompiled. Functions are not precompiled.4) Generally Stored procedures are … Read more

Important SQL Query asked in Interview

Important SQL Query asked in InterviewSQL query to check a record in table, if exist then insert else update the record. BEGIN  IF EXISTS (SELECT * FROM [users] WHERE email=@email)    UPDATE [users] SET updated_date = GETDATE() WHERE email=@emailELSE    INSERT INTO [users] (uid,first_name,last_name,email,password,gender,is_active,reg_date) VALUES (NEWID(),@fname,@lname,@email,@password,@gender,1,GETDATE())ENDStored procedure to return id of last inserted record:@id int … Read more

SQL Server Interview Questions.

In this blog, we will see some of the important SQL Interview Questions and Answers. 1. What is a Primary Key? The primary key is used to uniquely identify a record. A table can not have more than one primary key.If a table has userid, email, passport no. and userid is the primary key then how to maintain the … Read more

All about sp_addlinkedserver syntax

sp_addlinkedserver linked 2 servers. Once sp_addlinkedserver query executes, query can be run against these servers. Run below queries one by one to link, to provide login credentials and to access data EXEC sp_addlinkedserver @server=’ServerName’ EXEC sp_addlinkedsrvlogin ‘ServerName‘, ‘false’, NULL, ‘username’, ‘password’ select * from [ServerName].[DBName].[DBO].[TableName] Once your requirement is done, you may drop the linking by … Read more

How to link 2 SQL DB servers

If someone wants to get data from 2 tables from 2 different SQL DB server. Then execute below stored procedures before running your query. Step1: exec sp_addlinkedserver    @server=db-server-name Step2: EXEC sp_addlinkedsrvlogin db-server-name, false, NULL, id, password Once you executed above SPs successfully, you can check linked server list by executing below SP. exec sp_linkedservers

Error while restoring sql database in microsoft sql server 2012

I found below error while restoring a database to Microsoft SQL Server 2012.System.Data.SqlClient.SqlError: The operating system returned the error 5(Access is denied.) I solved this problem by doing below step:In restore window, from left side pane click on FilesThen in properties please ensure that “Relocate all files to folder” is checked. Try this, i resolved … Read more

How to automatically generate unique id in sql?

In sql there are many options to generate unique number. Here I am explaining about how to generate standard unique number in sql. Below is sql query : CREATE TABLE dbo.tblTable1  (ID INT IDENTITY(1,1) NOT NULL PRIMARY KEY CLUSTERED,   UserID AS UID + RIGHT(00000000 + CAST(ID AS VARCHAR(8)), 8) PERSISTED,   username varchar(200),city varchar(200)  )

Bulk copy data from excel sheet to sql table

How to process bulk copy data from excel sheet to sql table. Here is code to copy data from excel to sql.       C# Code:       string excelFile = @”C:Book1.xls”;        string ssqlTable = “[MyTableName]”;        string exceldataQuery = “select [id],[email],[status] from [Sheet1$]”;        try    … Read more