Showing posts with label source. Show all posts
Showing posts with label source. Show all posts

Wednesday, March 28, 2012

rowguid

After replication done, both source database and subscription database have
a extra column--rowguid.
SQL server did not clean them up.
Should I clean then one table by one table manually or there is a was to
clean them up.
Thanks
After replication done, both source database and subscription database have
a extra column--rowguid.
SQL server did not clean them up.
I delete the publication. but the column rowguid still there
Do I have to clean them one table by one table manually or there is a way
to
clean them up.
Thanks
|||This is quite normal behaviour. It may be that a (DBA) user's application
relies on these columns in some way, so it is left up to the user to remove
them. There are no inbuilt stored procs to remove these columns, but
creating yourself a script shouldn't be too difficult - syscolumns and
sysobjects are the tables you'll need. If it's just a cast of a couple of
tables I'd do it manually, but if not or if you want more of a challenge,
have a go at the script and post back if you can't do it and I'll create one
for you.
Cheers,
Paul Ibison SQL Server MVP, www.replicationanswers.com
(recommended sql server 2000 replication book:
http://www.nwsu.com/0974973602p.html)
|||I have hundreds tables.
It will appreciate very much if you can post a sample script for this.
Thanks
"Paul Ibison" <Paul.Ibison@.Pygmalion.Com> wrote in message
news:ug15GiVvFHA.720@.TK2MSFTNGP15.phx.gbl...
> This is quite normal behaviour. It may be that a (DBA) user's application
> relies on these columns in some way, so it is left up to the user to
remove
> them. There are no inbuilt stored procs to remove these columns, but
> creating yourself a script shouldn't be too difficult - syscolumns and
> sysobjects are the tables you'll need. If it's just a cast of a couple of
> tables I'd do it manually, but if not or if you want more of a challenge,
> have a go at the script and post back if you can't do it and I'll create
one
> for you.
> Cheers,
> Paul Ibison SQL Server MVP, www.replicationanswers.com
> (recommended sql server 2000 replication book:
> http://www.nwsu.com/0974973602p.html)
>
>
|||Chen,
I'll post it up tomorrow - what type of replication are you using?
Cheers,
Paul Ibison SQL Server MVP, www.replicationanswers.com
(recommended sql server 2000 replication book:
http://www.nwsu.com/0974973602p.html)
|||Thanks.
I did merge replication and I deleted the publication days ago.
"Paul Ibison" <Paul.Ibison@.Pygmalion.Com> wrote in message
news:eGxuc$VvFHA.2076@.TK2MSFTNGP14.phx.gbl...
> Chen,
> I'll post it up tomorrow - what type of replication are you using?
> Cheers,
> Paul Ibison SQL Server MVP, www.replicationanswers.com
> (recommended sql server 2000 replication book:
> http://www.nwsu.com/0974973602p.html)
>
>
>
>
|||Chen,
run each of these in turn and then run the output (backup your database
first )
-- drop rowguid indexes
select 'drop index ' + sysobjects.name + '.' + sysindexes.name from
sysindexes
inner join sysobjects
on sysindexes.id = sysobjects.id
where objectproperty(object_id(sysobjects.name),'IsMSShi pped') = 0
and sysindexes.indid > 0 and sysindexes.indid < 255 and (sysindexes.status &
64)=0
and index_col(sysobjects.name, sysindexes.indid, 1) = 'rowguid'
order by sysindexes.indid
-- remove rowguid default constraints
select 'alter table ' + b.name + ' drop constraint ' + a.name from
sysobjects a
inner join syscolumns on syscolumns.id = a.parent_obj
inner join sysobjects b on syscolumns.id = b.id
where syscolumns.name = 'rowguid'
and objectproperty(object_id(b.name),'IsMSShipped') = 0
and a.xtype = 'D'
-- remove rowguid columns
select 'alter table ' + sysobjects.name + ' drop column ''rowguid'' ' from
syscolumns
inner join sysobjects on syscolumns.id = sysobjects.id
where syscolumns.name = 'rowguid'
and objectproperty(object_id(sysobjects.name),'IsMSShi pped') = 0
Cheers,
Paul Ibison SQL Server MVP, www.replicationanswers.com
(recommended sql server 2000 replication book:
http://www.nwsu.com/0974973602p.html)
|||Thanks
Chen
"Paul Ibison" wrote:

> Chen,
> run each of these in turn and then run the output (backup your database
> first )
> -- drop rowguid indexes
> select 'drop index ' + sysobjects.name + '.' + sysindexes.name from
> sysindexes
> inner join sysobjects
> on sysindexes.id = sysobjects.id
> where objectproperty(object_id(sysobjects.name),'IsMSShi pped') = 0
> and sysindexes.indid > 0 and sysindexes.indid < 255 and (sysindexes.status &
> 64)=0
> and index_col(sysobjects.name, sysindexes.indid, 1) = 'rowguid'
> order by sysindexes.indid
> -- remove rowguid default constraints
> select 'alter table ' + b.name + ' drop constraint ' + a.name from
> sysobjects a
> inner join syscolumns on syscolumns.id = a.parent_obj
> inner join sysobjects b on syscolumns.id = b.id
> where syscolumns.name = 'rowguid'
> and objectproperty(object_id(b.name),'IsMSShipped') = 0
> and a.xtype = 'D'
> -- remove rowguid columns
> select 'alter table ' + sysobjects.name + ' drop column ''rowguid'' ' from
> syscolumns
> inner join sysobjects on syscolumns.id = sysobjects.id
> where syscolumns.name = 'rowguid'
> and objectproperty(object_id(sysobjects.name),'IsMSShi pped') = 0
> Cheers,
> Paul Ibison SQL Server MVP, www.replicationanswers.com
> (recommended sql server 2000 replication book:
> http://www.nwsu.com/0974973602p.html)
>
>

Monday, March 26, 2012

RowCount

Hi,

want to get the number of rows i'm retrieving from a source. This count should be written as " No: of roes retrieved" + varname

I have used OleDbSource, RowCount,Script [ To write in a file ]. Rows is the package level variable name used in rowcount. when i do this way it always writes as 0 in the file.

[code in Script]

Dim sw As New StreamWriter("D:\Vijay1.txt")

s = Variables.Rows

sw.WriteLine(s.ToString)

sw.close

[/Code]

Can anyone help on this

you should store the row count in an ssis variable, then retreive the value from the script...|||

Hi,

I have done the same way. you can see in the code i have added. Rows is the package level variable I have used. In script I used Variables.Rows to access the value. when i write into a file it rights as 0

Thanks

|||

Can you try as follows:

Dim sw As New StreamWriter("D:\Vijay1.txt")

sw.WriteLine(Dts.Variables("Rows").Value.ToString())

sw.close()

Thanks,
Loonysan

|||

ManjuVijay wrote:

Hi,

I have done the same way. you can see in the code i have added. Rows is the package level variable I have used. In script I used Variables.Rows to access the value. when i write into a file it rights as 0

Thanks

the code should be:

s = Dts.Variables("Rows").Value

|||

Hi,

I am getting error saying DTS is not declared.

Thanks

|||

Hi,

I want to know whether i'm missing anyother thing.

I beleive I have to set only the variable name in RowCount. Any thing else I have to do?

|||

If i use script task it works properly

why i am not able to do so in script transform component

|||

The Script Task and Script Component are very different beasts. It is wrong to assume that because you can do something in one then you can also do the same in the other.

Its also true to say there are different ways of doing the same thing. For example, the syntax for accessing variables in the script component is different to that for accessig them in the script task.

What exactly are you unable to do?

-Jamie

|||> If i use script task it works properly

>why i am not able to do so in script transform component

The script component is used within a DataFlow task. The DataFlow task "snapshots" a variable value when it begins execution and cannot modify the variable until it has completed.

So, your row count = 0 at the beginning of execution. Your script component accesses the "snapshot" value and writes out 0.

The RowCount component only updates the row count variable, when execution of the data flow has completed. Your script task accesses the value after this and writes out the final rowcount.

Why does SSIS snapshot variable values? Well imagine a conditional split where the data is split on a variable value. If that could change during execution of a data flow, the behaviour of the split would be unpredictable - rows would be directed depending on whether they just happened to reach the split before or after the variable changed.

Donald

sql

Wednesday, March 21, 2012

Row numbering unpredictable

Hi,
I need to create a stored procedure that returns the row number (for
paging) AFTER the data has been sorted with an order by. The source is
a view. The code I have is:
SELECT rownum = IDENTITY(1,1,bigint), *
INTO #tmp
FROM viewName
ORDER BY CustomerName -- field name I'm ordering by
When I recieve the results back, the rownum column is not the same
order as the customername (it jumps half way to a high number?!?),
which means I can't page it based on rownum without jumping all over
the dataset.
Anyone got any ideas on how to solve that other than client side paging
(in ADO :-P)
This is SQL 2000 SP3 (pah!)
Cheers,
Chris Smith
http://www.cswd.co.uk/Assuming CustomerName is unique:
select
(select count (*)
from #tmp t1
where t1.CustomerName <= t2.CustomerName) as rownum
, *
from
#tmp t2
order by
t2.CustomerName
Tom
----
Thomas A. Moreau, BSc, PhD, MCSE, MCDBA
SQL Server MVP
Toronto, ON Canada
.
<cseemeuk@.googlemail.com> wrote in message
news:1144755017.455143.6760@.v46g2000cwv.googlegroups.com...
Hi,
I need to create a stored procedure that returns the row number (for
paging) AFTER the data has been sorted with an order by. The source is
a view. The code I have is:
SELECT rownum = IDENTITY(1,1,bigint), *
INTO #tmp
FROM viewName
ORDER BY CustomerName -- field name I'm ordering by
When I recieve the results back, the rownum column is not the same
order as the customername (it jumps half way to a high number?!?),
which means I can't page it based on rownum without jumping all over
the dataset.
Anyone got any ideas on how to solve that other than client side paging
(in ADO :-P)
This is SQL 2000 SP3 (pah!)
Cheers,
Chris Smith
http://www.cswd.co.uk/|||you could create the table first with an ID column, then insert into
it. I suspect (though have no evidence) that the select into #tmp with
an id column created then is having issues with the order by|||(cseemeuk@.googlemail.com) writes:
> I need to create a stored procedure that returns the row number (for
> paging) AFTER the data has been sorted with an order by. The source is
> a view. The code I have is:
> SELECT rownum = IDENTITY(1,1,bigint), *
> INTO #tmp
> FROM viewName
> ORDER BY CustomerName -- field name I'm ordering by
> When I recieve the results back, the rownum column is not the same
> order as the customername (it jumps half way to a high number?!?),
> which means I can't page it based on rownum without jumping all over
> the dataset.
> Anyone got any ideas on how to solve that other than client side paging
Create the table with CREATE TABLE, and then use INSERT with SELECT ORDER
BY. Add OPTION (MAXDOP 1) as an extra precaution. I've been told from MS
people that it's guaranteed to work. Whether that really is true, I'm not
completely convinced of, but fairly. In any case, SELECT INTO is *not*
guaranteed to work that way, so stay away from it.
Erland Sommarskog, SQL Server MVP, esquel@.sommarskog.se
Books Online for SQL Server 2005 at
http://www.microsoft.com/technet/pr...oads/books.mspx
Books Online for SQL Server 2000 at
http://www.microsoft.com/sql/prodin...ions/books.mspx|||Thanks - works perfectly. The INTO was the problem - appears to be no
guaranteed order to the IDENTITY(bigint, 1,1)
All sorted
Cheers,
Chris Smith
http://www.cswd.co.uk/|||The order is not guaranteed when you use SELECT INTO.
See
http://support.microsoft.com/defaul...kb;en-us;273586
For a list of paging options see
http://www.aspfaq.com/show.asp?id=2120
Regards
Roji. P. Thomas
http://toponewithties.blogspot.com
<cseemeuk@.googlemail.com> wrote in message
news:1144755017.455143.6760@.v46g2000cwv.googlegroups.com...
> Hi,
> I need to create a stored procedure that returns the row number (for
> paging) AFTER the data has been sorted with an order by. The source is
> a view. The code I have is:
> SELECT rownum = IDENTITY(1,1,bigint), *
> INTO #tmp
> FROM viewName
> ORDER BY CustomerName -- field name I'm ordering by
> When I recieve the results back, the rownum column is not the same
> order as the customername (it jumps half way to a high number?!?),
> which means I can't page it based on rownum without jumping all over
> the dataset.
> Anyone got any ideas on how to solve that other than client side paging
> (in ADO :-P)
> This is SQL 2000 SP3 (pah!)
> Cheers,
> Chris Smith
> http://www.cswd.co.uk/
>|||One would think this type of thing,so common and important,
would have a kb or something written by MS.Are you aware of any
link?If none exists I would ask you to kindly request something in
'writing'.Key points of an enterprise database should not be rattling
around just in someone head! :)
Clarity,clarity and nothing but clarity.
Regards from:
www.rac4sql.net
"Erland Sommarskog" <esquel@.sommarskog.se> wrote in message
news:Xns97A28BF07D031Yazorman@.127.0.0.1...
> (cseemeuk@.googlemail.com) writes:
> Create the table with CREATE TABLE, and then use INSERT with SELECT ORDER
> BY. Add OPTION (MAXDOP 1) as an extra precaution. I've been told from MS
> people that it's guaranteed to work. Whether that really is true, I'm not
> completely convinced of, but fairly. In any case, SELECT INTO is *not*
> guaranteed to work that way, so stay away from it.
>
> --
> Erland Sommarskog, SQL Server MVP, esquel@.sommarskog.se
> Books Online for SQL Server 2005 at
> http://www.microsoft.com/technet/pr...oads/books.mspx
> Books Online for SQL Server 2000 at
> http://www.microsoft.com/sql/prodin...ions/books.mspx|||Steve Dassin wrote:
> One would think this type of thing,so common and important,
> would have a kb or something written by MS.Are you aware of any
> link?If none exists I would ask you to kindly request something in
> 'writing'.Key points of an enterprise database should not be rattling
> around just in someone head! :)
> Clarity,clarity and nothing but clarity.
http://support.microsoft.com/defaul...kb;en-us;273586
Do not assume that article means that all INSERTs will always cause
IDENTITY to be generated in a predetermined order. There are at least
some situations where that doesn't work - whether by design or a bug I
can't say.
Perhaps the safest course is to assume that you cannot control the
IDENTITY sequence with ORDER BY. In my view the wisest and most logical
solution is to use other methods like the ROW_NUMBER function for
example.
I can think of at least two good reasons for not using IDENTITY the way
proposed by the KB. Firstly IDENTITY is normally intended as an
arbitrary surrogate key - using the values in any "meaningful" way is a
compromise you don't need and is something it just isn't designed for.
Secondly, this supposed behaviour of an "ordered" INSERT looks contrary
to the set-based nature of an INSERT statement. Whether or not it works
today, it seems undesirable to assume that it should always work that
way in future. One would hope and expect that the engine could optimise
out any redundant sorting in INSERT...SELECT queries. That seems to be
what happens in some cases today and maybe it will happen more often in
future versions due to improvements in the optimiser. Just some things
to bear in mind.
David Portas, SQL Server MVP
Whenever possible please post enough code to reproduce your problem.
Including CREATE TABLE and INSERT statements usually helps.
State what version of SQL Server you are using and specify the content
of any error messages.
SQL Server Books Online:
http://msdn2.microsoft.com/library/ms130214(en-US,SQL.90).aspx
--|||>> Anyone got any ideas on how to solve that other than client side paging <
<
The basic principle of a tiered architecture is that display is done in
the front end adn NEVER in the database. Why are you sing violating
40 years of Software Engineering?|||"--CELKO--" <jcelko212@.earthlink.net> wrote in message
news:1144803667.948395.290650@.i40g2000cwc.googlegroups.com...
<<
> The basic principle of a tiered architecture is that display is done in
> the front end adn NEVER in the database. Why are you sing violating
> 40 years of Software Engineering?
>
Forty years of a life sentence is enough.Time to let the innocent free.
Convicted on trumped up,unsubstantiated and false charges.In other words,
NONSENSE.
The thread:
Monday, April 10, 2006 9:48 PM
microsoft.public.sqlserver.programming
Re: Membership Timeline Spanning
contains a response that further clarifies things:
"Itzik Ben-Gan" writes
>.
>In my previous reply I mentioned the ANSI OVER clause (with an ORDER BY
>option). It is really brilliant, and I wonder if the designers of the
>feature themselves knew how profound it is. I believe this option to be the
>bridge between cursors and sets; sort of the holy grail of SQL. :-)
To quote Bob Dylan:
'I would not feel so alone if everyone where getting stoned':)
Yes I agree with you in principal.The 'real' paradign shift has
little to do with the clr and everything to do with exploding
the perverted myth of the exclusivity of'set based' constructs.
The idea one can legitimately think in terms of rows without being
labelled an sql Jodus has arrived.But calling this windowing a
'profound' kind of insight and bestowing on the designers the aura
of 'brilliance' would be a mistake.It is at best an example of
'better late than never'.Calling this state of affairs profound
would surely overshadow the accountability that the commericial
database world should be held to.The fact that this mindset change
has taken almost 30 years should be seen as appalling.Neo-cons of
the industry had hijacked sense with sql creationism and marketing.
WMD was replaced with client/server and a tiered approach.A theory
was misapplied to a retrival mechanism and unapplied to a design
mechanism.An approach that vendors marketted that allowed them to
hide both their intellectual and creative shortcomings.Their db
failures made for the 'client'.And now the clr in the db has replaced
the client.And of course the dreaded cursor.This demanded regime
change and the field was bankrupted for 30 years.For this we are to
praise Ceasar?I think not.
It is interesting to look at the fanfare that vendors are using
to usher in this new paradign.In their documentation Oracle refers
to their analytic functions in windows as an example of
'data densification'.This phrase is supposed to illustrate the
flip side of the Group By.It was obviously borrowed from the idea
of pacification,right out of the Pentagon.This is the best they could
come up with?Any army of engineers berefit of language and concepts.
Not to be out done,MS in its highly touted BOL offers the next best
thing - absolutely Nothing!No explanations,no history no seqways.
The functions are thrown around like so much spaghetti on a wall.
If you write about concepts someone may quote you.MS needn't worry
now.Least I be accused of favortism,IBM was too busy pleasing its
shareholders to write anything intelligible.
Finally,to your point about MS leaving out a large chunk of analytic
material this was obviously not an oversight but just insurance
that anything done with sql-99 could most definitly be easily ported
to the competition.Less is more.Please!If they weren't sure of
what they were doing they could have at least looked at Oracle
which is probably about 8 years ahead.Or even looked at RAC to see what
you and I are really talking about :)
Interested readers maybe surprised that many of the ideas in sql
analytics can be found in the SAS (Statistical Analysis System) Data
Step...introduced about 20 years ago!Many of the Oracle extensions
(First/Last) can also be found here.MySql allows mixing of variables
and columns in a SELECT.Most of the analytics can be easily simulated
in a single SELECT.And of course little RAC, way ahead of its time:)
Some musing from:
www.rac4sql.net

