SQL function in column - sql

I have a table in my SQL database. It has already some data and I have to add a new column. The value of the column should be created by a SQL function I'd definded. The parameters are two values from the table.
When I SELECT the data it's to late to call the function.
So I have value1 and value2 in my table. And I have a third column which should calculated like this function(value1, value2).
I only found solutions which execute the function at the SELECT.
Can anyone help me how to do this?

Use a computed column:
alter t add function_column as (function(value1, value2));
This will be calculated when it is accessed -- which means every time you use it. If you want the value calculated only once, then you would need to add a new column and a trigger to keep it up-to-date.

Related

SQL-Creating Table

I am stuck with this question. I know table can be created with the select statement.
CREATE TABLE new_orders (ordr_id,order_date DEFAULT SYSDATE,cus_id)
AS
Select order_id.order_date,customer_id FROM Orders;
Which statement would be true regarding the above question ?
The NEW_ORDERS table would not get created beacuse the default value cannot be specified in the column definition.
The NEW_ORDERS table would get created and only the NOT NULL constraint define on the specified columnns would be passed to the new table.
The NEW_ORDERS table would not get created because the column names in the CREATE TABLE command and the SELECT clause do not match.
The NEW_ORDERS table would get created and all the constraints defined on the specified columns in the orders table would be passed to new Table.
I think correct answer should be 3, but it's wrong. The correct answer according to my book is 2. I don't get it how?
The answer should not be 3. Because in the select statement, you are taking values from Orders table. So you will use the column names of that table. Selecting value from there, you are inserting it into new_orders. So, the column names don't have to be same because they are different tables.

Changing Data Type to an existing Column with data already stored

Im using SQL Oracle developer.
I have a table called "Vaccine", Within Vaccine I have a column called 'Vaccine_Number' Currently Vaccine Number is a NUMBER datatype size(2). I need to change this to varchar2(10).
Is this possible without having to lose my data in vaccine number?
The following is predicated on you not having any foreign key references on Vaccine_Number:
Add a new column to the table with the datatype that you want.
Run an update statement that sets the value of the new column with the other column's value.
Update Vaccine set New_Vaccine_Number=Vaccine_Number where New_Vaccine_Number is null
Also, you may need to use the To_Char function to convert your number into a varchar.
Delete the other column.
Rename new column to Vaccine_Number

How can i add values to a existing table column

I have a table where i have a column called student id and in that column i want to insert like SN + userid.But i know how to create a new column and do this but i want to add this to an existing column whenever a user is inserted.
ALTER TABLE [dbo].[Profile_Master]
Add [new] as ('SN'+CONVERT([varchar] (10),[UserId],(0)))
In SQL Server you can use triggers to do this:
http://msdn.microsoft.com/en-us/library/aa258254%28v=sql.80%29.aspx
Your solution is already compliant to your requirements: you are creating a computed column.
Thus, it will work for existing records and for new ones created in the future.

sql - retain calculated result in calculated field

certain fields in our database contain calculated functions e.g.
select lastname + ', ' + firstname as fullname from contact where contact.id =$contact$
when viewing the field the correct data is shown (i assume this is because when you open the record, the calculation is executed). however, the data is not 'stored' to the field, and therefore is null until the record is opened. is it possible to 'store' the result to the field, making it possible to search the data?
many thanks
james
EDIT
it is not possible for me to create computed_columns using our software.
the above field is a text feild where either 1) a user can manual type in the required data or 2) the database can generate the answer for you (but only whilst you are looking at the record). i know that if I run the following:
Select * from contact where contact.id =$contact$ for xml auto
i only get lastname, firstname - so i know that the fullname field does not retain its information.
If you are using computed columns in sql server, the column is already searchable regardless of whether the calculation result is stored or not. However, if you would like to make it so that the calculation is not run each time you read the row, you can change that under row properties in your Modify Table GUI.
Use the PERSISTED key word when you create the column
From BOL:
PERSISTED
Specifies that the SQL Server Database Engine will physically store the computed values in the table, and update the values when any other columns on which the computed column depends are updated. Marking a computed column as PERSISTED lets you create an index on a computed column that is deterministic, but not precise. For more information, see Creating Indexes on Computed Columns. Any computed columns that are used as partitioning columns of a partitioned table must be explicitly marked PERSISTED. computed_column_expression must be deterministic when PERSISTED is specified.
This isn't the way computed columns work in SQL Server, so I suspect this is something your client application is doing. How are you looking at the data when the value is computed correctly? Does it work when you view the data in SSMS?
Take a look at http://msdn.microsoft.com/en-us/library/ms191250(v=SQL.90).aspx to see how to create computed columns properly.
eg.
create table TestTable
(a int,
b int,
c as a + b)
insert into TestTable (a,b)
values (1,2)
select * from TestTable
where c = 3
This query is based on the computed column and it returns the row that's been inserted.
You need to use the PERSISTED option on a column when you use CREATE TABLE e.g.
CREATE TABLE test (col_a INT, col_b INT, col_c AS col_A * col_B PERSISTED)

how to insert a column with value row number in an access table

I have a large access table . I need to have a column which will have value as row number
how can I write an sql query fill an empty column inserted by me with row numbers
Write a sequence for your table . Then have default value as sequence .
So, whenever the column does not get users values , it will take the sequence values.
Im sorry, im a little confused at what you're getting at, but could you not just do an alter table?
ALTER TABLE my_table
ADD row_number int;
Make the Field DataType AutoNumber it will do it for you automatically.