sql server default value not populating with bcp but working with direct insert

Amjai 1 Reputation point
2020-11-11T01:41:49.2+00:00

I am trying to insert into my table using bcp format file. But after insert the columns with default value is not getting inserted.
But when I run insert statement directly on the sql server, it takes the default value.

my table: MarginFeed

CREATE TABLE MarginFeed(
id BIGINT NOT NULL IDENTITY,
SMCP VARCHAR(50),
adtv NUMERIC(23,4),
adtvPeriod VARCHAR(8),
marketCap NUMERIC(23,4),
shortInt NUMERIC(23,4),
activeInd VARCHAR(5) DEFAULT 'R',
enteredDate DATETIME DEFAULT GETDATE(),
lastUpdatedDate DATETIME
);

the text file only has data for SMCP , adtv, adtvPeriod, marketCap, shortInt :

text file: Feed.txt
SMCP|ADV|ADVPeriod|MarketCapInUsd|ShortInterest
102982306|23068.76666666|90|29820313.20000000|
103656446|6167435.98888888|90|12318467.90720000|

format file: FeedFormat.Fmt
12.0
5
1 SYBCHAR 0 50 "|" 2 SMCP ""
2 SYBCHAR 0 54 "|" 3 adtv ""
3 SYBCHAR 0 8 "|" 4 adtvPeriod ""
4 SYBCHAR 0 54 "|" 5 marketCap ""
5 SYBCHAR 0 54 "\n" 6 shortInt ""

after bcp table value:
id SMCP adtv adtvPeriod marketCap shortInt activeInd enteredDate lastUpdatedDate
1 102982306 23068.7666 90 29820313.2000 (null) 2020-11-10 19:53:15 (null)
2 103656446 6167435.9888 90 12318467.9072 (null) 2020-11-10 19:53:15 (null)

The value of column 'activeInd' is defaulted to 'R' in create statement but it is blank after bcp IN using format file.

But when I run below on SQL Server Studio directly, i get the expected result.

INSERT INTO MarginFeed(SMCP, adtv, adtvPeriod, marketCap, shortInt)
VALUES ('102982306', 23068.76666666, '90', 29820313.20000000, null),
('103656446', 6167435.98888888, '90', 29820313.20000000, null);

Result:
id SMCP adtv adtvPeriod marketCap shortInt activeInd enteredDate lastUpdatedDate
1 102982306 23068.7667 90 29820313.2000 (null) R 2020-11-10 20:36:22 (null)
2 103656446 6167435.9889 90 29820313.2000 (null) R 2020-11-10 20:36:22 (null)

activeInd = R after direct insert.

Please help why it not working when using bcp.

Below bcp command I am using:
freebcp $table in $file -f$FormatFile $conStr -I$SQL_INTERFACES -t$DELIMETER -b$bnum -e$error

SQL Server
SQL Server
A family of Microsoft relational database management and analysis systems for e-commerce, line-of-business, and data warehousing solutions.
13,631 questions
Transact-SQL
Transact-SQL
A Microsoft extension to the ANSI SQL language that includes procedural programming, local variables, and various support functions.
4,620 questions
{count} votes

1 answer

Sort by: Most helpful
  1. Erland Sommarskog 109.8K Reputation points MVP
    2020-11-11T23:20:15.543+00:00

    I don't know about FreeBCP (never heard of it before), but I loaded your file with regular BCP that ships with SQL Server, and I got R in activeInd.

    On the other hand, I had to change the data type of the numeric columns to be numeric(23,8), or else I got errors on the excessive decimals.

    SYBCHAR? That's really old fashioned, but I was surprised to see that SQL Server's BCP accepts it!

    By the way, what does "SELECT @@version" report?

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.