Add a group id to address book kind of data with SQL
Posted: February 23, 2009 Filed under: SQL Server Leave a commentdeclare
@i intset
@i=1update
dbo.NABS_ListSET
@i= CASE WHEN myColumn is null THEN @i+1 ELSE @i END,gid
= @iIMEX=1 revisit and TypeGuessRows setting change to 0 (watch for performance)
Posted: February 13, 2009 Filed under: Excel, SQL Server 12 CommentsWhen we import mixed type data in a column from excel to SQL server, we may have issues with losing some data. We can set IMEX=1 to allow the incoming column allow mixed data types based on the Registry setting ImportMixedTypes to Text. However, there is a default setting for Excel to Guess Data Type of a column, the default value is 8.(The ISAM driver by default looks at the first eight rows and from that sampling determines the datatype.) We can change this value to 0 to force Excel to check all values in the column to choose the data type for the column.
Here is the registry setting we need to check:
Type Regedit from Run… command:
Under entries
Hkey_Local_Machine/Software/Microsoft/Jet/4.0/Engines/Excel/TypeGuessRows
Hkey_Local_Machine/Software/Microsoft/Jet/4.0/Engines/Excel/ImportMixedTypes
Change:
TypeGuessRows: 0 —-Check all column values before choosing the appropriate data type.ImportMixedTypes: Text —-import mixed-type columns as text.
Connection string:
Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\data\Book1.xls;Extended Properties="EXCEL 8.0;IMEX=1;HDR=YES";
–Or:
SELECT * FROM OPENROWSET(‘Microsoft.Jet.OLEDB.4.0’, ‘Excel 8.0;IMEX=1;HDR=YES;Database=C:\data\Book1.xls;’, ‘select * from [Sheet1$]’)
We are using IMEX=1, the Import Mode, in the connection string to apply the registry setting.
PS:
The possible settings of IMEX are: 0,1,2.
(0 is Export mode 1 is Import mode 2 is Linked mode (full update capabilities))
Swap column using UPDATE in SQL
Posted: February 9, 2009 Filed under: SQL Server Leave a commentDECLARE @t TABLE(
oddseqnum INT,
evenseqnum INT
)
INSERT INTO @t
SELECT 1,
2
UNION ALL
SELECT 3,
4
UNION ALL
SELECT 5,
6
UNION ALL
SELECT 7,
8
UNION ALL
SELECT 9,
10
SELECT *
FROM @t
UPDATE @t
SET oddseqnum = evenseqnum,
evenseqnum = oddseqnum
SELECT *
FROM @t