Conditional trigger on a table [closed] - sql-server

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 years ago.
Improve this question
I am unable to create a simple conditional trigger to insert data to a new table. Below is the trigger and its output:
CREATE TRIGGER [dbo].[test_trigger]
ON [dbo].[Export_Data]
FOR INSERT
AS
BEGIN
INSERT INTO test (Terminal_ID, EmpIdentification, SwipeDateTime)
SELECT
DeviceId, UserId, LogDate
FROM
Inserted
END
OUTPUT is as follows:
My requirement is that I need logs of only Terminal_ID = 32 & 33 only.
Thank you

Just add a where clause to the select statement from the Inserted table. If you're inserting Export_Data.DeviceId into test.Terminal_ID, then your filter should be where Inserted.DeviceId in (32,33):
CREATE TRIGGER [dbo].[test_trigger] on [dbo].[Export_Data]
for insert
as
begin
insert into test(Terminal_ID,EmpIdentification,SwipeDateTime)
select DeviceId,UserId,LogDate from Inserted
WHERE DeviceId in (32, 33)
end

Related

How to update dates in a table so that each row's date is pushed ahead by 1 day? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 12 months ago.
Improve this question
I have a table whose Date column needs pushing ahead by 1 day.
My update query is:
UPDATE TABLENAME
SET DATECOL=DATECOL+1
Is this correct approach? Or do I need to use CTE, for example:
;WITH CTE AS (
SELECT ID, DATECOL
FROM TABLENAME)
UPDATE T
SET T.DATECOL=CTE.DATECOL+1
FROM TABLENAME T
JOIN CTE ON T.ID=CTE.ID
To add a value to any part of date you can use DATEADD function. In your case the part time is DAY.
UPDATE TABLENAME
SET DATECOL=DATEADD(DAY, 1, DATECOL)

How to delete top (1) row from SQL if row count reached to 10000 [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
Improve this question
I have made fault history log into HMI through SQL database,
I am deleting top row by PLC TAG by using following code
DELETE TOP (10) PERCENT FROM [dbo].[ABC]
I want to delete first row while my row-count reached to 10000
in other way to say that ....
a new fault record add in log and first old record should delete and so on...
If I understansd you correctly, you need to number the rows using ROW_NUMBER() and then execute appropriate DELETE and INSERT statements:
DELETE t
FROM (
SELECT *, ROW_NUMBER() OVER (ORDER BY PLC DESC) RN
FROM [dbo].[ABC]
) t
WHERE RN >= 10000
INSERT [dbo].[ABC] (PLC) VALUES (...)

SQL Server - Update other table only if column values change [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
I have data streaming from an external program to TABLE1 in my SQL Server database. Let's say TABLE1 consists of columns data1 and data2. How do I update to another table (TABLE2) when a value changes in TABLE1?
For the sake of an example, let's say a value in the data1 column changes.
Note: I wrote a trigger that works, but this is only on the basis of a manual update. I can't figure out how to automate this, meaning the trigger would compare new and old values from the table on its own and then perform the update if a value is different.
Something like below would work,
CREATE TRIGGER after_update_table1
AFTER UPDATE ON table1
FOR EACH ROW
BEGIN
IF OLD.val1<> new.val1 THEN
/**
-- do operations on table2
**/
END IF;
END;
-- if val1 allows null we need to check by adding more conditions to the if clause like,
IF((OLD.val1 IS NULL AND NEW.val1 IS NOT NULL) OR (OLD.val1 IS NOT NULL AND NEW.val1 IS NULL) OR (OLD.val1<>NEW.val1)) THEN
/**
-- operations on table2
**/
END IF;

How to give system date while creating SQL table? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
SQL Server creating table error.
How to give system date, system date + 2 days while creating a table?
Is the system date the same as the current date?
You can try the SQL DEFAULT Constraint as shown below if I understood your question properly:
Create table Test(id int
, name varchar(20)
, dtDate datetime default DateAdd(dd, 2, getdate()))
insert into Test (id, name) values
(1, 'A')
select * from Test
Live db<>fiddle demo.

How can seprate string in three parts [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 3 years ago.
Improve this question
I want to Substring the string which is delimited
ABC/123/DEF
I want in
ABC in 1st column
123 in 2nd Column
DEF in 3rd Column
If your database in sql server you can try this:
declare #t table (name varchar(50))
insert into #t values ('ABC/123/DEF')
select PARSENAME(replace(name,'/','.'),3),PARSENAME(replace(name,'/','.'),2) , PARSENAME(replace(name,'/','.'),1) from #t

Resources