I have a utility that imports Excel spreadsheets to SQL Server. My utility broke recently when I had to convert some fields from nvarchar to decimal.
I use c# code to call a stored procedure. My c# code:
DataTable dtChem = new DataTable();
ada.Fill(dtChem); // Fills datatable with data from Excel
cmd.Parameters.Clear();
cmd.CommandText = "dbo.insertDataChem";
cmd.Parameters.AddWithValue("@chemResults", dtChem);
cmd.Parameters.AddWithValue("@eddName", strFileName);
cmd.ExecuteNonQuery();
My stored procedure:
create PROCEDURE [dbo].[insertDataChem]
@chemResults as dbo.udtableDataChem READONLY
, @eddName nvarchar(100) = null
AS
BEGIN
INSERT INTO MY_TABLE
SELECT cr.SampleDate, cr.*, @eddName, CURRENT_TIMESTAMP FROM @chemResults cr
END
And I use the following user-defined table type to store my data (I've removed a lot of fields for brevity):
CREATE TYPE [dbo].[udtableDataChem] AS TABLE(
...
[MDL] [decimal](30, 15) NULL, // data type changed
[RL] [decimal](30, 15) NULL, // data type changed
[DilutionFactor] [nvarchar](200) NULL, // original data type
[ExpectedValue] [nvarchar](200) NULL, // original data type
...
)
In my data table type, above, all of my types used to be nvarchar. When I changed the data types in the destination table to decimal(30, 15), I thought I could just change the data types in the corresponding user-defined table type to decimal as well, and that would force the data to be compatible. But my c# error trapping is still telling me "Error converting data type nvarchar to numeric.".
Any help would be appreciated.