Update one row only out of two identical rows in sybase - sql

I have three rows in the database out of those two are identical. Out of those two same rows I have to make changes in one using the sybase.Ex.
Row1: ABC 456 ancient block
Row2: ABC 456 ancient block
Row3: DEF 678 class block
I have to make changes in one of the first two block by changing ABC to XYZ.If there are only two identical blocks then I am doing the below method.
begin transaction AA
set rowcount 1
update table
set col1 = XYZ
where col1 = ABC
commit transaction AA
set rowcount 0
It is easy if there are two identical rows but if two identical and one different then sybase picks the unique row and updates it. Can someone tell how to solve this three rows problem ?
I am using aseisql for the Sybase.

Have you tried:
update top 1 table
set col1 = XYZ
where col1 = ABC

This is the solution I figured.
begin transaction a
set rowcount 2
update table
set col1 = XYZ
where col4 = block
commit transaction a
set rowcount 0
It will update one of the duplicate rows and the unique row. Then I will update the unique row to its original value separately using the update statement. I know its sound crude but no one has answered satisfactorily.

Related

Sequential update statements

When using multiple SETs on a single update query like
update table set col1=value1,col2=col1
is there an order of execution that will decide the outcome, when the same column is left or right of an equals sign? As far as I've tested so far, it seems when a column is used to the right of an equals as a data source, then its value is used from BEFORE it gets a new value within the same update statement, by being to the left of an equals sign elsewhere.
I believe that SQL Server always uses the old values when performing an UPDATE. This would best be explained by showing some sample data for your table:
col1 | col2
1 | 3
2 | 8
3 | 10
update table set col1=value1,col2=col1
At the end of this UPDATE, the table should look like this:
col1 | col2
value1 | 1
value1 | 2
value1 | 3
This behavior for UPDATE is part of the ANSI-92 SQL standard, as this SO question discusses:
SQL UPDATE read column values before setting
Here is another link which discusses this problem with an example:
http://dba.fyicenter.com/faq/sql_server/Using_Old_Values_to_Define_New_Values_in_UPDATE_Statements.html
You can assume that in general SQL Server puts some sort of lock on the table during an UPDATE, and uses a snapshot of the old values throughout the entire UPDATE statement.

How to update multiple records in a table?

I need to update the column B in a table, which has a column A as the primary key, with the a different value for each value in column A. There are about 50,000 rows to be updated in the table, which makes it impossible to do this manually. Is there any other way to update it?
Of all the records in the table, I want to update just 50000. For each record among these 50,000, the value to be updated is different. How can I update the table without having to write 50,000 update queries?
Column A. Column B
One. 1
Two 2
Three 3
I want to update one=4, two=5 and so on for about 50,000 rows.
Thanks in advance guys!
I don't know whether I got your requirement properly but i have written a below working snippet to replicate the scenario. Let me know if this helps
--Drop any existing table if present with same name
DROP TABLE SIMPLE_UPDATE;
--Create new table
CREATE TABLE SIMPLE_UPDATE
(
COL1 NUMBER,
COL2 VARCHAR2(2000 CHAR)
);
-- Inserting random test data
INSERT INTO SIMPLE_UPDATE
SELECT LEVEL,TO_CHAR(TO_DATE(LEVEL,'J'),'JSP') FROM DUAL
CONNECT BY LEVEL < 500;
-- Updating the col2 value assuming thta the increment is adding 3 to each number and updating the col2 with the same.
UPDATE SIMPLE_UPDATE
SET COL2 = COL1+3
WHERE <COL_NAME> = <CONDITON>;
COMMIT;

SQL Update Multiple Rows with Multiple Values

I have a list of items that I need to update based on their unique ID in a SQL Server 2005 environment. I was wondering what the best way of writing a script to update these items.
I know I can simply update a column by writing multiple queries such as:
UPDATE myTable
SET HelpLink = 'newLink'
WHERE ID = 5
UPDATE myTable
SET HelpLink = 'newLink2'
WHERE ID = 6
Is there any other way of doing this without having to repeat the above update about 20 times? Repeating the above tends to make a pretty ugly update script.
NOTE: I have a bulk set of items that I will be updating by their unique ID, these items are not coming from a database table.
I found out that you can use case statements which seems to simplify things quite a bit. This allows me to add multiple items into a single query.
UPDATE [MyTable]
SET HelpLink = CASE ID
WHEN 2 THEN 'MKSDefectsChart.png'
WHEN 6 THEN 'EPMRisks.png'
WHEN 7 THEN 'DCTSHardwareChanges.png'
ELSE NULL
END
WHERE ID IN (2, 6, 7)
You can always update from another table like
update myTable
set HelpLink = myOtherTable.HelpLink
from myOtherTable
where myTable.[ID] = myOtherTable.[ID]
You'll have to create that other table though

How to update one table row with same keys by SQL?

I have a table T like this:
Name(Not unique) Value
A 1
A 3
A 5
A 8
And if I only want to update the 3rd row, how to write the SQL?
Below SQL does not work, it updates all the rows.
update T set Value='10' where Name='A'
update T set Value='10' where Name='A' and value=5
UPDATE T SET Value='10' WHERE value=5

Delete the column for the particular value

Using Sql Server 2005
Table1
ID Name Value
001 Rajesh 90
002 Suresh 100
003 Mahesh 200
004 Virat 400
...
I want to delete the value from the table1 for the particular id
Tried Query
Delete value from table1 where id = '001'
The above query is not working.
How to make a delete query for delete the particular column
Need Query Help
There are at least two errors with your statement:
The word table will give a syntax error because it is a reserved word. You need to specify the table name of the specific table you wish to delete from.
Also you cannot write DELETE value FROM. It's just DELETE FROM. And note that it deletes the entire row, not just a single value.
A correct delete statement would look like this:
DELETE FROM table1
WHERE id = '001'
However if you want to change a single value to NULL you should use an UPDATE statement.
UPDATE table1
SET value = NULL
WHERE id = '001'
Of course this assumes that the column is nullable. If not, you'll have to fix that first. See this question for details:
Altering a column to be nullable
I think you want to set the value to null
update Table1 set value=NULL where id='001'