Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
Problem with integer conversion
Message
 
 
À
11/09/2013 18:51:21
Information générale
Forum:
Microsoft SQL Server
Catégorie:
Syntaxe SQL
Versions des environnements
SQL Server:
SQL Server 6.5 and older
Application:
Web
Divers
Thread ID:
01582916
Message ID:
01582924
Vues:
36
>Hi all -
>
>I am really puzzling over this one. This code
>
>oCommand.CommandText = "INSERT INTO [TIP_AmendmentApprovedFunding] (AmendmentNumber, TipID, Amount, Fundtype, Year, Phase) "
>oCommand.CommandText += "SELECT @AmendmentNumber, TipID, Amount, Fundtype, Year, Phase "
>oCommand.CommandText += "FROM [TIP_ApprovedFunding] WHERE (TipID = @TipID and year > @AmendmentNumber-1 and year < @AmendmentNumber+4)"
>oCommand.ExecuteNonQuery()
>
has been working fine for months. Today all of a sudden it no longer works. With a value of 2014.05 for the @AmendmentNumber, it crashes with an error that says it can't convert the varchar value '2014.05' to an integer. Why it wants to convert it to an integer is beyond me. The field it's going into is a number(7,2) field.
>So anyway, I thought I would help it along so I changed the code to this
>
>oCommand.CommandText = "INSERT INTO [TIP_AmendmentApprovedFunding] (AmendmentNumber, TipID, Amount, Fundtype, Year, Phase) "
>oCommand.CommandText += "SELECT @AmendmentNumber, TipID, Amount, Fundtype, Year, Phase "
>oCommand.CommandText += "FROM [TIP_ApprovedFunding] WHERE (TipID = @TipID and year between convert(integer, @AmendmentNumber) and convert(integer, @AmendmentNumber + 4))"
>oCommand.ExecuteNonQuery()
>
>but it renders the same error. The parameter value is set like this
>
>oCommand.Parameters.AddWithValue("@AmendmentNumber", Amendments_DDL.SelectedValue)
>
>Any thoughts?
>Thanks

1. Since year is a reserved word, you need to put it in brackets, e.g. [year]

2. What is the type of the [year] column?

3. Don't use AddWithValue, use Add method instead like this
oCommand.Parameters.Add("@AmendementNumber", SqlDbType.VarChar, 10).Value = Amendments_DDL.SelectedValue
I assume that [year] is varchar in the table. Most likely it's Char 4, so use this then instead.
If it's not broken, fix it until it is.


My Blog
Précédent
Répondre
Fil
Voir

Click here to load this message in the networking platform