Tag Archive : SQLServer

SSIS import mixed datatypes from Excel to SQLServer

1
Digg me

While importing data from Excel to SQLServer, if a Excel column in has both Text and Numeric values then either Text or Numeric values will be NULL in SQLServer.

See example..

If first data cell (in Excel) in that particular column is of type String/Text then all Numeric values in that column will be NULL in SQL table or viceversa.

This NULL can be avoided in 3 different ways

1. Manual Change

Manually add single quotes (') before each Numeric cell and convert it to String / Text type. This way all the values will be exported to SQLServer without NULLS.

2. Excel to Access to SQLServer

If number of rows to modify is more then …

First export the excel data to MS-Access database and then export Access DB to SQL Server. This way all the values will be exporeted to SQLServer without NULLS.

3. Change EXCEL Connection String

This is simple method but it has its own disadvantage.

Append IMEX=1 to ExcelFileConnectionString. Adding IMEX=1 changes ISAM driver to Import Mode.
(click on the image for full view)

You must be careful that IMEX=1 not be used indiscriminately. This is IMPORT mode, so the results may be unpredictable if you try to do appends or updates of data in this mode.

The possible settings of IMEX are:

0 is Export mode
1 is Import mode
2 is Linked mode (full update capabilities)

More Info :

http://support.microsoft.com/kb/194124/en-us

http://www.jimmcleod.net/blog/index.php/2008/06/25/importing-excel-spreadsheets-into-sql-server-2005-ssis/

How to Save Column Names with Results : SQL Server 2005

2
Digg me

Whether you are copying query output from SQL Server Management Studio or Saving result as .csv file, if you need to save the column name or column header along with result.. here is simple option you have to enable.

Goto Query > Query Options or Press CTRL + Shift + O

Goto Option Grid Under Results

Check the box which says “Include column headers when copying or saving the results”

Click OK.

Run the query and save result as .csv or copy paste to excel with column headers. :)

Useful Undocumented SQL Server Extended Stored Procedures

0
Digg me

- by Alexander Chigrik

An extended stored procedure (xp) is a dynamic link library that runs directly in the address space of SQL Server and is programmed using the SQL Server Open Data Services API. You can run extended stored procedures from the Query Analyzer, for example, just as you would normal stored procedures. Extended stored procedures are used to extend the capabilities of SQL Server.

sp_Msgetversion

This extended stored procedure can be used to get the current version of Microsoft SQL Server. To get the current SQL Server version, run:
EXEC master..sp_MSgetversion
Note. A more common way to retrieve the current SQL Server version (this way provides more information) is to use following SELECT statement:

SELECT @@version
xp_dirtree
This extended stored procedure can be used to get a list of all the folders for the folder named in the xp. To get a list of all the folders in the C:\MSSQL7 folder, run:
EXEC master..xp_dirtree ‘C:\MSSQL7′
xp_subdirs

This extended stored procedure is used to get the list of folders for the folder named in the xp. In comparison with xp_dirtree, xp_subdirs returns only those directories whose depth = 1.

This is the example:
EXEC master..xp_subdirs ‘C:\MSSQL7′
xp_enum_oledb_providers

This extended stored procedure is used to list of all the available OLE DB providers. It returns Provider Name, Parse Name and Provider Description. To get a list of all OLE DB providers for your SQL Server, run:
EXEC master..xp_enum_oledb_providers
xp_enumcodepages

This extended stored procedure can be used to list of all code pages, character sets and their description for your SQL Server. To see this, list, run:
EXEC master..xp_enumcodepages
xp_enumdsn

This extended stored procedure returns a list of all system DSNs and their descriptions. To get the list of system DSNs, run:
EXEC master..xp_enumdsn
xp_enumerrorlogs

This extended stored procedure returns the list of all error logs with their last change date. To get the list of error logs, run:
EXEC master..xp_enumerrorlogs
xp_enumgroups

This extended stored procedure returns the list of Windows NT groups and their description. To get the list of the Windows NT groups, run:
EXEC master..xp_enumgroups
xp_fileexist

You can use this extended stored procedure to determine whether a particular file exists on the disk or not. The syntax for this xp is:
EXECUTE xp_fileexist filename [, file_exists INT OUTPUT]
For example, to check whether the file boot.ini exists on disk c: or not, run:
EXEC master..xp_fileexist ‘c:\boot.ini’
xp_fixeddrives

