SQL Server 2008 - Use IDENTITY field when inserting a row - sql-server-2008

My table has 3 columns ID, ISOCode and CountryName.
Column ID is an IDENTITY column.
When I insert new records into this table, I want to populate the ISOCode field - on occasion - with the same value as the ID field.
I've tried SCOPE_IDENTITY(), ##IDENTITY and IDENT_CURRENT but none of these seems to work.
Is there a way I can do this?

you could write a trigger to update the ISOCode column
In Trigger use the code below
update T set T.ISOCode=I.ID
from your_table T
join INSERTED I
on T.ID=I.ID

Related

Sum up columns using group by and store as a new column (permanently) in MySQL

I have 50 columns in the MySQL table. I want to sum all these columns and make a new column into the same table (sum50).
This should be stored permanently into the MySQL table whenever I update or insert new data.
I know how to sum up while performing the query but it does not store into the table permanently.
CREATE TRIGGER `name`
AFTER INSERT ON `table`
FOR EACH ROW
UPDATE `table` SET `table`.`coulms` = `table`.`col1`+.... `table`.`col50`+
I am trying the above trigger, but not working.
Should I have a blank column inserted into the table and perform trigger? Which trigger would be correct?
Instead of the trigger, add a generated column to your table:
alter table tablename add(sum50 int generated always as (col1 + col2 + ...) stored);
See a simplified demo.

how to copy data from one SQL column table to another SQL column table

Need assistance on the following. How can I copy data from one SQL column table to another sql column table?
I have the following tables
dbo.Thecat62 and dbo.thecase6
inside dbo.Thecat62 , I need to copy the column Work_Order_Job_Start_Date values to dbo.thecase6 column Job_Start_Date. Currently there are null value in the Job_Start_Date column in dbo.thecase6.
I have tried using the following command
INSERT INTO dbo.thecase6 (Job_Start_Date)
SELECT Work_Order_Job_Start_Date
FROM dbo.thecat62
but received the error Cannot insert the value NULL into column 'CaseNo', table 'Therefore.dbo.TheCase6'; column does not allow nulls. INSERT fails.
The statement has been terminated.
Any help will be great!
Thanks!
Because on this table Therefore.dbo.TheCase6 for CaseNo you have specify Not NULL Constraints
something like this
CaseNo int NOT NULL
But you did not select the CaseNo column from the dbo.thecat62 table, so you are explicitly trying to insert nulls into a non-nullable column.
You just need to select the CaseNo column, as well, presuming it does not contain any nulls in teh source table.
INSERT INTO dbo.thecase6 (Job_Start_Date,CaseNo)
SELECT Work_Order_Job_Start_Date,CaseNo FROM dbo.thecat62
The error says it has a column CaseNo which doesn't allow NULL.
Cannot insert the value NULL into column 'CaseNo', table 'Therefore.dbo.TheCase6';
You are inserting rows in the new table which will have just 1 column filled and rest of the columns will be empty
Either
Alter the table in which you are inserting the data and allow the column to allow null values.
Or
if you don't want to allow null values, update the null values to some default values.

Computed field value based on other column while insertion

I have a table where I have an ID column(primary key auto increment) and one more column for name.
I want to fill name column's value automatically while insertion based on generated ID column value in format
<IDColumnValue>_School
I am aware of the two ways to do this
using trigger
inserting the row first and then update its column value based on the inserted row id column value
But actually I want to make this field Non Nullable but to use the second option I will have to make it nullable.
Is there any direct way to do this while inserting row so that I can have the field non nullable?
As I said in the comment, you can use a temporary value for the name column. You can use a request like :
INSERT INTO `table` VALUES('', 'name') /*let's assume name is the real name you want to insert*/
I'm not sure then how to use a trigger, but you may want to write something like this :
delimiter #
CREATE TRIGGER update_name_after_insert INSERT ON `table`
for each row
begin
update `table` set name = CONCAT_WS("_", id, name)
end#
It work for firebird but I think it must work in MySQL because MySQL have new/old operators. Bellow trigger for table XYZ with fields B and C (not null).
CREATE OR ALTER trigger xyz_bi0 for xyz
active before insert position 0
AS
begin
new.c=new.b||' some text';--this construction must work in MySQL
end

How to update MySQL database table cell based on the values in other cell

I've this MySQL database table called "DataField". It has an incrementing Primary Key column called "ID", another column called "Name" and another column called "LocType". What I would like to do is, as soon as the ID and Name fields are updated in the "DataField" table I would like to also update the "LocType" table automatically with value "1".
Can someone explain me how to do this?
You can use Trigger to achieve this. Try something like the following code,
CREATE TRIGGER update_LOC_TYPE
BEFORE UPDATE ON DataFiled
FOR EACH ROW BEGIN
UPDATE LocTYPE
SET Field_Name = 'New Value';

Trouble with MAX(col)+1 INSERT into same MySQL table

I need two id columns in the same table that create unique values upon insert. Since MySQL allows only one column to auto-increment, I need to do something like max(id)+1 for the other column.
Here's the SQL I expected to work:
INSERT INTO invoices (invoiceid)
VALUES ((SELECT MAX(invoiceid)+1 FROM invoices))
The select statement works independently, but within my INSERT, it's not allowed. I get the error : You can't specify target table 'invoices' for update in FROM clause
You want to use INSERT INTO .... SELECT FROM instead of INSERT INTO...VALUES():
INSERT INTO invoices (invoiceid)
SELECT MAX(invoiceid)+1
FROM invoices
My question for you would be why are you not use an AUTO INCREMENT field to generate the invoiceid value? That is what it is for, then you will not have to create this when inserting data.