Monday, March 12, 2012

Row delimiter {LF} is ignored?

Hi!

Im trying to import some data from a Flat File Source, where a row delimiter is {LF} and column separator is SPACE.

Data looks like this:
GI$0c2 T08b 1 1 20060508 000655 6 8 8 c0a81f1f 1 1 1 00A 3 24206816 3 24206816 1 1 3 59910000 001 1 3 14730050 0 25 F10 XX 317148-131136 loop TG_MRB 0 M027 1 3 0 20060508 000655 0 3 59910000 SIP

This is the first record that generates around 41 columns and sorts data as it should, but if the second record is smaller the row delimiter {LF} is ignored and put into one of the columns untill all 41 columns from previous record are filled. It seems to me that columns separator has the priority over the row delimiter which is very wrong. :). If there is a {LF} in the file that should mean that it needs to be in a new row as a new record. I try to keep this all in a SQL 2005 package without using any additional software, i know there might be a solution with the scripting component, but would like to see if theres someone with the similar solution before i start writing any scripts. (i dont like parsing strings with scripts from bulky files:))

Thanks!

Sebastijan L.

Is the file delimited by spaces or is it a fixed-column-width format?

When you say the second record is smaller" what do you mean? Does it have some missing columns? If so, which columns?

There may be some fairly easy ways to parse this, but they depend very much on exactly how you expect records to vary from row to row.