This very useful extended stored procedure returns the list of all hard drives and the amount of free space in Mb for each hard drive. To see the list of drives, run:
EXEC master..xp_fixeddrives
xp_getnetname

This extended stored procedure returns the WINS name of the SQL Server that you’re connected to. To view the name, run:
EXEC master..xp_getnetname
xp_readerrorlog

This extended stored procedure returns the content of the errorlog file. You can find the errorlog file in the C:\MSSQL7\Log directory, by default. To see the text of the errorlog file, run:
EXEC master..xp_readerrorlog
xp_regdeletekey

This extended stored procedure will delete an entire key from the registry. You should use it very carefully. The syntax is:
EXECUTE xp_regdeletekey [@rootkey=]‘rootkey’, [@key=]‘key’
For example, to delete the key ‘SOFTWARE\Test’ from ‘HKEY_LOCAL_MACHINE’, run:
EXEC master..xp_regdeletekey @rootkey=’HKEY_LOCAL_MACHINE’, @key=’SOFTWARE\Test’
xp_regdeletevalue

This extended stored procedure will delete a particular value for a key in the registry. You should use it very carefully. The syntax is:
EXECUTE xp_regdeletevalue [@rootkey=]‘rootkey’, [@key=]‘key’, [@value_name=]‘value_name’
For example, to delete the value ‘TestValue’ for the key ‘SOFTWARE\Test’ from ‘HKEY_LOCAL_MACHINE’, run:

EXEC master..xp_regdeletevalue @rootkey=’HKEY_LOCAL_MACHINE’, @key=’SOFTWARE\Test’, @value_name=’TestValue’

xp_regread

This extended stored procedure is used to read from the registry. The syntax is:
EXECUTE xp_regread [@rootkey=]‘rootkey’, [@key=]‘key’ [, [@value_name=]‘value_name’] [, [@value=]@value OUTPUT]
For example, to read into the variable @test from the value ‘TestValue’ from the key ‘SOFTWARE\Test’ from the ‘HKEY_LOCAL_MACHINE’, run:

DECLARE @test varchar(20)EXEC master..xp_regread @rootkey=’HKEY_LOCAL_MACHINE’, @key=’SOFTWARE\Test’, @value_name=’TestValue’, @value=@test OUTPUTSELECT @test

xp_regwrite

This extended stored procedure is used to write to the registry. The syntax is:
EXECUTE xp_regwrite [@rootkey=]‘rootkey’, [@key=]‘key’, [@value_name=]‘value_name’, [@type=]‘type’, [@value=]‘value’
For example, to write the variable ‘Test’ to the ‘TestValue’ value, key ‘SOFTWARE\Test’, ‘HKEY_LOCAL_MACHINE’, run:

EXEC master..xp_regwrite @rootkey=’HKEY_LOCAL_MACHINE’, @key=’SOFTWARE\Test’, @value_name=’TestValue’, @type=’REG_SZ’, @value=’Test’
Keep in mind that these undocumented extended stored procedures are not officially supported by Microsoft, and that they may not be found in the next version of SQL Server.

Retrieving the First N Records from a SQL Query

0
Digg me

3 Techniques

1. Simple Fixed ‘N’

SELECT TOP 10 NewsURL, ArticleTitle, ArticleAuthor
FROM NewsStoriesTable
ORDER BY PublicationDate DESC

2. Dynamic ‘N’ Records

CREATE PROCEDURE getRecentArticles
(
@ResultCount int
)
AS

Declare @sSQL

Set @sSQL = “Select TOP ” + @ResultCount + ” NewsURL, ArticleTitle, ArticleAuthor
FROM NewsStoriesTable ORDER BY PublicationDate DESC”

sp_executesql @sSQL

3. Dynamic ‘N’ Records (My Preferred way)

CREATE PROCEDURE getRecentArticles
(
@ResultCount int
)
AS

SET ROWCOUNT @ResultCount — KEY LINE WHICH DOES THE MAGIC

SELECT NewsURL, ArticleTitle, ArticleAuthor
FROM NewsStoriesTable
ORDER BY PublicationDate DESC