How to update a computed column in SQL Server 2008 R2 - sql

On a SQL Server 2008 R2 instance, we have a table where we would like to replace an existing column with a persisted computed column. So far, so good. We'd also like to allow existing queries that attempt to update this field to be "redirected" to actually update the "real" columns that are used to generate the value of the persisted computed column.
We've tried an INSTEAD OF UPDATE trigger on the table, but get the error: "...cannot be modified because it is either a computed column or is the result of a UNION operator."
I know that you can intercept the update of a computed column that is a part of a view with an INSTEAD OF UPDATE trigger, but apparently this technique does not work directly on a table itself.
We know we can modify the query or create a view and point the query at the view to make this work, but is there any way to accomplish the update without making these changes?

Related

How to decrement Auto column or Field by one using UPDATE SET on LibreOffice Base?

I have a table with auto increment column (ID) and have already filled my table with records. Then, after sometime I noticed that the auto increment (ID) column started from 2 instead of 1. I really wanted the count to start from 1. So, what I want to do is decrease the ID column by one for all the records using SQL statement UPDATE SET. I have used this SQL Statement on MySQL database and it worked. However, on LibreOffice base, it won't even allow me to execute Update statement saying that it is NOT a query. So, following is what I want to do.
UPDATE Accounts SET ID=ID-1;
Apparently, LibreOffice base doesn't like that sql statement. So, how can I do this?
It sounds like you tried to create a query, but that is not how to run an update command. Instead, go to Tools -> SQL and enter the following:
UPDATE "Accounts" SET ID=ID-1;
This was tested using the default HSQLDB engine.

sql server 2012 t-sql change value in table

In a t-sql 2012 stored procedure, I would like to know if my solution to solve the problem is correct and/or if you have any other suggestion(s) on how I can complete the task I am listing below:
In an existing SQL Server 2012 stored procedure, there is a table called Atrn that is truncated every night. The table Atrn has a column called ABS that is populated with incorrect data.
The goal is to place the correct value into the ABS column that is located in when the stored procedure is executing.
Note: The goal is to fix the problem now since it is a production problem. The entire stored procedure that updates the dbo.Atrn table will be rewritten in the near future.
My plan is to:
create a temp table called #Atrnwork that will contain the columns
Atrnworkid int and ABSvalue double .
The value in the column called Atrnworkid in the #Atrnwork table will obtain its value from the key of the Atrn table, called atrnid by doing a select into. At the same time, the value for ABSvalue will be obtained by running some sql when the select into occurs
The main table called Atrn will be changed with a update statement that looks something like:
Update Atrn
set ABS = ABSvalue
join Atrn.atrnid = #Atrnwork.Atrnworkid
In all can you tell me what a good solution is to solve this problem and/or display some sql on how to solve the problem listed above?

rewriting query in PostgreSQL

After a db schema change, what was a column is now computed in stored procedure. Is it possible to make this change seamless for application programs?
So that when a program sends a query like
SELECT id,
value
FROM table
...it instead gets a result of
SELECT id,
compute_value() AS value
FROM table
I thought I could use a RULE, but it is not possible to create SELECT rule on existing table.
So the only other option seems to me to create a new table and a view with the name of the existing one. Which, because of the need for INSERT/UPDATE triggers for the view is too complicated. Then I'd rather update all the client applications.
If you know you want to return value, you use a function rather than a stored procedure. Then you'd reference it like:
SELECT id,
your_function_name(parameter) AS value
FROM TABLE
There's an example under "SQL Functions on Composite Types" in the documentation.
Creating a view using the statement above is ideal if your application needs the computed value constantly, otherwise I wouldn't bother.

Sql Server 2005 - Insert Update Trigger - Get updated, insert row

I want to create a table trigger for insert and update. How can I get the values of the current record that is inserted/updated?
within the trigger, you can use a table called 'inserted' to access the values of the new records and the new version of the updated records. Similarly, the table called 'deleted' allows you to access deleted records and the original versions of updated records.
using function 'update' on column ( if you wanna check the fact of update) or retrieving rows from table 'inserted'
While triggers can be used for this, I'd be very careful about deciding to implement them. They are an absolute bear to debug, and can lead to a lack of maintainability.
if you need to do cascading updates (i.e. altering table A in turn changes table B), I would either use a stored procedure (which can be tested and debugged more easily than a trigger), or if you're fortunate enough to be using an ORM (Entity framework, NHibernate, etc.) perform this function within your model or repository.

Force SQL Server column to a specific value

Is it possible to force a column in a SQL Server 2005 table to a certain value regardless of the value used in an insert or update statement is? Basically, there is a bug in an application that I don't have access to that is trying to insert a date of 1/1/0001 into a datetime column. This is producing a SqlDateTime overflow exception. Since this column isn't even used for anything, I'd like to somehow update the constraints on the columns or something in the database to avoid the error. This is obviously just a temporary emergency patch to avoid the problem... Ideas welcome...
How is the value being inserted? If it's through a stored proc... you could just modify the Sproc to ignore that input parameter.
if it's through client-side generated SQL, or an ORM tool, otoh, then afaik, the only option is a "Before" Trigger that "replaces" the value with an acceptable one...
If you're using SQL 2005 you can create an INSTEAD OF trigger.
The code in this trigger wil run in stead of the original insert/update
-Edoode
I'd create a trigger to check and change the value
If it is a third party application then I will assume you don't have access to the Stored Procedure, or logic used to generate and insert that value (it is still worth checking the SPs for the application's database though, to see if you can modify them).
As Charles suggested, if you don't have access to the source, then you need to have a trigger on the insert.
The Microsoft article here will give you some in depth information on creating triggers.
However, SQL Server doesn't have a true 'before insert' trigger (to my knowledge), so you need to try INSTEAD OF. Have a look here for more information. In that article, pay particular note of section 37.7, and the following example (again from that article):
CREATE TRIGGER T_InsertInventory ON CurrentInventory
INSTEAD OF INSERT AS
BEGIN
INSERT INTO Inventory (PartNumber, Description, QtyOnOrder, QtyInStock)
SELECT PartNumber, Description, QtyOnOrder, QtyInStock
FROM inserted
END
Nick.
the simplest hack would be to make it a varchar, and let it insert that as a string into the column.
The more complicated answer is, you can massage the data with a trigger, but it would still have to be valid in the first place. For instance I can reset a fields value in an update/insert trigger, but it would still have to get through the insert first.