Groups | Blog | Home
all groups > sql server (alternate) > august 2003 >

sql server (alternate) : Problem with uploading data SQL



sql NO[at]SPAM hayes.ch
8/21/2003 2:36:41 AM
[quoted text, click to view]

One option is to create a staging table with one column, load the data
into that table without changing it, then insert into the final table
like this:

insert into dbo.TEST (col1, col2, col3)
select left(StagingColumn, 6), left(StagingColumn, 8),
left(StagingColumn, 3)
from dbo.StagingTable

Robert K
8/21/2003 8:59:50 AM
Hello,

I have problem:

My *.txt file is like it:
"
12345612345678123
abcdefabcdefghabc
" etc.

i want upload data into table (for example TEST) i want to sql read this
file and automatically upload to table.(as job for example)
but i have 3 columns and i dont know how to separate this text to 3 diffrent
text columns

1 column | second column | third column
-----------------------------------------
123456 | 12345678 | 123
abcdef | abcdefgh |abc

PLEASE HELP ME, i dont know how to do it.

Robert Kloma





Michael Zankl
8/21/2003 9:56:35 AM
Hi Robert,

i'm using the following stored proc (sp) for this. I suggest to use field
separators to make it
easier to separate the columns. This sp can be executed by a job.
The imported file should look like this:

1234;abcd;1212
321123;kdkdkd;121233

In the sp you have to replace CPRave15 with your database name.

Hope it helps.

Michael Zankl
http://www.zankl-it.de
Berlin



SET QUOTED_IDENTIFIER OFF
GO
SET ANSI_NULLS ON
GO



/*
============================================================================
==
Syno: imports file specified in @UNCPathFileName into a table, specified
in @DBTable
Use ';' as fieldterminator in the imported file


REMARKS:
- User, who runs this SP, has to be a member of SysAdmin
or BulkAdmin
- User must have Insert-Permission on specified Table or
has to be a member of db_owner


TEST:


DECLARE @RC int,
@UNCPathFileName varchar(1024),
@DBTable varchar(128)


SET @UNCPathFileName = '\\Absrv02\Components\Debitoren.csv'
SET @DBTable = 'cprSYSMD_DebImp'


EXEC @RC = cprIMP_File @UNCPathFileName, @DBTable
PRINT @RC

select * from cprsysmd_debimp
--delete from cprSYSMD_DebImp




Author: MZA, http://www.zankl-it.de, 14.01.2003
============================================================================
==
*/
CREATE PROCEDURE cprIMP_File @UNCPathFileName varchar(1024),
@DBTable varchar(128)
AS


DECLARE @RetVal int,
@Cmd varchar(8000)




--Example
-- BULK INSERT CPRave15.dbo.cprSYSMD_DebImp
-- FROM '\\Absrv02\Components\Debitoren.csv'



SET @Cmd = '
BULK INSERT CPRave15.dbo.' + @DBTable + '
FROM ''' + @UNCPathFileName + '''
WITH (FIELDTERMINATOR = '';'')' --<== IMPORTANT: use a fieldterminator in
imported file

--print @Cmd
EXEC (@Cmd)

SET @RetVal = @@ROWCOUNT



RETURN @RetVal

GO
SET QUOTED_IDENTIFIER OFF
GO
SET ANSI_NULLS ON
GO



"Robert K" <rkloma@hotmail.com> schrieb im Newsbeitrag
news:bi1qj0$etr$1@news.onet.pl...
[quoted text, click to view]

Robert K
8/21/2003 1:19:54 PM
hello, thank you for hint but:

i want to read this data
My *.txt file is like it:
"
1234567890

" etc

1 column | second column | third column
-----------------------------------------
12 | 3456 | 789

how to do it ,

[quoted text, click to view]

the result is of this is
1 column | second column | third column
-----------------------------------------
12 | 1234 | 1234


AddThis Social Bookmark Button