Skip to main content

Posts

Showing posts with the label sql

How to backup database in sql server using query

This article demonstrates how to backup database from SQL server to disk using query. Let's start step by step. Open Microsoft SQL Server management studio and run following query. The following example backs up the complete TestDB database to disk. USE TestDB; GO BACKUP DATABASE TestDB TO DISK = 'c:\temp\TestDB.bak' WITH FORMAT, MEDIANAME = 'SQLServerBackups', NAME = 'Full Backup of TestDB'; GO   Full back up to disk as default location from SQL Server Management Studio In this example, the database Test will be backed up to disk at the default backup location. 1. The connect SQL server database engine instance in your machine. 2. Expand Databases , then right click on Test database, then select Tasks , then select Back Up . 3. Then click OK. 4. When the backup completes successfully, select OK to close dialog box.  

How To Import Data From Excel To SQL Table Using Query

This article demonstrates how to import the data from a Microsoft Excel sheet to a SQL Server table in Microsoft SQL Server. Let’s start step by step. Here I have one Excel sheet with employee details data with six columns as above and this detail I will use as an example and load into employee table.  Open SQL Server management studio and run following SQL query. DECLARE @Qry NVARCHAR(MAX) DECLARE @FilePath NVARCHAR(255)='D:\Employee.xlsx' EXEC sp_configure 'Show Advanced Options', 1; RECONFIGURE; EXEC sp_configure 'Ad Hoc Distributed Queries', 1; RECONFIGURE; SET @Qry='SELECT * FROM OPENROWSET(''Microsoft.ACE.OLEDB.12.0'', ''Excel 12.0; HDR=yes; Database='+CONVERT(NVARCHAR(255),@FilePath)+''', [Sheet1$]);' INSERT INTO Employees EXEC (@Qry) select * from Employees You have to make sure "Microsoft Access Database Engine Redistributable" is install or not in your local machine. If it is not

SQL Joins

A SQL JOIN is used to combine two or more tables, based on a related column between tables. Let's take a look selection from the "Orders" table: OrderId CustomerId OrderDate 1001 1 2020-05-22 1002 2 2020-05-22 1003 3 2020-05-22 Then, look at a selection from the "Customers" table: CustomerId CustomerName Country 1 John USA 2 Bill USA 3 Jimmy USA Now, the relationship between Orders table and Customers table is the "CustomerId" column. Then, we can create the following SQL statement that contains an INNER JOIN, It'll select records that have matching values in both tables: SELECT o.OrderId, c.CustomerName, o.OrderDate FROM Orders as o INNER JOIN Customers as c ON o.CustomerId=c.CustomerId and above SQL statement will produce following result like this: Orde