SQL update, check changed value - sql

I have some problem with the update in Sybase DB.
I need, for audit, get a field that value is changed.
I do an update for all fields because I don't know what the user change.
Suppose we have this table with one row. Name,Surname,id
My update is:
update table
set Name = #name, Surname = #Surname, id = #id;
Now I need now only the fields that are changed in the update.
Sorry my English and thanks

Related

how to delete a value from a column in SQL

How can I delete only the field of a column without deleting the entire row? I have a table that owns the fields:
ID, Name, Mail, Phone, Company and Would like to delete only the email of a person without deleting all the fields.
If I use:
DELETE FROM TEST_TABLE WHERE MAIL = 'myemail#gmail.com'
that way will delete everything and I want to delete just the email
you can use this
Update myTable set MyColumn = NULL where Field = Condition.
References
1- How do I set a column value to NULL in SQL Server Management Studio?
2- UPDATE, DELETE, and INSERT Statements in SQL
try
UPDATE
TEST_TABLE
SET
MAIL = ''
WHERE
id = your_id
or
if you want delete the field
ALTER TABLE TEST_TABLE
DROP COLUMN MAIL;
It is good practice to update the field with a NULL instead of leaving it blank, this indicates that there is a missing value and will later allow you to do queries where something is NOT NULL, which will give better results if it isn't returning bad data. Remember, garbage in, garbage out.
UPDATE TEST_TABLE SET MAIL = NULL WHERE MAIL = 'myemail#gmail.com'

How do I prevent update on wrong data?

I have a scenario, like I want insert a record into a table, then I
update same record by getting the last inserted row Id, so now my
issue is if a another user inserts a new record before the first
record gets updated, so according to my scenario I get the last
inserted row Id , in this case update applied on the last row instead
of the first one, any solution please.
This is for SQL Server
if you have an Identity column in the Table, after inserting use the ##IDENTITY variable or SCOPE_IDENTITY() function to get the Identity value of the Row inserted and then while updating use the Identity Filed in the Where Clause.
Something like this
INSERT INTO MyTable(FullName)
VALUES('My Name')
SELECT #IdVal = SCOPE_IDENTITY()
UPDATE MyTable SET Phone='1234' WHERE IdCol = #IdVal

Automatically fill row with value based on inserted id

I have a table where the user is able to insert the ID of a Node that corresponds to a title elsewhere in the database. I want this tile to be automatically inserted into the row after the user has chosen the id.
This is my table:
I need to have the "SommerhusNavn" column be automatically filled with values based on the "SommerhusId" inserted.
I am using a third party to handle the CRUD functionality, where the user picks the ID from a dropdown. I already know in which table the title for the ID is located, I'm just not sure how to fill the row with the insert statement. Would I need to run a separate query for this to happen?
Edit:Solution
CREATE TRIGGER [dbo].[BlokeredePerioderInsert]
ON dbo.BlokeredePerioder
AFTER INSERT
AS
BEGIN
SET NOCOUNT ON;
UPDATE BlokeredePerioder SET SommerhusNavn = text FROM umbracoNode AS umbNode
where SommerhusId = umbNode.id
END
GO
Yes, you need to run additional UPDATE query. Let's assume that you have the TitlesTable, with columns ID and Title. Then it should look like:
UPDATE MyTable SET SommerhusNavn = Title FROM TitlesTable AS A
WHERE SommerhusId = A.ID
AND SommerhusNavn IS NOT NULL --not necessary
Perhaps i'm not understanding, but why can't you use send the value across in the initial update?
Can you use a trigger on the database side?
Alternatively, you'll need to send a update across, following the insert.

Does my trigger updates all the records

I have written a simple trigger which sets a column value with the id column value.
This is my trigger.
CREATE TRIGGER SubSectionsPrioritytrigger
ON SubSections
AFTER INSERT
AS
UPDATE dbo.SubSections
SET Priority = Id
After writing this trigger that, does this updates all the records after each insert. Or the only created new row.
Could some one provide any info on this.
Thanks.
To Update just the inserted row, you can do this:
UPDATE dbo.SubSections
SET SubSections.Priority = SubSections.Id
FROM INSERTED
WHERE INSERTED.Id = dbo.SubSections.Id
It does exactly what you wrote
UPDATE dbo.SubSections
SET Priority = Id
Any row will be updated.
You can change this to
UPDATE dbo.SubSections
SET SubSections.Priority = Inserted.Id
FROM INSERTED
WHERE INSERTED.id = SubSections.id
this will affect only the inserted rows.
to update just the last inserted row to what ever value you want you need to specify that row id which makes that row unique, from other rows.
and in your trigger you have not specified a where clause which causes all rows in the table to be updated.
whenever a any operation/event occurs on the table which has trigger associated with it mean a new record is inserted/updated or deleted an magic table is created in memory of SQL Server and we can access that magic table with the keyword "Inserted" in case of insert or Update and "Deleted" in case of delete.
so your query should be like this.
UPDATE dbo.SubSections
SET SubSections.Priority = SubSections.Id
FROM INSERTED
WHERE INSERTED.Id = dbo.SubSections.Id

SQL Server Query Command

I have an SQL database table, I want to find everything in a table where the 'Room' says 'DISPOSED', Insert 'DISPOSED' into the 'Status' field and then delete the entry in 'Room'.
Basically moving the entry from one field to another (if the 'Room' field has 'DISPOSED' in it)
Hope this makes sense.
Thanks for any help.
Update table_name SET room='', Stauts='DISPOSED' where Room='DISPOSED'
OR
Update table_name SET room=null, Stauts='DISPOSED' where Room='DISPOSED'
The SQL is
UPDATE table
SET status = room, room = null
WHERE room = 'DISPOSED'