I have a function that sending a code gets a description through various formatings, I need to use the function inside a view in four columns but rather than calling it in the query I would like to associate it as a computed column (so we don't have to modify the query/view if the function name or parameters changes so the DBA can manage it in the future...)
Is it possible?
Yes - you can do something like:
ALTER TABLE dbo.YourTableNameHere
ADD ComputedColumn1 AS dbo.FunctionCall1(args......)
This adds a computed column to your table - and if the function is "deterministic", you can even add the PERSISTED keyword to it, so that the resulting value will actually be stored in the table (and the function isn't call every time you select a row).
Related
I don't know if this already has an answer as I wasn't able to find it, but if so please provide the link.
Column names are used in User Defined Stored Procedures and Functions when accessing certain columns in a table. But what if I want to edit the column name in a table and I have called that column name a lot of times in multiple usp's/udf's.
I am currently using SQL Server and obviously it will not let me update a table column since it is called in multiple existing usp's/udf's. Is there a way to update the column name of a table together with the column names in those usp's/udf's?
I'm using PostgreSQL to coordinate a large scale simulation which involves initializing various components of my application via arrays of integers. In particular, I have the notion of a "Controller", where each controller requires a variable number of parameters to be initialized.
I have a job table which stores the controller_id and a controller_parameters foreign key for actually linking to the set of parameters we want. My idea to start with was to do the following:
Use the controller_id to dynamically choose a table name from which to select the initialization parameters. Each of these tables would have a controller_parameters column that links the actual initialization data to the source table.
Once we know the table, run a SELECT * FROM #someController_parameters_table p WHERE p.controller_parameters = controller_parameters LIMIT 1;
Put these into a custom type which has an integer array field to be returned to the client.
The main problem is that this has Dynamic SQL, which I hear is not a good thing to do.
My proposed change is to have a new table, let's say controller_parameters which has the columns (controller_id, parameters_id, parameters[]). The third column stores the initialization parameters for an individual controller and parameter set.
Pros of this design are that we move back into static SQL land, which is good. Cons are that, when I generate the actual parameters to insert to the individual parameters table, I usually use a cross join to get all of the permutations of the individual parameters, and insert them accordingly to individual tables. I personally don't know how to take a cross-joined table row and convert it to an int[], so that's a potential roadblock.
Thoughts?
You can use array_agg to take the result of a group and turn it into an array.
select controller_id, parameters_id, array_agg(parameter)
from ...
group by controller_id, parameters_id
Here's postgresql docs on aggregate functions:
https://www.postgresql.org/docs/9.5/static/functions-aggregate.html
I have a table for an Invoice and another for an InvoiceItem. An invoice can contain one or more Invoice Items. In our database, we have defined both Invoices and InvoiceItem with a Balance column. Is there a way to define the Balance column as a calculated column using Code First and also specify the SQL Server Scalar function to use?
I know I can setup the column as computed in Code First, but I do not see how to specify the SQL function to call using Fluent. Is this even possible?
I need to get the id of the updated row in a table to use it to update another table via trigger
Also need to get the id of the deleted row in a table to use it to update another table via trigger
How can I do this?
Is there any built in functions in SQL Server?
If not what kind of trick that can help to accomplish this
Within your trigger, if you want to know the OLD value that was either updated / deleted
SELECT idColumnName FROM deleted
Where idColumnName is the column that contains the ID that you are interested in.
You can then use this ID value to then perform whatever processing that you need.
Additionally. if you want to use the NEW value being updated, the below query gives you that. This is useful especially in case of Updates where you want to compare old / new values of certain fields. In your case, since its an ID column, this will probably not be relevant
SELECT idColumnName FROM inserted
I'm new to complex database design. I'm currently into a project where the user should be able to retrieve Instructions based on a combination of 18 columns. So my parameter Table has the following columns
Job
State
Manager
ProcessCode
ProcessType
(rest of the columns truncated).
InstructionID (FK of Instruction Table)
When adding / Modifying the instruction, he can choose multiple options in each of the above parameters. The Stored Procedure will store data in all combinations possible, in order facilitate easy retrieval, as during search (retrieval) only one option will be chosen in each of the columns.
There can be multiple instructions for same combination and the same instruction can apply to multiple combinations.
I have somehow created the SP for adding instruction, but am now struck with modification. When my Webpage passes the new combination to SP, what is the best way to update the Table?
I could delete all existing rows and create new rows for new combination, but I wanted to maintain the created date and created user columns. Further, there is a requirement to maintain history of these in a separate history table.
Sorry for the length of the question... And, Thank you for help.
If you're trying to retrieve data based on a combination of parameters then you can set the parameters to have the default value of NULL e.g.
CREATE PROC spProcName
#FieldName INT = NULL
The only other thing to do is set the WHERE section of the statement to look at the parameter values and compare them to see if they or null or not e.g.
WHERE ((FieldName = #FieldName) OR (#FieldName IS NULL))
Use this for querying the tables and use standard update queries in a similar fashion using the default parameter value of null but setting the value like this:
FieldName = ISNULL(#FieldName, FieldName)
Which lets you update only given parameters.
Hope this is something you are after, I can give a full example if needed.
What you have is many-to-many relationship, so I would suggest you use: