Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

record query

Posted on 2003-11-04
11
Medium Priority
?
704 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 6
  • 2
  • 2
11 Comments
 
LVL 9

Assisted Solution

by:rherguth
rherguth earned 1000 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
Get free NFR key for Veeam Availability Suite 9.5

Veeam is happy to provide a free NFR license (1 year, 2 sockets) to all certified IT Pros. The license allows for the non-production use of Veeam Availability Suite v9.5 in your home lab, without any feature limitations. It works for both VMware and Hyper-V environments

 
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 1000 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

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

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

Recently I was talking with Tim Sharp, one of my colleagues from our Technical Account Manager team about MongoDB’s scalability. While doing some quick training with some of the Percona team, Tim brought something to my attention...
Instead of error trapping or hard-coding for non-updateable fields when using QODBC, let VBA automatically disable them when forms open. This way, users can view but not change the data. Part 1 explained how to use schema tables to do this. Part 2 h…
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …
Suggested Courses

610 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