Showing posts with label SQL Server. Show all posts
Showing posts with label SQL Server. Show all posts

Monday, February 3, 2014

SQL Server: Import Tab From Excel

--FOR XLS FILES (ALL COLUMNS EXAMPLE)

SELECT * INTO NEW_TABLE_NAME
FROM OPENROWSET('Microsoft.Jet.OLEDB.4.0',

'Excel 8.0;Database=FILESOURCE&NAME.xls', 'SELECT * FROM [TABNAME$]')


--FOR XLSX FILES (SPECIFIC COLUMNS EXAMPLE)

SELECT
[COLUMN_NAME],
[ANOTHER_COLUMN_NAME]
INTO NEW_TABLE_NAME
FROM OPENROWSET('Microsoft.ACE.OLEDB.12.0',
'Excel 8.0;Database=FILESOURCE&NAME.xlsx', 'SELECT * FROM [TABNAME$]')


--LIVE EXAMPLE FOR FY11 ES, XLSX FILE

SELECT
[Legacy Sales Order Identifier],
LEFT([Legacy Sales Order Identifier],9) AS [ES Transaction Number],
'Int ES' AS [Sales Channel Name],
[Sales Motion]
INTO ESTS_Data
FROM OPENROWSET('Microsoft.ACE.OLEDB.12.0',
'Excel 8.0;Database=C:\location\filename.xlsx', 'SELECT * FROM [FY11 ES Data 4-14-11$]')

Read More

SQL Server: Enable 'Ad Hoc Distributed queries'

If you want to use OpenDataSource or OpenRowSet query, then you must have to enable “Ad Hoc Distributed queries”. It is disabled by default as a part of security configuration as per Microsoft.
You can set this option by two ways.
1.)    sp_configure command
2.)    Surface area configuration.
Since I am a script bee, I will explain steps for sp_configure.
You can run sp_configure to see current status of “run_value” field. If you don’t able to see “Ad Hoc Distributed queries” in the results set.  You have to enable “Show advanced option” by following command.
sp_configure 'show advanced options', 1
Reconfigure
GO
You may get message
Configuration option ‘show advanced options’ changed from 0 to 1. Run the RECONFIGURE statement to install.

Once you are done with that, you can see “Ad Hoc Distributed queries” in list of sp_configure. You have to set “Run_value” to “1”. If it is “0” than run following command.

sp_configure 'Ad Hoc Distributed Queries', 1
Reconfigure
GO
Which will show you following message.
Configuration option ‘Ad Hoc Distributed Queries’ changed from 0 to 1. Run the RECONFIGURE statement to install.
Once you will go through the procedure described here, you will be able to run OpenRowSet and OpenDataSource query.


Read More

SQL, Excel, MS Access: First Word in String

Some examples, find out yourself, hope this helps

IN SQL

--Taking the first word of the Partner Name (FIELD) in this case
(CASE WHEN CHARINDEX(' ',[FIELD]) = 0 THEN UPPER([FIELD])
 ELSE REPLACE(UPPER(SUBSTRING(LTRIM([FIELD]),1,CHARINDEX(' ',[FIELD]))),',','')
 END)



IN EXCEL
=IF(ISERROR(LEFT(B2,FIND(" ",B2)-1)),B2,LEFT(B2,FIND(" ",B2)-1))


IN ACCESS
TRIM(Left([Yourfieldname], InStr([Yourfieldname], " ")))



Read More

SQL Server 2008 R2: Configure Server Startup Options (SQL Server Configuration Manager)

SQL Server 2008 R2

Use SQL Server Configuration Manager to configure startup options to be used each time the Database Engine starts. For a list of startup options, see Using the SQL Server Service Startup Options.
SQL Server Configuration Manager writes startup parameters to the registry. They take effect upon the next startup of the Database Engine.

Note
On a cluster, changes must be made on the active server while SQL Server is online, and will take effect when the Database Engine is restarted. The registry update of the startup options on the other node will occur upon the next failover.

To configure startup options
  1. In SQL Server Configuration Manager, click SQL Server Services.
  2. In the right pane, right-click SQL Server (<instance_name>), and then click Properties.
  3. On the Advanced tab, in the Startup Parameters box, type the parameters separated by semicolons (;).
    For example, to start in single-user mode, insert
     -m; in front of the existing startup options, and then restart the database. (When you start SQL Server in single-user mode, first stop SQL Server Agent. Otherwise, SQL Server Agent might connect first and prevent you from connecting as a second user.)

Important
After you are finished using single-user mode, you must remove the -m; from the Startup Parameters box before you can restart the server instance in the normal multi-user mode.
  1. Click OK.
  2. Restart the Database Engine.



Read More

SQL Server 2008 R2: Restore the master Database (Transact-SQL)

SQL Server 2008 R2

This topic explains how to restore the master database from a full database backup.
To restore the master database
  1. Start the server instance in single-user mode.
    For information about how to specify the single-user startup parameter (
    -m), see How to: Configure Server Startup Options (SQL Server Configuration Manager).
  2. To restore a full database backup of master, use the following RESTORE DATABASE Transact-SQL statement:
    RESTORE DATABASE master FROM
     <backup_device> WITH REPLACE
    The REPLACE option instructs SQL Server to restore the specified database even when a database of the same name already exists. The existing database, if any, is deleted. In single-user mode, we recommend that you enter the RESTORE DATABASE statement in the sqlcmd utility. For more information, see Using the sqlcmd Utility.

