[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Strange error

Posted on 2012-08-16
5
Medium Priority
?
450 Views
Last Modified: 2012-08-19
Hi!

Have this store procedure:

SET QUOTED_IDENTIFIER OFF

go
CREATE PROCEDURE VEC_Fakturaimport
@PathFileName varchar(100),
@FileType int
AS

DECLARE @SQL varchar(2000)
IF @FileType = 1
 BEGIN
  SET @SQL = "BULK INSERT dbo.VEC_Fakturalinjer_TMP FROM '"+@PathFileName+"' WITH (CODEPAGE='ACP', FIELDTERMINATOR = '"";""') "
 END
ELSE
 BEGIN
  SET @SQL = "BULK INSERT dbo.VEC_Fakturalinjer_TMP FROM '"+@PathFileName+"' WITH (CODEPAGE='ACP', FIELDTERMINATOR = ';',ROWTERMINATOR = '\n') "
 END

EXEC (@SQL)

UPDATE dbo.VEC_Fakturalinjer
SET dbo.VEC_Fakturalinjer.Løpenummer = dbo.VEC_Fakturalinjer_TMP.Løpenummer,
dbo.VEC_Fakturalinjer.Vectura_materialnummer = dbo.VEC_Fakturalinjer_TMP.Vectura_materialnummer,
dbo.VEC_Fakturalinjer.Fakturert_innpris = CAST(dbo.VEC_Fakturalinjer_TMP.Fakturert_innpris AS NUMERIC(12,1)),
dbo.VEC_Fakturalinjer.Enhetspris = CAST(dbo.VEC_Fakturalinjer_TMP.Enhetspris AS NUMERIC(12,2)),
dbo.VEC_Fakturalinjer.Enhet = dbo.VEC_Fakturalinjer_TMP.Enhet,
dbo.VEC_Fakturalinjer.Fakturert_kvantum = CAST(dbo.VEC_Fakturalinjer_TMP.Fakturert_kvantum AS NUMERIC(28,2)),
dbo.VEC_Fakturalinjer.Fakturert_volum = CAST(dbo.VEC_Fakturalinjer_TMP.Fakturert_volum AS NUMERIC(28,2)),
dbo.VEC_Fakturalinjer.Vectura_kundenummer = dbo.VEC_Fakturalinjer_TMP.Vectura_kundenummer,
dbo.VEC_Fakturalinjer.Vectura_kundenavn = dbo.VEC_Fakturalinjer_TMP.Vectura_kundenavn,
dbo.VEC_Fakturalinjer.Konteringsdato = CAST(dbo.VEC_Fakturalinjer_TMP.Konteringsdato AS DATE),
dbo.VEC_Fakturalinjer.Vectura_bilagsnummer = dbo.VEC_Fakturalinjer_TMP.Vectura_bilagsnummer,
dbo.VEC_Fakturalinjer.EAN = dbo.VEC_Fakturalinjer_TMP.EAN,
dbo.VEC_Fakturalinjer.Kodefirma = CAST(dbo.VEC_Fakturalinjer_TMP.Kodefirma AS INT)


FROM dbo.VEC_Fakturalinjer_TMP
INNER JOIN dbo.VEC_Fakturalinjer
   ON dbo.VEC_Fakturalinjer_TMP.Løpenummer = dbo.VEC_Fakturalinjer.Løpenummer

INSERT INTO dbo.VEC_Fakturalinjer (Løpenummer,Vectura_materialnummer, 
Fakturert_innpris,Enhetspris,Enhet,Fakturert_kvantum,Fakturert_volum,Vectura_kundenummer,Vectura_kundenavn,Konteringsdato,Vectura_bilagsnummer,EAN,Kodefirma)

SELECT dbo.VEC_Fakturalinjer_TMP.Løpenummer,dbo.VEC_Fakturalinjer_TMP.Vectura_materialnummer, 
CAST(dbo.VEC_Fakturalinjer_TMP.Fakturert_innpris AS NUMERIC(12,1)),CAST(dbo.VEC_Fakturalinjer_TMP.Enhetspris AS NUMERIC(12,2)),
dbo.VEC_Fakturalinjer_TMP.Enhet,CAST(dbo.VEC_Fakturalinjer_TMP.Fakturert_kvantum AS NUMERIC(28,2)),
CAST(dbo.VEC_Fakturalinjer_TMP.Fakturert_volum AS NUMERIC(28,2)),
dbo.VEC_Fakturalinjer_TMP.Vectura_kundenummer,dbo.VEC_Fakturalinjer_TMP.Vectura_kundenavn,
CAST(dbo.VEC_Fakturalinjer_TMP.Konteringsdato AS DATE),dbo.VEC_Fakturalinjer_TMP.Vectura_bilagsnummer,
dbo.VEC_Fakturalinjer_TMP.EAN,CAST(dbo.VEC_Fakturalinjer_TMP.Kodefirma AS INT)

FROM dbo.VEC_Fakturalinjer_TMP
LEFT OUTER JOIN dbo.VEC_Fakturalinjer
   ON dbo.VEC_Fakturalinjer_TMP.Løpenummer = dbo.VEC_Fakturalinjer.Løpenummer
WHERE dbo.VEC_Fakturalinjer.Løpenummer IS NULL   

Open in new window


And when i import this file (2 records):

97232;103034;571,80;47,65;BOT;12,00;9,00;0000010280;A/S Vinmonopolet  Stjørdal;2012.07.19;0137916387;8410537050126;87
97233;108086;127,38;21,23;BOT;6,00;4,50;0000010120;A/S Vinmonopolet  City Syd;2012.07.19;0137912952;8000160673009;87

I get this error message :
(2 row(s) affected)

(0 row(s) affected)
Msg 8114, Level 16, State 5, Procedure VEC_Fakturaimport, Line 38
Error converting data type nvarchar to numeric.

Olso include Tabel definition for both tables:

CREATE TABLE
    VEC_Fakturalinjer
    (
        Løpenummer INT NOT NULL,
        Vectura_materialnummer NVARCHAR(20) COLLATE Danish_Norwegian_CI_AS,
        Fakturert_innpris NUMERIC(12,1),
        Enhetspris NUMERIC(12,2),
        Enhet NVARCHAR(3) COLLATE Danish_Norwegian_CI_AS,
        Fakturert_kvantum NUMERIC(28,2) DEFAULT 0 NOT NULL,
        Fakturert_volum NUMERIC(28,2) DEFAULT 0 NOT NULL,
        Vectura_kundenummer NVARCHAR(40) COLLATE Danish_Norwegian_CI_AS,
        Vectura_kundenavn NVARCHAR(40) COLLATE Danish_Norwegian_CI_AS,
        Konteringsdato DATE,
        Vectura_bilagsnummer NVARCHAR(40) COLLATE Danish_Norwegian_CI_AS,
        EAN NVARCHAR(30) COLLATE Danish_Norwegian_CI_AS,
        Kodefirma INT,
        VareiAX INT NOT NULL
    )

Open in new window


CREATE TABLE
    VEC_Fakturalinjer_TMP
    (
        Løpenummer INT NOT NULL,
        Vectura_materialnummer NVARCHAR(20) COLLATE Danish_Norwegian_CI_AS,
        Fakturert_innpris NVARCHAR(20) COLLATE Danish_Norwegian_CI_AS,
        Enhetspris NVARCHAR(20) COLLATE Danish_Norwegian_CI_AS,
        Enhet NVARCHAR(5) COLLATE Danish_Norwegian_CI_AS,
        Fakturert_kvantum NVARCHAR(30) COLLATE Danish_Norwegian_CI_AS,
        Fakturert_volum NVARCHAR(30) COLLATE Danish_Norwegian_CI_AS,
        Vectura_kundenummer NVARCHAR(30) COLLATE Danish_Norwegian_CI_AS,
        Vectura_kundenavn NVARCHAR(40) COLLATE Danish_Norwegian_CI_AS,
        Konteringsdato NVARCHAR(40) COLLATE Danish_Norwegian_CI_AS,
        Vectura_bilagsnummer NVARCHAR(40) COLLATE Danish_Norwegian_CI_AS,
        EAN NVARCHAR(30) COLLATE Danish_Norwegian_CI_AS,
        Kodefirma NVARCHAR(20) COLLATE Danish_Norwegian_CI_AS
    )

What is wrong ?

Open in new window

0
Comment
Question by:team2005
  • 2
  • 2
5 Comments
 
LVL 13

Expert Comment

by:LIONKING
ID: 38300481
The problem is that your numerical values are using a comma (,) as a decimal separator. So in order for this to work, you need to change that into a period (.).

So your file instead of saying something like 571,80 should say 571.80

Hope it helps!

Info: http://social.msdn.microsoft.com/Forums/en/transactsql/thread/57823668-afa4-4017-9a45-f6f4b034858e
0
 
LVL 66

Expert Comment

by:Jim Horn
ID: 38300486
Highlight for us which line is Line 38 in your script.
0
 
LVL 2

Author Comment

by:team2005
ID: 38303882
Hi!

From a texteditor, this is line 38:
INSERT INTO dbo.VEC_Fakturalinjer (Løpenummer,Vectura_materialnummer,

This error driving me crazy :(
0
 
LVL 13

Accepted Solution

by:
LIONKING earned 2000 total points
ID: 38304699
Have you tried what I told you?

Give this a shot:

SET QUOTED_IDENTIFIER OFF

go
CREATE PROCEDURE VEC_Fakturaimport
@PathFileName varchar(100),
@FileType int
AS

DECLARE @SQL varchar(2000)
IF @FileType = 1
 BEGIN
  SET @SQL = "BULK INSERT dbo.VEC_Fakturalinjer_TMP FROM '"+@PathFileName+"' WITH (CODEPAGE='ACP', FIELDTERMINATOR = '"";""') "
 END
ELSE
 BEGIN
  SET @SQL = "BULK INSERT dbo.VEC_Fakturalinjer_TMP FROM '"+@PathFileName+"' WITH (CODEPAGE='ACP', FIELDTERMINATOR = ';',ROWTERMINATOR = '\n') "
 END

EXEC (@SQL)

UPDATE dbo.VEC_Fakturalinjer
SET dbo.VEC_Fakturalinjer.Løpenummer = dbo.VEC_Fakturalinjer_TMP.Løpenummer,
dbo.VEC_Fakturalinjer.Vectura_materialnummer = dbo.VEC_Fakturalinjer_TMP.Vectura_materialnummer,
dbo.VEC_Fakturalinjer.Fakturert_innpris = CAST(REPLACE(dbo.VEC_Fakturalinjer_TMP.Fakturert_innpris,',','.') AS NUMERIC(12,1)),
dbo.VEC_Fakturalinjer.Enhetspris = CAST(REPLACE(dbo.VEC_Fakturalinjer_TMP.Enhetspris, ',','.') AS NUMERIC(12,2)),
dbo.VEC_Fakturalinjer.Enhet = dbo.VEC_Fakturalinjer_TMP.Enhet,
dbo.VEC_Fakturalinjer.Fakturert_kvantum = CAST(REPLACE(dbo.VEC_Fakturalinjer_TMP.Fakturert_kvantum,',','.') AS NUMERIC(28,2)),
dbo.VEC_Fakturalinjer.Fakturert_volum = CAST(REPLACE(dbo.VEC_Fakturalinjer_TMP.Fakturert_volum,',','.') AS NUMERIC(28,2)),
dbo.VEC_Fakturalinjer.Vectura_kundenummer = dbo.VEC_Fakturalinjer_TMP.Vectura_kundenummer,
dbo.VEC_Fakturalinjer.Vectura_kundenavn = dbo.VEC_Fakturalinjer_TMP.Vectura_kundenavn,
dbo.VEC_Fakturalinjer.Konteringsdato = CAST(dbo.VEC_Fakturalinjer_TMP.Konteringsdato AS DATE),
dbo.VEC_Fakturalinjer.Vectura_bilagsnummer = dbo.VEC_Fakturalinjer_TMP.Vectura_bilagsnummer,
dbo.VEC_Fakturalinjer.EAN = dbo.VEC_Fakturalinjer_TMP.EAN,
dbo.VEC_Fakturalinjer.Kodefirma = CAST(dbo.VEC_Fakturalinjer_TMP.Kodefirma AS INT)


FROM dbo.VEC_Fakturalinjer_TMP
INNER JOIN dbo.VEC_Fakturalinjer
   ON dbo.VEC_Fakturalinjer_TMP.Løpenummer = dbo.VEC_Fakturalinjer.Løpenummer

INSERT INTO dbo.VEC_Fakturalinjer (Løpenummer,Vectura_materialnummer, 
Fakturert_innpris,Enhetspris,Enhet,Fakturert_kvantum,Fakturert_volum,Vectura_kundenummer,Vectura_kundenavn,Konteringsdato,Vectura_bilagsnummer,EAN,Kodefirma)

SELECT dbo.VEC_Fakturalinjer_TMP.Løpenummer,dbo.VEC_Fakturalinjer_TMP.Vectura_materialnummer, 
CAST(REPLACE(dbo.VEC_Fakturalinjer_TMP.Fakturert_innpris,',','.') AS NUMERIC(12,1)),CAST(REPLACE(dbo.VEC_Fakturalinjer_TMP.Enhetspris, ',','.') AS NUMERIC(12,2)),
dbo.VEC_Fakturalinjer_TMP.Enhet,CAST(REPLACE(dbo.VEC_Fakturalinjer_TMP.Fakturert_kvantum,',','.') AS NUMERIC(28,2)),
CAST(REPLACE(dbo.VEC_Fakturalinjer_TMP.Fakturert_volum,',','.') AS NUMERIC(28,2)),
dbo.VEC_Fakturalinjer_TMP.Vectura_kundenummer,dbo.VEC_Fakturalinjer_TMP.Vectura_kundenavn,
CAST(dbo.VEC_Fakturalinjer_TMP.Konteringsdato AS DATE),dbo.VEC_Fakturalinjer_TMP.Vectura_bilagsnummer,
dbo.VEC_Fakturalinjer_TMP.EAN,CAST(dbo.VEC_Fakturalinjer_TMP.Kodefirma AS INT)

FROM dbo.VEC_Fakturalinjer_TMP
LEFT OUTER JOIN dbo.VEC_Fakturalinjer
   ON dbo.VEC_Fakturalinjer_TMP.Løpenummer = dbo.VEC_Fakturalinjer.Løpenummer
WHERE dbo.VEC_Fakturalinjer.Løpenummer IS NULL   

Open in new window

0
 
LVL 2

Author Closing Comment

by:team2005
ID: 38310705
Thanks
0

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
Windocks is an independent port of Docker's open source to Windows.   This article introduces the use of SQL Server in containers, with integrated support of SQL Server database cloning.
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.
Suggested Courses

873 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question