Re: Possible Type Conversion Defect
- From: Eric Molitor <EricMolitor@xxxxxxxxxxxxxxxxxxxxxxxxx>
- Date: Wed, 19 Oct 2005 14:49:06 -0700
Right, I dug into this and was able to solve the problems both with
conversions and by fixing some bad practices in our SQL...
In several places after executing an insert we would simply...
select @@IDENTITY as identityValue
and then retrieve the value from the result set in java. Obviously we should
have been using SCOPE_IDENTITY() for one but also we should have been using
an out put parameter...
So the proc becomes
CREATE PROCEDURE spTestProc
(
@username varchar(8),
@firstname varchar(255),
@lastname varchar(255),
@UserID int OUTPUT
) as
INSERT INTO User (username, firstname, lastname)
VALUES (@username, @firstname, @lastname)
SELECT @UserID = SCOPE_IDENTITY()
and in the code we just fetch the value as an output parameter...
So once all of this shakes out I do think it will be a positive change,
however I'm sure lots of people will have some SQL and Java to cleanup.
Cheers,
Eric
"Angel Saenz-Badillos[MS]" wrote:
> We wanted to be very explicit with our data coercion story and as far as we
> have been able we are not going to allow getting a type from the server that
> would require a downcast to the client and possible loss of data. This
> strategy has the advantage of high predictability with limited chance of
> data loss, but it is very restrictive.
>
> Quite frankly I was expecting to see a lot more people commenting on these.
> In your case 108 is of type NUMERIC, a 38bit precission decimal and you are
> trying to shove it into an INTEGER. Type 38 is an INTEGER which does not fit
> on a SMALLINT.
>
> We only have two choices here that don't involve data loss (something we are
> definitelly not going to allow),
> 1) We can NEVER allow a conversion from a type if _the type you are trying
> to convert_ does not fit into the type that you are trying to coerce it
> into. This is the behavior that we have opted for in the 2005 JDBC driver.
>
> 2) We can allow a conversion from a type that does not fit into the coreced
> type _only_ when the current value that you are asking for can be coerced
> into the type that you are asking for. This is the behavior of the 2000 JDBC
> driver.
>
> Let's say that you have a NUMERIC column that has a value of 5, when you
> call getInt on this we will throw an exception if following (1) but the
> coercion will work on a driver that supports (2) since 5 does fit into an
> INTEGER type. When you have a driver that provides the (1) functionality you
> will realize the first time you run your code that a NUMERIC column will not
> always fit into an int and change your code accordingly. When working with a
> driver of the (2) type you will test and deploy your application with
> getInt. When the value of the NUMERIC column goes over what an INTEGER can
> handle you will get a runtime exception and you will have to go service your
> deployed application.
>
> We realize that it can be inconvenient to have this kind of issues surfaced
> early, but we feel it is better to let you know up front about possible data
> coercion issues, if you really wanted to get an integer from the server you
> would have defined your table accordingly, or you could have requested an
> integer in your query with the CONVERT function.
>
> I think that this is going to be a common question, I am going to convert
> this post into a blog and post it into the http://blogs.msdn.com/dataaccess/
> with a complete data coercion table to help make this design clearer, of
> course comments/suggestions are welcome.
> --
> Angel Saenz-Badillos [MS] DataWorks
> This posting is provided "AS IS", with no warranties, and confers no
> rights.Please do not send email directly to this alias.
> This alias is for newsgroup purposes only.
> I am now blogging: http://weblogs.asp.net/angelsb/
>
>
>
>
> "Eric Molitor" <EricMolitor@xxxxxxxxxxxxxxxxxxxxxxxxx> wrote in message
> news:237EAB6D-63BA-4DB2-AB0C-7EB8D98B3E2D@xxxxxxxxxxxxxxxx
> > Seem to be getting these consistantly in certain portions of our
> > application.
> > Works fine with the older JDBC driver (2000) but under the 2005 driver we
> > see....
> >
> > com.microsoft.sqlserver.jdbc.SQLServerException: Unsupported conversion
> > from
> > 108 to INTEGER
> > at com.microsoft.sqlserver.jdbc.SQLServerStatement.getRowsetField(Unknown
> > Source)
> > at com.microsoft.sqlserver.jdbc.SQLServerResultSet.getInt(Unknown Source)
> > at com.microsoft.sqlserver.jdbc.SQLServerResultSet.getInt(Unknown Source)
> >
> > and
> >
> > com.microsoft.sqlserver.jdbc.SQLServerException: Unsupported conversion
> > from
> > 38 to SMALLINT
> > at com.microsoft.sqlserver.jdbc.SQLServerStatement.getRowsetField(Unknown
> > Source)
> > at com.microsoft.sqlserver.jdbc.SQLServerResultSet.getShort(Unknown
> > Source)
> > at com.microsoft.sqlserver.jdbc.SQLServerResultSet.getShort(Unknown
> > Source)
> >
> > I'm looking at the SQL and the code but I'd think 38 is a valid SMALLINT
> > and
> > that 108 is a valid INTEGER.
> >
> >
>
>
>
.
- Follow-Ups:
- Re: Possible Type Conversion Defect
- From: Angel Saenz-Badillos[MS]
- Re: Possible Type Conversion Defect
- References:
- Re: Possible Type Conversion Defect
- From: Angel Saenz-Badillos[MS]
- Re: Possible Type Conversion Defect
- Prev by Date: Re: Possible Type Conversion Defect
- Next by Date: Re: Possible Type Conversion Defect
- Previous by thread: Re: Possible Type Conversion Defect
- Next by thread: Re: Possible Type Conversion Defect
- Index(es):
Relevant Pages
|