1

How can I insert a numeric variable in a statement from within a stored procedure (here is my attempt):

...
declare @SQL1 nvarchar(1500)
declare @calc numeric(18,2)
Set @calc = 18.00
SET @SQL1= 'insert into Table1(cv) values(' + @calc + ')' 

The error I get on the last line is:

Error converting data type varchar to numeric.

If I insert a variable of type nvarchar like this:

SET @SQL1= 'insert into Table1(cn) values(''' + @column_name + ''')' 

it works like a charm - but not when I try to insert a numeric field as above in the first example. Thank you!

PS: The table Table1 was correctly defined for its columns to match the variable types. Table1 has 2 columns: cn of type nvarchar(50) and cv of type numeric(18,2). @column_name is nvarchar(50) and @calc is numeric(18,2).

1
  • One query has cn as a field, the other cv. Which is right, and what are their data types? Commented Jan 5, 2013 at 11:45

1 Answer 1

2

You need to cast your numeric to a nvarchar - try this:

declare @SQL1 nvarchar(1500)
declare @calc numeric(18,2)

Set @calc = 18.00

SET @SQL1 = N'insert into Table1(cv) values(' + CAST(@calc AS NVARCHAR(10)) + N')' 
Sign up to request clarification or add additional context in comments.

1 Comment

Great, worked like a charm, thank you. I had the funny feeling Sql was automatically converting to nvarchar and the statement was expecting a number to match the 'cv' column type. I'm not sure why this happens but glad to apply your solution.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.