Solved

record query

Posted on 2003-11-04
11
658 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
 
LVL 9

Expert Comment

by:rherguth
Comment Utility
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
Comment Utility
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
Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

 
LVL 9

Expert Comment

by:rherguth
Comment Utility
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
Comment Utility
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
Comment Utility
That seems like a good idea!
0
 
LVL 2

Expert Comment

by:David Little
Comment Utility
I'm still interested ...
0
 
LVL 9

Expert Comment

by:rherguth
Comment Utility
Let's split the points
0

Featured Post

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

Suggested Solutions

I guess that all of us know that caching the data usually increase the performance, but I worried if all of us are aware about the risk that caching the data provides and how to minimize this.  That’s the reason why I decided to write this short art…
This article describes some very basic things about SQL Server filegroups.
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…

771 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now