|
You're welcome
|
|
|
|
|
I must be off my rocker but I can't see why you are using a cursor for this logic.
Need software developed? Offering C# development all over the United States, ERL GLOBAL, Inc is the only call you will have to make.
If you don't ask questions the answers won't stand in your way.
Most of this sig is for Google, not ego.
|
|
|
|
|
Because I need to loop through each record in a table and update a field based on an incrementing integer.
I'm failry new to all this so if you have any other ideas then I'm all ears!
|
|
|
|
|
Well autonumbers are the way to do incrementing integers in Sql Server and sequences in Oracle (look-up either SCOPE_IDENTITY() or sequence NextVal depending on your poison of choice)
However, to get your code to work it seems to me:
1) Begin Transaction
2) Insert
3) UPDATE set sequence = sequence + 10 WHERE sequence > @sequence
4) UPDATE set sequence = sequence + 10 where id = @id
5) COMMIT
CURSORS are evil and should be avoided.
Need software developed? Offering C# development all over the United States, ERL GLOBAL, Inc is the only call you will have to make.
If you don't ask questions the answers won't stand in your way.
Most of this sig is for Google, not ego.
|
|
|
|
|
Thanks for your response. I understand your concern about cursors but unfortunately I don't think your idea is going to work. The reason is because the insert statement is inserting a record with a sequence which isn't a multiple of 10, i.e. 15, 25, 35 etc. This then means that the newly inserted record is in the right place accoring to sequence number. The cursor then gets all the records ordered by sequence, and updates them back to numbers divisible by 10, including the newly inserted record (obviously, the sequence doesn't have to be multiples of 10, it could of easily have been 2, but still).
Again, I realise that cursors are not a good idea but I don't see any other way around it (other than doing it from my VB.NET application with multiple database calls which definately isn't a good idea). Also, there are going to be at most 30 records in this table so I thought that using a cursor wouldn't be too much of a problem.
Like I said, I am fairly new to this so any other suggestions you may have are welcomed.
|
|
|
|
|
I highly recommend you reevaluate your logic in your application and database. I think you will find the answer should come to you. While I do not know what you are doing, I have a pretty good idea.
Need software developed? Offering C# development all over the United States, ERL GLOBAL, Inc is the only call you will have to make.
If you don't ask questions the answers won't stand in your way.
Most of this sig is for Google, not ego.
|
|
|
|
|
Ok Thanks. I have just looked at your example again and now understand a little more where you're coming from and i think that it would acually work for my insert statement by changing one of the lines from
UPDATE set sequence = sequence + 10 where id = @id
to
UPDATE set sequence = sequence + 5 where id = @id
which is great and I will use this, thanks
But I have a similar stored procedure for doing an update on an existing record, that is, updating the sequence of one of the records already in the table. Maybe I will look into my logic further for this too.
Thanks again
|
|
|
|
|
I understood that you're trying to do ordering. For example you have ordinals 1,2,3,4 and 5 then you add a new record which has the ordinal 3 so old ordinals 3,4 and 5 will be replaced with 4,5 and 6 respectively. If that's the case, you could simply:
- update all rows, set ordinal = ordinal + 1 where ordinal >= 3
- insert the new row with ordinal 3
|
|
|
|
|
That is actually a good and very simple idea too, why didn't I think of that!
Will try that when I get to work tomorrow!
Thanks
|
|
|
|
|
|
This works great when inserting a new record and then updating each sequence but not when changing an existing record's sequence and then updating all the other sequences. But, with my new found knowledge, I'm sure that I'll work it out!
Thanks again all for your help.
|
|
|
|
|
Liqz wrote: , with my new found knowledge, I'm sure that I'll work it out!
I'm confident that you will
Liqz wrote: Thanks again all for your help
No problem.
|
|
|
|
|
hi,
i think you need to create a transaction for insert and then commit, there after you retrieve and update.
reagrds
modified on Thursday, January 22, 2009 5:49 AM
|
|
|
|
|
Yes, I thought this also and did try it but it didn't seem to work. Maybe I coded it incorrectly. I will try again!
Thanks for your help
|
|
|
|
|
Hi all
How do i read html content into a sql server report. Or if possible may you please help me with links on where to do this.
Thank you.
|
|
|
|
|
good morning sir/madam,
iam working on app in vb6.0 with msaccess ihave to convert that in oracle db..
here i have msaccess select statement..in which i am autogenerating codenumber..here iam trying to convert to oracle format..
but iam receiving Error:invalid relational operator
rsDetail.Open "SELECT max(to_number(cnor_cnee_id,3,13)) FROM cnor_cnee_master where (cnor_cnee_id,1,2)=" & intBranchPrefix, DBConnection, adOpenStatic, adLockReadOnly
so please suggest the require corrections...
thanx inadvance..
prem...
|
|
|
|
|
premprakashbhati wrote: where (cnor_cnee_id,1,2)=" & intBranchPrefix
What is the condition you're trying use? Field cnor_cnee_id should be what?
premprakashbhati wrote: max(to_number(cnor_cnee_id,3,13))
Also what do you want to do with cnor_cnee_id here?
|
|
|
|
|
Hi,
I have below select statement using MS Access.
SELECT Sum(IIf(Sku_Statistics_File.ExcessSA=0
And Sku_Statistics_File.ExcessSOH=0
And Sku_Statistics_File.StockOutFirst6wks=0
And Sku_Statistics_File.StockOutNext6wks=0
And Sku_Statistics_File.PastDue=0,1,0)) AS BALANCED
FROM SELECTEDPRODUCTS1
INNER JOIN Sku_Statistics_File ON (SELECTEDPRODUCTS1.LOCATION=Sku_Statistics_File.Location) AND (SELECTEDPRODUCTS1.PRODUCT=Sku_Statistics_File.Product);
How to convert to SQL CE? It seems i need to use CASE to replace the IIf, but i donno how to convert. Please help.
Thx.
|
|
|
|
|
Just replace IIF with
CASE WHEN x=1 and a=z ... THEN d=1 ELSE d=2 END
For further details see the BOL help on CASE
Bob
Ashfield Consultants Ltd
Proud to be a 2009 Code Project MVP
|
|
|
|
|
Thx for reply.
Is this the way should be done?
I get hitting error at "Sku_Statistics_File.PastDue = 0,1,0 " saying "," is invalid
SELECT Sum(CASE WHEN Sku_Statistics_File.ExcessSA=0
AND Sku_Statistics_File.ExcessSOH=0
AND Sku_Statistics_File.StockOutFirst6wks=0
AND Sku_Statistics_File.StockOutNext6wks=0
AND Sku_Statistics_File.PastDue=0,1,0 THEN 0 ELSE 0 END) AS BALANCED
FROM SELECTEDPRODUCTS1
INNER JOIN Sku_Statistics_File ON (SELECTEDPRODUCTS1.LOCATION=Sku_Statistics_File.Location) AND (SELECTEDPRODUCTS1.PRODUCT=Sku_Statistics_File.Product);
Pls help. Thx.
|
|
|
|
|
No, you have missed the point a bit.
SELECT Sum(CASE WHEN Sku_Statistics_File.ExcessSA=0
AND Sku_Statistics_File.ExcessSOH=0
AND Sku_Statistics_File.StockOutFirst6wks=0
AND Sku_Statistics_File.StockOutNext6wks=0
AND Sku_Statistics_File.PastDue=0) THEN 1 ELSE 0 END) AS BALANCED
FROM SELECTEDPRODUCTS1
INNER JOIN Sku_Statistics_File ON (SELECTEDPRODUCTS1.LOCATION=Sku_Statistics_File.Location) AND (SELECTEDPRODUCTS1.PRODUCT=Sku_Statistics_File.Product);
the THEN is the first condition of your IIF (true), the ELSE is the second condition (false)
Bob
Ashfield Consultants Ltd
Proud to be a 2009 Code Project MVP
|
|
|
|
|
Hello All,
I have a sql script that pulls certain records by my company's fiscal year which ends 8/31/yyyy. As I am trying to pull records based on the year (yyyy) I have written a condition as such a.THRU_DATE = '8/31/' + Year(GetDate()), however I receive the following syntax error Syntax error converting the varchar value '8/31/' to a column of data type int. Of the course the Thru_Date column type is datetime. Can someone please assist with the proper syntax for such a condition. Thanks in advance to all that reply
|
|
|
|
|
I found the answer to my own question using the CAST function. FYI, a.THRU_DATE = CAST('8/31/' + @year AS DateTime).
|
|
|
|
|
I have to use aggregate function sum() on a table having more than 4 millions of records. It is a history table. Are there any tips to be followed. the queries are a follows
SELECT @TransferIn = isnull(sum(abs(trxqty)),0)
FROM iv30300 WHERE doctype = 3 and itemnmbr = @itemnmbr
and trnstloc = @route and docdate between @FromDate and @ToDate
SELECT @TransferOut = isnull(sum(abs(trxqty)),0)
FROM iv30300 WHERE doctype = 3 and itemnmbr = @itemnmbr
and trxloctn = @route and docdate between @FromDate and @ToDate
SELECT @Sales = isnull(sum(abs(trxqty)),0)
FROM iv30300 WHERE doctype=6 and itemnmbr = @itemnmbr and
trxloctn = @route
In the above query @route,@FromDate and @ToDate are user inputs
|
|
|
|
|
Dear Uma,
Please give me the table structure with the available indexes.
Senthil
|
|
|
|