Important
After master is restored, the instance of SQL Server shuts down and terminates the sqlcmd process. Before you restart the server instance, remove the single-user startup parameter. For more information, see How to: Configure Server Startup Options (SQL Server Configuration Manager).
  1. Restart the server instance and continue other recovery steps such as restoring other databases, attaching databases, and correcting user mismatches.

The following example restores the master database on the default server instance. The example assumes that the server instance is already running in single-user mode. The example starts sqlcmd and executes a RESTORE DATABASE statement that restores a full database backup of master from a disk device:Z:\SQLServerBackups\master.bak.
 Note
For a named instance, the sqlcmd command must specify the -S<ComputerName>\<InstanceName> option.
C:\> sqlcmd
1> RESTORE DATABASE master FROM DISK = 'Z:\SQLServerBackups\master.bak' WITH REPLACE;
2> GO



Read More

SQL Server Management Studio: Set a Database to Single-user Mode

This topic describes how to set a user-defined database to single-user mode by using Object Explorer in SQL Server Management Studio. Single-user mode specifies that only one user at a time can access the database and is generally used for maintenance actions.

Caution
If other users are connected to the database at the time that you set the database to single-user mode, their connections to the database will be closed without warning.
The database remains in single-user mode even if the user that set the option logs off. At that point, a different user, but only one, can connect to the database.

To set a database to single-user mode
  1. In Object Explorer, connect to an instance of the SQL Server Database Engine, and then expand that instance.
  2. Right-click the database to change, and then click Properties.
  3. In the Database Properties dialog box, click the Options page.
  4. From the Restrict Access option, select Single.
  5. If other users are connected to the database, an Open Connections message will appear. To change the property and close all other connections, click Yes.




Read More

SQL Server: Import MS Access DB Tables into SQL Server

Microsoft SQL Server 2005 and 2008 offer the Import and Export Wizard to move data to and from an external source. You can also create a basic SQL Server Integration Services (SSIS) package which can be used later in the Business Intelligence Development Project.
With the Import and Export wizard, you can access different types of data sources. These sources include database formats such as Microsoft Access, Microsoft SQL Server, flat files, Microsoft Excel, and Oracle. This article discusses importing Access 2007 database tables into MS SQL Server 2005/2008.

Importing Microsoft Access MDB databases (2003-format or earlier) is a built in feature of SQL Server. However, because of the difference between the database engine of Microsoft Access 2007 and earlier versions, it is not possible to connect to the Access 2007 database (*.ACCDB) using the built-in data source “Microsoft Access”. To import data from a Microsoft Access 2007 database, you must install the OLEDB Provider for Microsoft Office 12.0 Access Database Engine. Refer to the following FMS tip for details:

After installing this driver, open SQL Server Management Studio and connect to the desired instance of SQL Server database engine. In the Object Explorer, it shows database list available in that instance of SQL Server. Select a desired database or create a new one. Right Click this database and select Tasks -> Import Data.


Now follow the several pages of the wizard. The steps below detail how to import data into a SQL Server 2005 database, but the steps are very similar in SQL Server 2008.
On the first page, select the Data source from which you want to import the data. There are several data sources also available such as:
  • Microsoft OLEDB provider for SQL Server
  • Microsoft OLEDB provider for Oracle
  • SQL Native Client
  • Microsoft Access
  • Microsoft Excel, etc…
Because of the difference between the database engine of Microsoft Access 2007 and earlier version of Microsoft Access, it is not possible to connect to the Access 2007 database using data source “Microsoft Access”. You can use this if you wish to import data from a MDB format, but not an ACCDB from Access 2007.
If you have properly installed the 2007 Office System driver, you will see another Data Source option: “Microsoft Office 12.0 Access Database Engine.”

Select this option to import from an ACCDB file. Then click the Properties button to open the Data Link Properties window:

On the Data Link Properties page, provide the location of database in to the Data Source field, and enter the User name and password if applicable. Click on the Test Connection and make sure it succeeds.
Click OK to close the Data Link Properties sheet, and continue to the next page of the Wizard, to choose the Destination.

Your destination is SQL Native Client, since you want to import to SQL Server. The server name and database should already contain the database that you want to import to.
Click Next to continue, and choose option for Copy data from one or more tables or views.

Click Next, and select the Source Tables or Views to import. If necessary use the Edit Mapping button to map the Columns correctly.

The next screen of the wizard shows two options. First one is to Execute Package Immediately and second one is to save SSIS package which can be used in the Business Intelligence Development Studio Project.

Click Next and Finish the Wizard. It shows all successful action list and errors if any.

Once execution is finished, you can click on the database and see the imported tables from Access database.

This way you can import data from Access 2007 database tables and also create an SSIS package for a Business Intelligence Development Project.
The limitation of the wizard is it doesn’t share sources with other packages and more advanced logic can’t be applied in the wizard.



Read More