Showing posts with label inserting. Show all posts
Showing posts with label inserting. Show all posts

Wednesday, March 21, 2012

Row Not Inserting At Bottom

Greetings, SQLanoids,

When I insert a row of data into a table via a stored procedure, the row SOMETIMES gets inserted at the bottom, and sometimes gets inserted at another random location (top, midding, etc). I am using Microsoft SQL Server.

Dea anybody know the cause of this? Or how to ensure that the row gets inserted at the bottom?

I am a novice SQL Server user, but this seems like a simple problem that the more advanced users solved long ago. Thanks in advance!This behavior you are seeing is by design. SQL Server decides where to put the data and we have no control over that. Nor, really, is it important.

If you need the data to come out of the database in a particular order, you need to use an ORDER BY statement. If it is important for you to know the order in which records were inserted into the database, I suggest using a DateAdded column with a Default Value of GETDATE() so that SQL Server will automatically populate this for you. You might also consider using a column flagged as an IDENTITY for this purpose.

Terri|||Thank you for the response. I tried your advice and it worked well. However, I noticed only one problem.

When it inserts the date, using a Default Value of GETDATE(), and using data type smalldatetime, the field gets inserted as "1/17/2005 2:23:00 AM".

However, when I set the data type to vnarchar, the field gets inserted as "Jan 17 2005 2:23AM"

I prefer the second method, as it spells out the first 3 letters of the month. The problem, however, is that the ORDER BY statement does not accurately pull the records when stored as nvarchar. It is unable to distinguish between AM and PM and pulls the records in order of numeric chronology only.

Do you know how I can fix this, and still store the items as "Jan..."?

Thanks|||Use a datetime data type to store datetime data. Use output formatting to display the data in the desired format (either by using CAST or CONVERT, or in the ASP.NET front end).

Terri

Wednesday, March 7, 2012

Rounding problems

I have rounding problems when editing or inserting a new record in float type fields.
e.g. I have a cursor running an agrregate SQL statement. I have a calculated field Sum(DFactor*Cost). DFactor gets values -1,1 and values of Cost in the table have 2 digits. I get these values in a variable e.g. @.FCost. Then I round @.FCost=Round(@.FCost,2).
When I try to inert this value to a new record again I'using Round(@.FCost,2).
However in a lot of records a lot of digits are stored.
I have the same probelm when trying to insert values from MSAccess by ODBC. Although I'm using CLng(@.FCost*100)/100 in order to have 2 digits, a lot of demical values are created.
What is the best practise in order to solve this problem?
Regards,
ManolisIf you are using a FLOAT column to store data of type MONEY, that's a problem. If you are using a FLOAT column to store data of type DECIMAL (x, 2), that's also a problem. Is your underlying problem one of datatype, not actually rounding?

-PatP|||Although I'm using CLng(@.FCost*100)/100 in order to have 2 digits, a lot of demical values are created.The result will have decimals. You need this: CLng(@.FCost*100/100)