I have a column like this:
MYID INT GENERATED ALWAYS AS IDENTITY PRIMARY KEY
I know that when doing an INSERT it's possible to override the generated value.
However once the row has been created I can't find a way to use an UPDATE to change the value.
Is there a way to do it?
GENERATED ALWAYS means what is says. You cannot override the values. If you want the system to simply ignore the values you provide, you can use OVERRIDING USER VALUES in the INSERT statement. Otherwise you cannot supply values for the GENERATED column. Values for columns defined as GENERATED BY DEFAULT can be overridden by the user.
Related
In intersystems cache, I have an existing table with example data as below
ID Name
1 Allen
2 Benny
I want to modify column ID so that it will be auto incremented. After adding this, if I insert charlie into the table then the id should be 3.
I think I have to use IDENTITY but not sure how to use it. Thanks for your help
If you want to change default ID column to anything else, you can use any other Property of class, and Index by this property which marked as IdKey. But, you have to manage incrementing this property by yourself. And You can do it with InitialExpression for setting value for any new object even if it will not be saved, or in %OnBeforeSave method, which will be called just before saving object.
Background: In a Java application I'm working on, I'm doing a refactoring of the storage of enum values. Previously, these were stored as integers, and mapped through enum values with a helper method in the enum. I would like to utilize the #EnumType.STRING capabilities of JPA to make the database more readable.
So, what I'm basically trying to do is change the type (as well as the values) of a column. For example, I had this table definition to begin with:
table Something (
id int,
source int,
[more columns]
)
I wanted to change the source-column into a VARCHAR(100) column instead, and here is how I did that:
Introduce a new column, called source_new with VARCHAR(100).
Populate the new column with mapped values based on the values of the old column (so each row with value 1 in the source column get's the value 'SomeSource' in source_new, each row with value 2 in source gets 'OtherSource', and so on
Drop the source-column
Rename the source_new column to source (using sp_rename)
My problem is this: Once this is done, I can't update the now newly defined source-column, because it still insists that it's an int column, and not a varchar column!
So a query like this:
update Something set source = 'SomeSource' where id = 1;
fails with this:
Error: Conversion failed when converting the varchar value 'SomeSource' to data type int.
SQLState: 22018
ErrorCode: 245
At the same time, sp_help of the table shows that the column is defined as varchar(100), and not int! Also, the column holds numerous varchar values from the original datamigration (from before the rename).
Is this a bug, or am I doing something wrong by renaming a column to a column name that was previously used with another type? (And as I'm typing the last question, it just sounds absurd to me, when I drop a column I expect to disappear, not to leave traces and in effect not allowing me to reuse the column name at any time in the future..)
SQLFiddle to illustrate (sp_rename doesn't work with SQLFiddle it seems): http://sqlfiddle.com/#!3/0380f/3
I have found the culprit, and it's name is trigger!
Some genious decided to put a check if the value updated is a valid source (checking against another table) in a trigger, so much for trusting your own code..
I spit on the shadow of people who hide functionality in database triggers, pfoy! Go back to the 80's where you belong! :p
I have to take a value from one table which I have just added using seq.nextval(say Query 1) into another table for entering further information into another table through Query 2.I have used objects to pass values to the sql(oracle).I am using Spring framework's JDBCTemplate.
Can you help me with that.
Thank you.
use seq.currval to get the current value of the sequence, and include that in your subsequent insert.
I'm hoping this is a simple goof I did on my end ...
I have a table in my database set up like so:
column name: widget_guid
data type: uniqueidentifier
allow nulls: false
default value: newid()
identity: false
row guid: true
When records are created (via LINQ to SQL) that the values in this field are formatted as a GUID but contain all 0's
My assumption was that when a new record was created, that a guid would be autogenerated for that column, much like an auto-incrementing row id. Is this not true? Any help would be greatly appreciated.
Thanks.
You need to check your properties on the GUID column - what you need to make sure is:
Auto Generated Values is set to True (so you basically tell Linq-to-SQL that the database will generate the value)
Auto-Sync should be set to OnInsert so that your C# object will be populated with the new value after you've called context.SubmitChanges()
With these two settings, you should get the expected behavior: no need to set the GUID on the client side, the database will generate a new value and insert it, and you'll get it back right after the call to .SubmitChanges()
In your dbml file, set the field to nullable.
If it is set to not-nullable, LINQ does not go as far as checking that the field has a default; it simply believes the field non-nullable and will send Guid.Empty causing the 0's.
When using Linq-to-SQL, adding a column to an existing table, and setting a default value on that new column, it seems that Linq to SQL ignores the default value.
Has anyone else experienced this behaviour? Is there a way to fix it, so that Linq-to-SQL or SQL Server automatically sets the default value?
The column I added was of type Bit, with a default value set to 1.
I have now definitively fixed this by using an example from this blog.
partial void OnCreated() {
if (this.DateTimeCreated == null) {
this.DateTimeCreated = DateTime.Now;
}
}
I needed to pass this into a partial datacontext class, as the default one is automatically overwritten every time you change something in the dbml.
Set Auto Generated property to True.
Go into the designer and select "Auto-Sync" value of "OnInsert". This will sync the value when the record is inserted into the database.
I've seen this. What you can do is go into the l2sql designer, view properties for the table column that has a default value. There is a property "Auto generated value", set that to true.
This same value is set to true automatically for the identity column automatically, as in that case SQL Server is generating your row IDs.
I was faced the same problem of boolean data type with default as false and not updating that value
So when i see the database table, i was found there is not primary key was set, therefore when i was set the primary key it was updated find.
so check a table if it has the primary key or not.
If you ever need to change the value in your own code (eg. a type table value with a default) then don't use 'Auto Generated Value' or you'll get this later when you try updating it.
Value of member 'AlternativeQuoteStatus' of an object of type
'OrderDetail' changed. A member that is computed or generated by the
database cannot be changed.
I prefer to set such values when I create the object, with a fallback in OnCreated as described by others here.