Thursday, March 8, 2012
BULK INSERT flat file with only one column
I have a text file with a single column that i need to bulk insert into a table with 2 colums - an ID (with identity turned on) and col2
my text file looks like:
row1
row2
row3
...
row10
so my bulk insert i have like this:
BULK INSERT test FROM 'd:\testBig.txt'
WITH (
DATAFILETYPE = 'char',
FIELDTERMINATOR = ',',
ROWTERMINATOR = '\n'
)
but i get the error:
Server: Msg 4866, Level 17, State 66, Line 1
Bulk Insert fails. Column is too long in the data file for row 1, column 1. Make sure the field terminator and row terminator are specified correctly.
However, as you can see from the text file, there is only one column, so i dont have any field terminators.
Any ideas how to make this work?
Thanks.You'll probably need to use a format file to be able to skip columns.|||i dont think i want to skip any columns ??
the above method works if i have say 2 columns in my text file, and 3 columns in my table, since the first column in the table is an identity column.
my problem is that the bulk insert doesnt seem to be able to handle a text file with just one field per row. it seems to be looking for a field delimiter, when in fact there is only one field and then its the row delimiter etc etc.|||Create a table with one column which is used to store data from your text file and then do BULK INSERT.
BULK INSERT test FROM 'd:\testBig.txt'
WITH (
DATAFILETYPE = 'char',
--FIELDTERMINATOR = ',',
ROWTERMINATOR = '\n'
)
At last, you can add a column for the ID.|||Of course you do, you want to skip columns in the target table by not populating them (I am talking about your identity field.) With "FORMATFILE = 'your_format_file' " you can define exactly what you want or don't want to populate.
Sunday, February 12, 2012
Building a table from SQL Query
I have query that returns all the colums in a row (SELECT * FROM table WHERE value = 'value') and I need to build a table with this data. Some of the columns may not have values in them, and so I dont want to build a table row for it. I also need to use the column name as the table header. As an example:
==============================
Column Name || Column Value
------||------
Column Name || Column Value
------||------
I hope I have explained myself properly. Any help would be greatly appreciated.
Do you want to transpose your data from row to column?
If this is what you want, it depends on which version of SQL server you are using. In SQL Server 2005, there is a PIVOT function which does this knid of job. In 2000, you can use CASE statement to construct a customized solution.
If you need help on this, you can post some sample data for others to look at. Plus the final result you are expecting.
|||Unless I misunderstood, PIVOT is not quite what I am looking for.What I need is to take the name of the column that a field is in and use that as a header for table.
Any help would be greatly appreciated.
GKC|||
Could you test this out: (instead of PIVOT, use UNPIVOT)
step 1:
create table usingunpivot2005
(
myID varchar(10) primary key,
col1 varchar(50),
col2 varchar(50),
col3 varchar(50)
)
insert into usingunpivot2005 (myID, col1, col2, col3)
values('myid01','40','20','30')
Step 2:
with switchCTE as(
select cast(colname as varchar(5)) as colname, value
from usingunpivot2005 p
UNPIVOT
(value for colname in (col1, col2, col3)) as unpvt)
select *
from switchCTE
You should know your columns' name. If the value in that column is NULL, that column will be skipped.
|||SELECT ColName, MIN(CASE WHEN P.myID = 'myid01' THEN col1 END) AS ValueFROM
(SELECT 'Col 1' as ColName, myID, col1 FROM usingunpivot2005
UNION
SELECT 'Col 2', myID, col2 FROM usingunpivot2005
UNION
SELECT 'Col 3', myID, col3 FROM usingunpivot2005) P
GROUP BY ColName
ORDER BY ColName