How to update one table row with same keys by SQL? - 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

Related

How to update the first row of a table in dolphindb?

Does anyone know how to write the where conditions for the first row for dolphindb?
update table set x=100 where ?
you can use the dolphindb function rowNo:
update t set x=100 where rowNo(x)=0

UPDATE and REPLACE part of a existing column value with new column value

I am trying to update a existing column value with new value in sql table.
For example, My table has below data
table1:
ID
RequestName
1
Victor-123
2
Hello-123
3
Victor-124
4
Victor-125
5
Hi-123
6
Victor-126
In the above table I want to update Request Name column value wherever we have Victor, I want to replace with Victor-ID. For example
for ID 1 we have RequestName column value is Victor-123. I want to update it with Victor-ID-123 using Sql. I know we can do it with update sql statement, but if we have lot of data how to achieve that or replace 'Victor' with 'Victor-ID'. Since we might have different values in Request Name column. I want to update only column value with Victor to Victor-ID in table
Any help, I appreciate it
Thank you
UPDATE [tablename] SET [RequestName] = REPLACE(RequestName,'Victor','Victor-ID')
Here are two examples:
Example 1:
update table1
set RequestName = REPLACE(RequestName, 'Victor', 'Victor-ID')
where RequestName like 'Victor-%'
Example 2 (this one will let you use this for any name by changing the where clause):
update table1
set RequestName = LEFT(RequestName, CHARINDEX('-', RequestName) - 1) + '-ID-' + RIGHT(RequestName, LEN(RequestName) - CHARINDEX('-', RequestName))
where RequestName like 'Victor-%'
I don't believe example 1 actually requires the where clause though, as it should only affect those with "Victor". However, if you have something like "Victor1-123" that should not be changed, the where clause will prevent that.
Edit: Just occurred to me that example 1 can be adjusted to:
update table1
set RequestName = REPLACE(RequestName, 'Victor-', 'Victor-ID')
This will allow you to eliminate the where clause.

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 multiple column in a single table

I have created a table having three colums GsID,ALlowance and amount
Begin if exists(select * from [dbo].[HRAllowances] where GdId=#GdId) begin
update [dbo].[HRAllowances] set
Amount=#Amount where GdId=#GdId end
end
this only works for one particular row..
i want to allow user to update amount for all rows
After seeing your comment, if you wish to update all Amount fields for all records, remove your WHERE clause as this is filtering records by ones where GdId=#GdId:
UPDATE [dbo].[HRAllowances]
SET Amount=#Amount
If you want to update all the rows than you have to remove where condition that is
where GdId=#GdId from you query.
Like this :-
update [dbo].[HRAllowances] set Amount=#Amount
I hope it will works!!

Delete one column on the criteria of another SQL Server 2008

One attribute in a table became corrupted after a certain point in a table of mine. I want to delete every pat_coun attribute if it has an ID that begins with 11 (number, not text). So I don't want to get rid of any of the records in the database, just clear out the attribute pat_coun if it's ID begins with 11
DELETE pat_coun from myTable
WHERE id %11
Just want to make sure this is right before I go deleting stuff. Thanks.
To clear out an attribute, do NOT use the DELETE function! That deletes a row from your table!
You need to use UPDATE instead:
UPDATE myTable
SET pat_coun = NULL
WHERE id LIKE '11%'
If you want to delete a record (a row) you can use
DELETE FROM myTable
WHERE condition
If you just want to "clear" a particular column you should use
UPDATE myTable
SET pat_coun = 0 // or NULL or whatever you please
WHERE condition
For condition IMHO you should convert your number to string and check like this
WHERE CONVERT(VARCHAR(20), pat_coun) LIKE '11%'
try this
update myTable
set pat_coun = null
where id like '11%'