Simple trigger in SQL Server 2008 - sql

Can anybody tell me how can I update a column of a record when it is inserted to the database.
Here's the pseudocode that I want.
if( mytable.OriginalId == null )
mytable.OriginalId = Scope_Identity();

This would be contradictory I think. Either OriginalId is set or it isn't on insert.
If it isn't where does Scope_Identity() get it's value from? If it's from another column then would it not be better to use a computed column?
Sorry, it's not clear what the overall objective is... you would not normally use a trigger especially if OriginalId is part of the PK

Related

How to query a condition on a single object of sql record?

I'll try my best to explain the situation here. We are using Maria DB.
In certain case we are looping over all the records/rows of a REQUESTS table.
and for each object/record we are checking few conditions and if condition passes we are performing an action. All the said things here are being done using ruby. Say I want to check the same on pure SQL, I wanted to see how.
Sample Object a Request table:
[{"ot_rqst_id":27460354,"rqst_type_cd":"NONTP","svc_type_cd":"TD","sl_ttl_type_cd":"","lot_num":41843022, ttl_stg_cd: 'CMPLT'}]
What I want to check on sql:
To see if the ttl_stg_cd of this object is equal to 'CMPLT'. If it is then update it to NULL else "something_else".
Let me remind you again that, I am already doing this in ruby language, but I am not an expert on sql so help would be appreciated.
The following query should do what you want.
I have taken the id from your query string. You need to put in the real table name and check the WHERE clause. If you remove the WHERE clause it will update all records in the table, you may wish the change it to something like
WHERE column_X = value_Y
UPDATE table_name
SET ttl_stg_cd = CASE ttl_stg_cd
WHEN 'CMPLT' THEN null
ELSE 'something_else' END
WHERE ot_rqst_id = 27460354;

Cannot use OUTPUT clause with INTO clause when using Triggers to update records in SQL, when adding a record in FileMaker

I know a similar question to this has been asked a fair few times on here, but none seem to give me the answer I need - specifically because I am talking about the relationship between SQL and FileMaker.
See, I'm not actually using an OUTPUT clause at all in my Trigger! But FileMaker seems to either think I am, or insert something itself as an OUTPUT clause that I can't see.
Here is an example trigger:
CREATE TRIGGER [dbo].[TRG_person__name]
ON [dbo].[person]
AFTER INSERT,UPDATE
AS
BEGIN
-- SET NOCOUNT ON added to prevent extra result sets from
-- interfering with SELECT statements.
SET NOCOUNT ON;
IF UPDATE ( person_name_first )
BEGIN
UPDATE person
SET person_name_first = dbo.mslTitleCase ( i.person_name_first )
FROM person AS x
JOIN inserted AS i ON x._pk_person = i._pk_person
WHERE x._pk_person = i._pk_person
END
--END IF
END
The mslTitleCase function has been declared and works, if I was to create a record using a SQL Query.
But I can an error when creating a new record in FileMaker.
Does anyone have any tips to find a way to stop this error in FileMaker?
Any guidance greatly appreciated.
Thank you
Lewis
This has been reported as a bug: http://help.filemaker.com/app/answers/detail/a_id/7870/~/external-sql-data-sources-(ess)%3A-unable-to-write-to-a-table-with-triggers-on
It does not look like it was resolved.
You may have other problems, but the correct syntax for the update is:
UPDATE p
SET person_name_first = dbo.mslTitleCase ( i.person_name_first )
FROM person p JOIN
inserted i
ON p._pk_person = i._pk_person
WHERE p._pk_person = i._pk_person;
The important change is that the update references the table alias not the table name. I think p is a much better alias than x for a table named person.
It sounds like your application is using the OUTPUT clause to directly return the modified data. If the table has any trigger, this is not allowed. They would have to output the modified data into a table, and then select the data from the table.
The issue is covered in this article:
https://blogs.msdn.microsoft.com/sqlprogrammability/2008/07/11/update-with-output-clause-triggers-and-sqlmoreresults/
If this is the case, you will not be able to add a trigger to the table. The corrections to the data will need to happen from an external source.

Firebird trigger translated to MS SQL Server