Donald Farmer

|||Thank you for a quick response Donald!

Records are delimited by spaces as shown in the data sample above. For example:
If my 1st row contains 41 columns that are space delimited (row ends with {LF}), and the second row is smaller and contains only enough data for 39 columns, it will fill up the rest of the 2 columns with the data that is already a third record, and in that data i can see a row delimiter which is in my case{LF} from the second row!!

What it should do is whenever there is a specified row delimiter in data it should create another record, and in that case it doesnt..

I can import the data within SQL 2000, Access, Excell with no problem, just that im trying to keep everything inside SQL2005 as some features are pretty good, without the need of 3rd software.

Sebastijan L.
|||

Thanks.

SSIS does not know that columns 40 and 41 are missing - perhaps it was columns 1 and 2, or columns 8 and 13. And it does not know that you do not expect the characters used as row delimiters embedded in the middle of a record - there are cases where that can happen.

Other applications do make assumptions about row delimiters - some of them read ahead to the row end, take the whole row from delimiter to delimiter into memory and parse it out column by column from there. That is good for your scenario, but slow, and unreliable where delimiters can be embedded.

We can - and most likely will at some point - build some more "smarts" into the flat file source to handle some of these situations, although users do need to be aware that every new conditional property we add will decrease the performance of the source adapter.

