Solved

record query

Posted on 2003-11-04
11
692 Views
Last Modified: 2008-02-01
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
0
Comment
Question by:Aiysha
  • 6
  • 2
  • 2
11 Comments
 
LVL 9

Assisted Solution

by:rherguth
rherguth earned 250 total points
ID: 9682901
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
 
LVL 9

Expert Comment

by:rherguth
ID: 9683317
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
 

Author Comment

by:Aiysha
ID: 9718294
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
Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

 
LVL 9

Expert Comment

by:rherguth
ID: 9719355
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
 

Author Comment

by:Aiysha
ID: 9723642
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
 
LVL 9

Expert Comment

by:rherguth
ID: 9723758
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
 
LVL 2

Accepted Solution

by:
David Little earned 250 total points
ID: 9853974
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
 
LVL 9

Expert Comment

by:rherguth
ID: 9854965
That seems like a good idea!
0
 
LVL 2

Expert Comment

by:David Little
ID: 10909234
I'm still interested ...
0
 
LVL 9

Expert Comment

by:rherguth
ID: 10909924
Let's split the points
0

Featured Post

NFR key for Veeam Backup for Microsoft Office 365

Veeam is happy to provide a free NFR license (for 1 year, up to 10 users). This license allows for the non‑production use of Veeam Backup for Microsoft Office 365 in your home lab without any feature limitations.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Never store passwords in plain text or just their hash: it seems a no-brainier, but there are still plenty of people doing that. I present the why and how on this subject, offering my own real life solution that you can implement right away, bringin…
As technology users and professionals, we’re always learning. Our universal interest in advancing our knowledge of the trade is unmatched by most industries. It’s a curiosity that makes sense, given the climate of change. Within that, there lies a…
Video by: Steve
Using examples as well as descriptions, step through each of the common simple join types, explaining differences in syntax, differences in expected outputs and showing how the queries run along with the actual outputs based upon a simple set of dem…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

733 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question