Loop update total field in procedures [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 8 years ago.
Improve this question
How to loop update total field in procedures in SQL Server?
I have table 1. I want to create procedures to update total field automation.
Table1
Value 1 Value 2 Total
2 4
3 4
3 3
3 1
3 4
PROCEDURE:
Create PROCEDURE test1
AS
Select value1,value2,(value1*value2) as total from Table1

UPDATE Table1 SET Total = value1*value2
Or you can make Total as calculated field in table1 and it will happen automatically.

CREATE PROCEDURE up_UpdateTotals
AS
BEGIN
UPDATE Table1 SET Total = Value1 + Value2 WHERE Total IS NULL OR Total <> (Value1 + Value2)
END
Will update totals for where it's not set or not in sync (in case values change). Can be extended by adding parameters and such, ofcourse.
Update: Change + to * in case you need to multiply the values instead of add them together.

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)

update column of table 1 according to the similar column of table 2, Sqlite3 [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 two tables in sqlite. Let's say table_1 and table_2. table_1 has two columns named(Barcode, Price) , similarly table_2 has two columns named(Barcodes, Price). I want to update table_1.Price. if table_1.Barcode is present in table_2. Barcodes, then take the price of table_2. Price otherwise keep it table_1.Price.
I hope I am very clear. Let me just write it down in a programmatic sense to be very clear.
if(table_1.Barcode == table_2.Barcodes)
table_1.Price = table_2.Price
else
table_1.Price = table_1.Price
I need sqlite query which does this. Is it possible?
If your version of SQLite is 3.33.0+ you can do it with UPDATE...FROM syntax:
UPDATE table_1 AS t1
SET Price = t2.Price
FROM table_2 t2
WHERE t2.Barcode = t1.Barcode;
For older versions of SQLite you need a corelated subquery which returns the price from table_2:
UPDATE table_1
SET Price = COALESCE(
(SELECT t2.Price FROM table_2 t2 WHERE t2.Barcode = table_1.Barcode),
Price
);

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;

find orders for three consecutive weeks [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 4 years ago.
Improve this question
I have table like the below which has overlapping weeks from two years 2018 and 2019. I want to identify the first set of 3 consecutive weeks for each customer using TSQL.
The resultset should have the customer_id and the max(order_date) from the first set of 3 consecutive weeks like below.
I created my own data. Try running this (this uses windowing functions which is a 2012 and on feature):
create table i1(i1 int identity (1,1) primary key, idate date, name varchar(25));
insert into i1 values ('1-1-2019','b'), ('1-8-2019','b'), ('1-15-2019','b'),
('1-1-2019','c'), ('1-8-2019','c'), ('1-24-2019','c'),('1-31-2019','c')
;
with ct1 as (select name,idate, datepart(ww,idate) -lag(datepart(ww,idate),1,0) over (partition by name order by idate) diff
,row_number() over (partition by name order by idate) id from i1),
ct2 as (
select name , idate,sum(diff) over ( partition by name order by idate rows between 3 preceding and current row) diff2
from ct1)
select name, idate
from ct2 where diff2=3

Conditional trigger on a 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 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

Resources