2012年2月23日星期四

BCP and column defaulting

I am bringing data from a file using bcp as below in a stored procedure.
SET @.SQLString = 'BCP STAGING IN "' + @.file_path + '" -c -F2 -Uxyz -Pxyz'
EXEC master..xp_cmdshell @.SQLString
The table has only one column varchar (1024). I need to add one more column
called code which i can pass to the stored procedure but need to be added to
the staging table when rows gets inserted. Because the stored procedure
could be called at the same time with two different codes. So based on what
code is passed to the SP all the rows inserted by bcp in that instance of
the SP should have that code. How would i do this?
Thanks,
DavidHi
You may want to look at using the BULK INSERT command and load it into a
temporary table.
CREATE TABLE staging( id int not null,
[name] varchar(10),
col1 varchar(80),
col2 varchar(80) )
CREATE PROCEDURE MyLoad ( @.Name varchar(10), @.filename varchar(256) )
AS
BEGIN
CREATE TABLE #test ( id int not null,
col1 varchar(80),
col2 varchar(80) )
EXEC ('BULK INSERT #test FROM ''' + @.filename + '''')
INSERT INTO Staging ( id, name, col1, col2 )
SELECT id, @.Name, col1, col2
FROM #test
END
EXEC MyLoad 'Test1', 'C:\temp\test.txt'
John
"Viji" wrote:

> I am bringing data from a file using bcp as below in a stored procedure.
> SET @.SQLString = 'BCP STAGING IN "' + @.file_path + '" -c -F2 -Uxyz -Pxyz'
> EXEC master..xp_cmdshell @.SQLString
> The table has only one column varchar (1024). I need to add one more colum
n
> called code which i can pass to the stored procedure but need to be added
to
> the staging table when rows gets inserted. Because the stored procedure
> could be called at the same time with two different codes. So based on wha
t
> code is passed to the SP all the rows inserted by bcp in that instance of
> the SP should have that code. How would i do this?
> Thanks,
> David
>
>

没有评论:

发表评论