record query

Experts out there... please help me with this query.. I am trying to insert a date in the date column of this table..the date goes in a chronological order

use master
declare
@counter datetime, @noRows int,@i int
select @i=1
select @counter='01/01/1970'
set @noRows=(select count(*) from cp1)
while(@noRows>0)
begin
update abs1 set [Date] = @counter
where                                      record = i------> i dont know how to refer a row number

set @i=@i+1
set @counter=@counter+1
set @noRows=@noRows-1
continue
end
go

thanx a lot
AiyshaAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

rherguthCommented:
It looks to me like you are using MS SQL Server and need to use a CURSOR to do a bunch of updates.  I don't know why you are setting what looks like a date to a number, but maybe you have dates stored as Ints - it happens.  You could also probably just use a single UPDATE statement that uses a derived table to generate a counter if that's what you really need.  At any rate, let me know more about what you are trying to do or add back your counter vars inside the loop.

SET NOCOUNT ON
USE MASTER
DECLARE csrCP1 CURSOR FOR
      -- I don't know what fields you need in the cursor but you need something to use as an expression for you UPDATE clause
      -- This is where you decide what rows will be updated (takes the place of your @counter var)
      SELECT CPIDColumn, CP1DateColumn FROM CP1 -- WHERE <some expression> -- if applicable
DECLARE @CP1IDColumn int
      , @CP1DateColumn DATETIME

OPEN csrCP1
FETCH NEXT FROM csrCP1 INTO @CP1IDColumn
WHILE (@@FETCH_STATUS <> -1)
BEGIN
    IF (@@FETCH_STATUS <> -2)
    BEGIN    
      UPDATE abs1 SET [Date] = @CP1DateColumn
    END
    FETCH NEXT FROM csrCP1 INTO @CP1IDColumn
END
CLOSE csrCP1
DEALLOCATE csrCP1
SET NOCOUNT OFF
0
rherguthCommented:
After reconsidering what you are trying to do, i think you want something more like this:

SET NOCOUNT ON
USE MASTER
DECLARE @counter int, @noRows int
SET @noRows = (SELECT count(*) FROM cp1)
SET @counter = 1

DECLARE csrabs1 CURSOR SCROLL DYNAMIC FOR
      SELECT [Date] FROM abs1 ORDER BY DATE -- WHERE <some expression> -- if applicable
      FOR UPDATE OF [Date]

DECLARE @absDate DATETIME

OPEN csrabs1
FETCH NEXT FROM csrabs1 INTO @absDate
WHILE (@@FETCH_STATUS <> -1 And @counter <= @noRows)
BEGIN
    IF (@@FETCH_STATUS <> -2)
    BEGIN
      UPDATE abs1 SET [Date] = DATEADD(dd, @counter, '01/01/1970') WHERE CURRENT OF csrabs1
      SET @counter = @counter + 1
    END
    FETCH NEXT FROM csrabs1 INTO @absDate
END
CLOSE csrabs1
DEALLOCATE csrabs1

There needs to be a unique index in abs1, or the updateable cursor will return "FOR UPDATE cannot be specified on a READ ONLY cursor"

If this isn't what you want, please drop a table structure in or elaborate on what you're trying to do.
0
AiyshaAuthor Commented:
Thanx for your response rherguth, I think you are adding too many statements to this query.. or maybe I am thinking it to be a simply problem..

the structure of my tabel is as follows

            table name : abs
            column 1 : depth --- the numbers repeat after 1200 records
            column2: trace name
            column 3 : abs value
            column 4: date

now the date column is null. I want to fill it complete with date starting from 1/1/1970 all the way down till the end of the table.. I am filling the date as a counter because the other applicaiton to which I will be exporting the abs1 table, recongnizes date as a counter...
now all i need a command which will tell me the record number within a table..

Thanx a lot
           
0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

rherguthCommented:
There isn't a function or command that returns a rownumber in SQL Server.  That is why I used a cursor.  There are two ways to iterate through a rowset that I know of using T-SQL.  One is to use a cursor, the second is to use a SELECT WHILE which is generally much slower.

For one time conversions, I have used a technique where I add an IDENTITY column to the table.  By doing so, a number from 1 to the last row wil be created.  Then you could use the UPDATE command to
UPDATE abs SET [Date] = DATEADD(dd, [IdentityColName], '01/01/1970') WHERE [Date] IS NULL
0
AiyshaAuthor Commented:
Thanx rherguth

I dont know why I have this error when i try to run the above query SQL query analyzer
" FOR UPDATE cannot be specified on a READ ONLY cursor."
0
rherguthCommented:
You get that error if there is no unique column for the update.  If you have a combination of columns that can form a unique key, then Ctrl-Click those columns names in the Table Design window in SQL EM and select "Primary Key".  If you don't have a unique key, then you really will need to add an identiy column and then set that as the Primary Key.
0
David LittleCommented:
How about this? SQL Server does store dates a a number. "1/1/1970" actually translates to 25567. Change your date column to type int, tell it to make the column an IDENTITY column, and set the initial seed to 25567. SQL Server will assign one up numbers to each row. Then go back and change your date column back to a datetime and viola! Sequential dates!

David
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
rherguthCommented:
That seems like a good idea!
0
David LittleCommented:
I'm still interested ...
0
rherguthCommented:
Let's split the points
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Databases

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.