Skip to main content

MSSQL Code

took me some time to search for it eventhough i already found a few months ago ;(

first is to read from excel file..i tried this using Query Analyzer
-- Read from Excel file
SELECT *
FROM OPENROWSET(
'Microsoft.Jet.OLEDB.4.0',
'Excel 8.0;Database=PATH_TO_EXCEL_FILE_HERE;IMEX=1;HDR=Yes;',
'SELECT * FROM [SHEET_NAME_HERE$]')

Or

SELECT *
FROM OpenDataSource(
'Microsoft.Jet.OLEDB.4.0',
'Data Source="PATH_TO_EXCEL_FILE_HERE";Extended properties=Excel 8.0;')...SHEET_NAME_HERE$


PS:
[FROM MS]
IMEX=1 tells the driver to use Import mode. In this state, the registry setting ImportMixedTypes=Text will be noticed. This forces mixed data to be converted to text. For this to work reliably, you may also have to modify the registry setting, TypeGuessRows=8. The ISAM driver by default looks at the first eight rows and from that sampling determines the datatype. If this eight row sampling is all numeric, then setting IMEX=1 will not convert the default datatype to Text; it will remain numeric. ,

HDR=Yes means the first row will always be the Header.

second is to save query result to a text file in this example..if it contains alot of records..
using BULK COPY and try this in command windows

BCP "SELECT * FROM DB_NAME..TABLE_NAME" queryout "FILE_NAME_PATH.txt" -S "SERVER_NAME\INSTANCE_NAME" -T -w -k -t"|"

-- insert to a table from a file
BULK INSERT DB_NAME..TABLE_NAME FROM 'FILE_NAME_PATH.txt'
WITH
(
FIELDTERMINATOR = '|',
ROWTERMINATOR = '\n',
DATAFILETYPE = 'widechar',
KEEPNULLS
)
GO

see u

Comments

Popular posts from this blog

SBO - Internal Error (-5002) Occured in Inventory Transfer

this is something that not well documented in SBO, it took me several hours to look at Google and can't find anything about it. Thank God friend of mine got answer for this one.So the Sympton is something like this, u need to make Inventory Transfer for Batch Item and Serial Item in one single Transaction so the Transaction is the combination of it. ex in detail lines: 1. Serial Item 2. Batch Item 3. Batch Item 4. Batch Item 5. Serial Item 6. Serial Item . When u reach this code oStockTransfer.Add it will return an error "Internal Error (-5002) Occured". This is because ur details is not in order, meaning u need to order the line with combination of Batch first then Serial or Serial first then Batch so be careful with this thing :D see u

Crystal Report 2010 - Failed to load database information

"Failed to load database information" error when i try deploy my program with CR 2010 using TTX file as report definition this is because there is missing .dll, call "CRDB_FIELDDEF.DLL"..u need to manually copy the file to "C:\Program Files (x86)\SAP BusinessObjects\Crystal Reports for .NET Framework 4.0\Common\SAP BusinessObjects Enterprise XI 4.0\win32_x86" folder then the report is loading again it said that TTX is old tech but i'm still using it because i don't need to connect the report to any DB, but until i found new one to replace :D check this link to read it [EDIT 25 Jan 2011] i think i'll using ADO.NET Dataset next time :D until then

Resolve Collation error in SQL Server

i often get this error when trying to JOIN some table Cannot resolve the collation conflict between "SQL_Latin1_General_CP850_CI_AS" and "SQL_Latin1_General_CP1_CI_AS" in the equal to operation. u need to use COLLATION when performing the JOIN, something like this SELECT field1 FROM table1 INNER JOIN table2 ON table1.field3 COLLATE DATABASE_DEFAULT = table2.field4 COLLATE DATABASE_DEFAULT i found this when trying to JOIN, maybe it affect other areas..until i found one see u