Harry Boy Harry Boy - 2 months ago 19
SQL Question

Update row entries using Transact-SQL

I have an MSSQL Server 2014 database called "Database.Main" which has column headers called "Type" and "Code".
I need to modify an existing Transact-SQL script to select all the rows that have "Type" equal to "MyObject.Main" and change the "Code" to integer 1000.

How can I do this in Transact-SQL?

GO

BEGIN TRY
BEGIN TRAN Update_Table;

COMMIT;
END TRY
BEGIN CATCH
print 'Error encountered updating entries'
print ERROR_MESSAGE()
rollback;
END CATCH


Answer
Based on the answer from Raul below the solution is this:

BEGIN TRY
BEGIN TRAN Update_Table;
UPDATE Database.Main
SET Code = 1000
WHERE Type = 'MyObject.Main';
COMMIT;
END TRY
BEGIN CATCH
print 'Error encountered updating entries'
print ERROR_MESSAGE()
rollback;
END CATCH

Answer

A simple UPDATE command would do?

update table_name
set Code = 1000
where Type = 'MyObject.Main';