Meanwhile, you can indeed handle this situation using a script or even expressions in the derived column component. The best solution for you will depend on how complex the error handling has to be, for example, if you know what columns are likely to be missing it makes it much easier.

Donald Farmer

Wednesday, March 7, 2012

Rounding error: Between flat file connection manager Source & OLE DB Connection Destination

I have a Rounding error: Between flat file connection manager Source & OLE DB Connection Destination (SQL Server 2005) in my Dataflow.

File looks like this lets call column names Col A,B,C,D

70410000 RD1 1223631.92 196042.42
70329000 ICD 11025.84 3353.88
71167300 COL 104270.59 24676.96

flat file connection manager settings: first row Column names then Advanced tab Col A float , Col B float , Col C string ,Col D float ,

OLE DB Connection Destination (SQL Server 2005)

CREATE TABLE [dbo].[PT_CUST_ABR](

[PARTY_NO] [float] NULL,

[PARTY_NAME] [varchar](75) COLLATE SQL_Latin1_General_CP1_CI_AS NULL,

[TELECOMABR] [float] NULL,

[GENIABR] [float] NULL,

Problem: ColA (Source) Rounding error to PARTY_NO (Destination)

I have a field of text of in a flat file that the flat file connection manager Source picks up correctly “70000893”

However when it gets the OLE DB Connection Destination the data has changed to 70000896. That’s before its even Written to the database.

The only clue that something is wrong in the middle is the great Data viewer shows the number as 7.000009E+07

Other clues looking at the data it appears there is a rounding error on only the number that dont end in 00

ColA (Source) PARTY_NO (Destination)
71167300 71167296
70329000 70329000
70410000 70410000

Any ideas people?

Thanks in advance

Dave

Float type by definition is not precise, it holds about 7 decimal digits, so it is expected.

If you want to keep exact values, you should use types that are big enough to hold them. E.g. Int for ColA and Decimal for ColC & ColD.

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/tsqlref/ts_fa-fz_6r3g.asp

|||

Thanks Michael--already tried that --turned out to the XML file in SSIS had my old settings locked in and my new settings in the flat file connection manager Source did not take effect

I fixed the problem by changing the datatype to string in the flat file connection manager and bigint in the SQL Server 2005 table. However I needed to delete all the objects in the dataflow task and re-recreate them again.--this fixed the XML metadata problem

Dave

Rounding error when using UPDATE function

I am trying to discover the source of an error that occurs in some code
that I run regularly.
I have found a problem that when I try to update a table with a
calculated figure a rounding error occurs.
This problem can be shown by running the following code:
UPDATE NumericVal
SET ValExpected = 0.72139753801593054
WHERE NumericVal.ValItemCode = 2441 AND ValTPCode = 4159
SELECT ValExpected
FROM NumericVal
WHERE NumericVal.ValItemCode = 2441 AND ValTPCode = 4159
ValExpected returns the value 0.72139752, although it should be
identical to entered value which would round to 0.72139754.
I know it's only a small difference but it is causing greater errors in
later calculations.
Is there any explanation as to why this is happening and how can I
resolve the issue?What are the datatypes? Without knowing the datatypes all we an do is
speculate.
I speculate that you are using FLOAT, or REAL.
FLOAT and REAL are internally binary. They store data to the right of
the decimal point as binary fractions: 1/2, 1/4, 1/8, 1/16, 1/32, etc.
Just as the decimal system can not accurately store 1/3, the binary
system can not store some numbers that store accurately in the decimal
system. This is why REAL and FLOAT are described in the documentation
as: "Approximate number data types for use with floating point numeric
data. Floating point data is approximate; not all values in the data
type range can be precisely represented."
Roy Harvey
Beacon Falls, CT
On 28 Jun 2006 04:49:50 -0700, "AdamHCC"
<adam.roberts@.healthcarecommission.org.uk> wrote:

>I am trying to discover the source of an error that occurs in some code
>that I run regularly.
>I have found a problem that when I try to update a table with a
>calculated figure a rounding error occurs.
>This problem can be shown by running the following code:
>UPDATE NumericVal
>SET ValExpected = 0.72139753801593054
>WHERE NumericVal.ValItemCode = 2441 AND ValTPCode = 4159
>SELECT ValExpected
>FROM NumericVal
>WHERE NumericVal.ValItemCode = 2441 AND ValTPCode = 4159
>
>ValExpected returns the value 0.72139752, although it should be
>identical to entered value which would round to 0.72139754.
>I know it's only a small difference but it is causing greater errors in
>later calculations.
>Is there any explanation as to why this is happening and how can I
>resolve the issue?|||Adam
ValExpected has a DECIMAL /FLOAT/REAL datatype ?
"AdamHCC" <adam.roberts@.healthcarecommission.org.uk> wrote in message
news:1151495390.107042.281570@.d56g2000cwd.googlegroups.com...
>I am trying to discover the source of an error that occurs in some code
> that I run regularly.
> I have found a problem that when I try to update a table with a
> calculated figure a rounding error occurs.
> This problem can be shown by running the following code:
> UPDATE NumericVal
> SET ValExpected = 0.72139753801593054
> WHERE NumericVal.ValItemCode = 2441 AND ValTPCode = 4159
> SELECT ValExpected
> FROM NumericVal
> WHERE NumericVal.ValItemCode = 2441 AND ValTPCode = 4159
>
> ValExpected returns the value 0.72139752, although it should be
> identical to entered value which would round to 0.72139754.
> I know it's only a small difference but it is causing greater errors in
> later calculations.
> Is there any explanation as to why this is happening and how can I
> resolve the issue?
>|||It looks like ValExpected is declared to be of type REAL, which has limited
precision. To preserve the exact value, use a DECIMAL type with appropriate
precision and scale, or to keep about 15-16 decimal places of accuracy and
allow a wider range, use FLOAT instead of REAL.
Steve Kass
Drew University
AdamHCC wrote:

>I am trying to discover the source of an error that occurs in some code
>that I run regularly.
>I have found a problem that when I try to update a table with a
>calculated figure a rounding error occurs.
>This problem can be shown by running the following code:
>UPDATE NumericVal
>SET ValExpected = 0.72139753801593054
>WHERE NumericVal.ValItemCode = 2441 AND ValTPCode = 4159
>SELECT ValExpected
>FROM NumericVal
>WHERE NumericVal.ValItemCode = 2441 AND ValTPCode = 4159
>
>ValExpected returns the value 0.72139752, although it should be
>identical to entered value which would round to 0.72139754.
>I know it's only a small difference but it is causing greater errors in
>later calculations.
>Is there any explanation as to why this is happening and how can I
>resolve the issue?
>
>|||ValExpected is declared as a real variable.
Why when storing the value would it store 0.72139752 rather than
rounding it to 0.72139754 or even truncating it to 0.72139753?
NOTE: I get the same result if I use the fraction as follows:
UPDATE NumericVal
SET ValExpected = CAST (3985 AS REAL) / CAST (5524 AS REAL)
WHERE NumericVal.ValItemCode = 2441 XAND ValTPCode = 4159|||SORRY:
The SQL Script above should read:
UPDATE NumericVal
SET ValExpected = CAST (3985 AS REAL) / CAST (5524 AS REAL)
WHERE NumericVal.ValItemCode = 2441 AND ValTPCode = 4159|||Adam,
A REAL just can't represent any value within 0.00000001 of
0.72139754.
declare @.r real
set @.r = 0.72139754
select @.r, cast(@.r as binary(4))
set @.r = 0.72139755
select @.r, cast(@.r as binary(4))
-- Results
0.72139752 0x3F38AD82
0.72139758 0x3F38AD83
The two binary values here are "adjacent" REAL values, and
the decimal output is rounded for display purposes to 8 places.
A REAL can't represent any value between these two.
Eight decimal places are shown because there are REAL values
that are different numbers but that have the same 7-place decimal
rounding. The tradeoff is then that there are 8-place decimal values
that are not the rounded version of any REAL. The decimal equivalent
of a REAL's precision is just over 7 digits.
SK
AdamHCC wrote:

>ValExpected is declared as a real variable.
>Why when storing the value would it store 0.72139752 rather than
>rounding it to 0.72139754 or even truncating it to 0.72139753?
>
>NOTE: I get the same result if I use the fraction as follows:
>UPDATE NumericVal
>SET ValExpected = CAST (3985 AS REAL) / CAST (5524 AS REAL)
>WHERE NumericVal.ValItemCode = 2441 XAND ValTPCode = 4159
>
>|||Thank you for all of your help. However I am still .
If REAL can only store that the number to seven places, why then does
it dump the following number into excel; 0.721397519111633 ?
This has far more digits that can be stored in REAL and is totally
incorrect after the first 7 decimal places. Why return another 8
decimal places that are incorrect? Surely common sense dictates that
this should not happen.|||Adam,
I can only guess, but I imagine that the connection between SQL Server
and Excel doesn't have the sophistication to distinguish between source
values that are 4-byte floating point and source values that are 8-byte
floating point, so Excel displays all "number" values to 15 decimal places
by default, since that is roughly the maximum that could be meaningful
without knowing more about the source type.
To be fair, the extra 8 decimal places are not "incorrect." Once the
value of a real number is stored in SQL Server, it is an exact value.
Nothing is stored along with the number to indicate whether precision
was lost when the number was stored.
SK
AdamHCC wrote:

>Thank you for all of your help. However I am still .
>If REAL can only store that the number to seven places, why then does
>it dump the following number into excel; 0.721397519111633 ?
>This has far more digits that can be stored in REAL and is totally
>incorrect after the first 7 decimal places. Why return another 8
>decimal places that are incorrect? Surely common sense dictates that
>this should not happen.
>
>