I am converting a Firebird database to MS SQl Server. As there are multiple applications accessing the database, I really want to have the MS SQL Server act in as similar way as possible as to the Firebird database.
In Firebird it is declared as
CREATE TRIGGER CUSTOMER_BI FOR CUSTOMER
ACTIVE BEFORE INSERT POSITION 0
as
begin
if (new.cust_id is null) then
new.cust_id = gen_id(gen_cust_id,1);
end
So I have a Sequence (Generator in FB) called gen_cust_id
and my main objective is to fill the field cust_id with the nextvalue from the Sequence.
I am very much aware that the SQL Server offers me an autoinc field. This is not really what I am looking for here, as the frontend application(s) do this in various manners. Some of them get a sequence number first and may or may not commit the record. I do in this case just discard the generated sequence number.
Any help is highly appreciated.
Thanks in advance
Since the ANSI sequence was not implemented until SQL 2012, you should check out this article. I've used these suggestions to make use of sequences in SQL 2005 - 2008 for a while with great results.
http://blogs.msdn.com/b/sqlcat/archive/2006/04/10/sql-server-sequence-number.aspx
So, using option 2 (my preferred), you might have a trigger looking like below.
Note: this only works on a single row insert. If you want more than 1, you need to modify the example code in the link to give you ranges and do a set-based solution to address each null id row in the inserted "table".
CREATE TRIGGER dbo.CUSTOMER_BI
ON dbo.CUSTOMER INSTEAD OF INSERT
AS
BEGIN
DECLARE #sequence_id INT;
IF EXISTS(SELECT * FROM INSERTED WHERE cust_id IS NULL)
BEGIN
EXEC #sequence_id = dbo.GetNewSeqVal_Customer;
END
INSERT INTO CUSTOMER
(
cust_id,
<col list>
)
SELECT
ISNULL(cust_id, #sequence_id),
<col list>
FROM INSERTED;
END
the position 0 only matters if there is more than one trigger on the table and if there is the mssql side can merge them into 1

My SQL Update Statement updates even if the value is the same

I want to update a column value. But my Update procedure statement updates even the value of the this column is the same.
UPDATE TableName
SET ColumName=#ParameterName
WHERE Id=#ParameterId
Any Idea?
Thank you.
From what I get from your post, you are wondering why MySQL is updating even when the value is not changed. Well, you don't check beforehand if the value is really different, so that's just what an update statement with an ID does...
You can add additional condition AND (COALESCE(ColumName,'')<>COALESCE(#ParameterName,'')
so you're only updating when those are different.
UPDATE TableName
SET ColumName=#ParameterName
WHERE (Id=#ParameterId) AND (COALESCE(ColumName,'')<>COALESCE(#ParameterName,''))
The coalsece in my example assumes that ColumnName is of type varchar if is a numeric value use AND (COALESCE(ColumName,0)<>COALESCE(#ParameterName,0) instead.

How to get last inserted id on ms sql server with FireDac component?

with FireDac, How to get last inserted id on ms sql server?
thanks
Make the that id to a identity column and then get it by Using
SELECT SCOPE_IDENTITY()
after the insert statement
Please refer the following links
http://msdn.microsoft.com/en-us/library/ms190315.aspx
Use auto-incremented field type
http://www.da-soft.com/anydac/docu/Auto-Incremental_Fields.html
This would provide for code like
DataSet.Insert;
....
DataSet.Post;
id := DataSet.FieldByName('ID').AsInteger;
Another approach might be crafting proper SQL statements like described at
http://en.wikipedia.org/wiki/Insert_(SQL)#Retrieving_the_key
SQL Server - Return value after INSERT
Best way to get identity of inserted row?
AnyDAC author also suggests a special method to fetch DBMS-specific toolings via http://docs.embarcadero.com/products/rad_studio/firedac/uADCompClient_TADCustomConnection_GetLastAutoGenValue#String.html
But all those post-factum requests with SELECT ##identity or SELECT SCOPE_IDENTITY are fragile and dangerous. When you insert data into table A, its triggers may insert data into related tables B and C, and identity would recall C's autoinc, rather than table where you started inserting at.