SQL Server Computed Column that doesn't affect existing data - sql-server

is that possible to do a computed column without affecting existing data?
I have a unique index column named
"BookingNo"
For newly insert Column I want it to be following this format
(format([CreationTime],'yyMMddHHmmssff'))
I tried used Computed Column but it modified all my existing data.
my existing BookingNo format was
201800123
Is there anyway to generate via database? Or we have insert via code?

You could just add a default constraint:
ALTER TABLE TableName
ADD CONSTRAINT DF_BookingNo DEFAULT(format(SYSDATETIME(),'yyMMddHHmmssff'))
FOR BookingNo
This way you will get the value only for newly created rows.
Please note that if this column has a unique constraint on it some inserts might fail if they are executed at the same time.

Related

An error occurred while the batch was being executed

I am trying to add a foreign key to my database table, but I am getting this error:
An error occurred while the batch was being executed.
Possible data issues
The column [dbo].[Transactions].[Expenses_Id] on table [dbo].[Transactions] must be added, but the column has no default value and does not allow NULL values. If the table contains data, the ALTER script will not work. To avoid this issue you must either: add a default value to the column, mark it as allowing NULL values, or enable the generation of smart-defaults as a deployment option.
Warnings
The column [dbo].[Transactions].[Expenses_Id] on table [dbo].[Transactions] must be added, but the column has no default value and does not allow NULL values. If the table contains data, the ALTER script will not work. To avoid this issue you must either: add a default value to the column, mark it as allowing NULL values, or enable the generation of smart-defaults as a deployment option.
But my other table (the one I am referring to using the FK) is not empty, so there should not be any null exceptions.
You are attempting to add new column which does not allow NULLs. Since there are some rows already, what value should be in every row right after column creation? That is NULL or default if defined. Create it with allowed NULLs, update to real values, alter to disable NULLs.

Updating identity column in SQL Server and setting the seed starting value

I have a table filled with data and one of the columns - TrackingNumber - is an integer value. I have gotten a request to change it to auto-increment and to have it start the identity seed at 1000000. I know that I cannot alter a column to be an identity column in an existing table with data, so I have two options: either create an entirely new table and then move data from the old table into that new table or add an new identity column and update it with data from the old column.
The problem is that I need to retain all the existing values in column TrackingNumber. I have tried the following:
1) ALTER TABLE [dbo].[Table1]
ADD [TrackingNumber2] [bigint] IDENTITY (1000000, 1) NOT NULL
2) UPDATE [dbo].[Table1]
SET [TrackingNumber2]=[TrackingNumber]
3) ALTER TABLE [dbo].[Table1]
DROP COLUMN [TrackingNumber]
GO
4) EXEC sp_rename 'Table1.TrackingNumber2', 'TrackingNumber','COLUMN'
I got an error on step 2 - Updating new column with the value from the old column: "Cannot update identity column 'TrackingNumber2'"
Can anyone recommend a workaround?
You just need to set identity_insert on for this table so you can update the values. Make sure you turn it back off when you complete the update. :)
https://msdn.microsoft.com/en-us/library/ms188059.aspx
Are you sure you need to use an identity column? There are alternatives. For example, since SQL Server 2012 (and azure, too), there are these things called sequences. You can define a sequence to start at any number you like:
create sequence dbo.TrackingSequence
as int
start with 1000000
increment by 1
no maxvalue
no cycle
no cache
Then, you can alter the table such that the default value for the column in question defaults from the sequence:
alter table dbo.MyTable
add constraint [MyTable.TrackingNumber.Default.TrackingSequence]
default( next value for dbo.TrackingSequence ) for TrackingNumber
(If the column already has a default value, you need to remove that first - in a separate statement.)
A sequence works a lot like an identity, but you don't have to disrupt existing values or the column definition per se.
The only trick is to remember to not specify the value for TrackingNumber, and let the DB do its thing.
Sequences are cool in that you can have one sequence that is used by multiple tables, giving you somewhat shorter db-wide unique IDs than alternatives in the past. For such an application, you'd probably be better off with a bigint column - or know that the tables in question aren't going to be terribly long.
I ended up creating a new table and moving data in there

i started this replication wizard it adds GUID FIELD, how should i add data to this manually in future?

using replication in sql srv causes the addition of this guid field, it also adds a value to it
but when i insert new records to the db, i have to give somthing or the guid field
it should be like aaaaa-aaa-something and unique!!
this is a problem for me , how am i supposed to do this keeping it unique every time?
should sql srv automatically add some yhing?
The ROWGUIDCOL added by Merge Replication should be populated with... guids:
ROWGUIDCOL does not enforce uniqueness
of the values that are stored in the
column and does not automatically
generate values for new rows that are
inserted into the table. To generate
unique values for each column, either
use the NEWID function on INSERT
statements or specify the NEWID
function as the default for the
column.

Compute hash-value of entered value when insert or update using sql server 2008 by triggers

I have a table with two columns {FlatContent, HashedContent}. Now I want to automatically compute the hash value of FlatContent when new row was inserted or an existing row was updated. To date, I've never used from trigger, so I can't do this by trigger or another approach which is exist to solve this issue.
Thanks if anybody can help me ;)
Instead of using a trigger, make HashedContent a persisted computed column in your table definition.
ALTER TABLE YourTable
ADD HashedContent AS HashBytes('SHA1', FlatContent) PERSISTED

DB2 override auto generated key when inserting

I want to insert an item to a table in the database. The table has a key that is auto generated. Is it possible to override the auto generated key, (force a value). If so how?
I'm going to assume you are talking about identity columns and not sequences.
In DB2's CREATE TABLE syntax, have a look at the "generated-column-spec" in the syntax diagram as it relates to identity columns. There are two ways to specify how the identity value will be generated:
GENERATED ALWAYS: this option will always generate and identity value, and you cannot specify a value for the identity column in an insert statement
GENERATED BY DEFAULT: this option will generate an identity value if no value for the column is specified in the insert statement. If you provide a value for the column in the insert statement, db2 will not generate an identity value for it.
If the table you are trying to insert into used the ALWAYS option when the table is created, then you cannot override it. You'll need to drop and recreate the table or use the ALTER TABLE statement to redefine the column to generate the identity value by default only.
If you're trying to LOAD data into a table that has an identity column which is GENERATED ALWAYS then you can do this:
db2 load from tab43.ixf of ixf modified by identityoverride into tablename

Resources