Creating Stored Procedure to compare Date Column and update Status Column - sql

I need a stored procedure to be created for comparing EXPIRYDATE column with SYSTEMDATE and if they match equal, then an additional column called MATCHSTATUS must be updated with value 1 and if they don't match then it must be update with value 0.
Both EXPIRYDATE and MATCHSTATUS are present in the same table.
Example:
username password expirydate matchstatus
-----------------------------------------------------
abc 987 01-Feb-2015 1
xyz 678 10-Feb-2015 0
ghi 456 15-Jan-2015 0
In this example, the match status column should be update by comparing expirydate column with sysdate and it should be done by a stored procedure.

Code can be something like that
Declare #crntdt NVARCHAR(20)
Declare #mydt NVARCHAR(20)
Select
#crntdt = REPLACE(rtrim(ltrim(CONVERT(CHAR(15), getdate(), 106))),' ',' - ')
--select #crntdt
select #mydt=expirydate from your_table
if #crntdt=#mydt
begin
update your_table set matchstatus=1 where expirydate=#mydt
end

This is what you need I think - this assumes expirtydate is a date, if it's varchar you will need to cast it as a date:
CREATE PROCEDURE sp_name
as
Begin
Update tbl
set matchstatus = case when expirydate = cast(GETDATE() as date) then 1 else 0 end
END

Related

Write a cursor to search and insert

I have tables like that:
Main
id name isedit
1 kyle 0
2 jhon 1
3 dave 0
EditHistory
id idmain name isedit Begin end
1 2 jhon 0 28.05.2020 18:30 28.05.2020 18:35
2 2 jhon 0 28.05.2020 18:35 NULL
3 1 kyle 0 27.05.2020 12:03 NULL
I currently use trigger:
(…) if update(isedit) and exists (
select 1
from Inserted I
where design = 0
) begin
Insert into dbo.HistoryEdit
([idmain][name][isedit][Begin][end]) SELECT id, name, iedit, GETDATE(), null
from Inserted
end;
I need to create cursor that will check through EditHistory for previous rows with same idmain and if there is such row edit its end date to GETDATE() and insert into HistoryEdit as in my current insert.
I know it can be easily done with IF's and thats how I would do it. But I have to use cursor for that, and I never used cursor before.
I never used cursor before.
Well don't start now. Just update the older rows before you insert the newer ones:
declare #d datetime = GetDate()
update EditHistory set end = #d
where id in (select id from inserted)
and end is null;
Insert into dbo.HistoryEdit
([idmain][name][isedit][Begin][end])
SELECT id, name, iedit, #d, null
from Inserted

Only begin stored procdure if previous value is less than new value being inserted into table SQL Server

I have a stored procedure. I only want it to insert new values if the date value is higher than previous record.
Department Change Date
------------------------------------
Catering 3 2018-01-01 08:09:00.000 - Current Record
Department Change Date
-----------------------------------------------
Catering 3 2018-01-02 09:10:00.000 - New record to be inserted
I have this so far, but obviously the select is wrong. I want to do something like this before the Insert statement
if select top 1 *
from database.[dbo].[DepartmentChange]
where Department = #Department
and Date > #Date
order by Date desc
begin
please help
Pretty vague details here but I think you are looking for something like this.
if not EXISTS
(
select *
from database.[dbo].[DepartmentChange]
where Department = #Department
and Date > #Date
)
begin
--do your insert
end
else
begin
--do something else here
end
Vague question. I'm assuming #Date, #Department are from the new record. So maybe like this . . .
if #Date > (select isnull(max(date),'1500-01-01') from database.[dbo].[DepartmentChange]
where Department = #Department)
begin
insert statement here
end

SQL Server: how to update a column with a value that is in that column when another number in another column is >1

I have a table with the following data:
Part Comp level item_nbr
-------------------------------
abc ab 1 1
null cd 2 2
null ef 3 3
cde gh 1 4
null ij 2 5
null kl 3 6
null mn 4 7
I would like to update the nulls to the value in each level 1, so every level that is >1 is updated with the level one value.
Part Comp level
---------------------
abc ab 1
abc cd 2
abc ef 3
cde gh 1
cde ij 2
cde kl 3
cde mn 4
I am at a loss as to how to achieve this on a very large dataset. Any help would be greatly appreciated!
To explain another way,
part level
abc 1
2
3
Then the next row is populated with another part
efg 1
2
2
etc.
Further clarification:
I need the string"abc" to be filled down with the string "abc" while the column fields below are null. The next row has a string of efg and the following column fields below are null, again, those fields should be filled down with the value "efg" and so on.
The level field = 1 will always have a part number, but all the other levels report up to the level 1 part, so should be populated identically. And repeat.
Hope this makes sense.
Use an updatable CTE with window functions:
with toupdate as (
select t.*,
max(part) over (partition by itm_nbr_not_null) as new_part
from (select t.*,
max(case when part is not null then item_nbr end) over (order by item_nbr) as itm_nbr_not_null
from t
) t
)
update toupdate
set part = new_part
where part is null;
You can run the CTE to see what is happening.
well, from your question what I understand is, you need to update the null column's value until you get a not null value. and you want to continue it up to the last row of the table.
for that scenario, I created a stored procedure, where I read the value of every n-th cell if it is null I changing it with the prev. cell's value, when the cell was not null.
Approach:
create a temporary table/ table variable.
add an extra column, which is basically identity, which will help to rank the column.
iterate a loop until the maximum row is reached.
in each iteration, read the cell value for the i-th row
4.1 if it is not null put it in a temporary variable.
4.2 else, replace/update the i-th cell's value with the temporary variable
continue it, until you reached up to the last row of the table/table variable.
look at my following snippets:
create proc DemoPost
as
begin
declare #table table(serial_no int identity(1,1), name varchar(30), text varchar(30), level int)
insert #table
select Name, Text, Level from Demo
declare #max as int = (select max(serial_no) from #table)
--select #max
declare #i as int =0
declare #temp as varchar(30)
declare #text as varchar(30)
while #i < #max
begin
set #i = #i +1
set #temp = (select name from #table where serial_no = #i)
-- if #temp is not null, fetch its value, otherwise, update/replace it with
-- previously gotten not-null cell's value.
if #temp is not null
begin
set #text = (select name from #table where serial_no = #i)
end
else
begin
update #table
set name = #text where serial_no = #i
end
end
select name, text, level from #table
end
You can update it using temporary table according to the given scenario i thought item_nbr is unique in row Hope this will help
SELECT *
INTO #TEMP
FROM URTablehere
DECLARE #PRev VARCHAR(MAX)
WHILE ( SELECT COUNT(*)
FROM URTablehere
) > 0
BEGIN
DECLARE #ID INT
DECLARE #Part VARCHAR(MAX)
DECLARE #Num INT
SELECT TOP ( 1 )
#ID = level ,
#Part = Part ,
#Num = item_nbr
FROM #TEMP
IF ( #ID = 1 )
BEGIN
SELECT #PRev = #Part
END
IF ( #ID > 1
AND #Part IS NULL
)
BEGIN
UPDATE URTablehere
SET Part = #PRev
WHERE item_nbr = #Num
END
DELETE
FROM #TEMP WHERE item_nbr=#Num
END

Understanding a stored procedure SQL-Server

i am trying to understand what a stored procedure is doing but i am struggling.
Here is the code
DECLARE
#person int,
#year int,
#default float = 0
IF EXISTS (SELECT 1 FROM Table1 WHERE PERSONID = #person AND YEAR1 = #year
AND TYPE1 = 'A')
BEGIN
UPDATE Table1 SET DAY1 = #default
WHERE PERSONID = #person AND YEAR1 = #year AND TYPE1 = 'A'
END
ELSE
BEGIN
INSERT INTO Table1 (PERSONID, YEAR1, DAY1, TYPE1)
VALUES (#person, #year, #default, 'A')
This procedure takes data from a website and inserts it into a table into a database from my knowledge. But i cant see where it takes it from. Its just updating or inserting the existing table. Can anyone give any advice as to what this might be doing?
Thanks
Assuming #person, #year and #default are inputs to the stored procedure it is checking whether any record(s) exist in the Table1 table with the specified #person and #year and the Type1 field equal to the value A
If the record(s) exists it is updating that table's Day field with the specified #default value.
If the record(s) do not exist, it is inserting a new record with the specified #person, #year and #default and the Type1 value of A.

Need Query - Row count with loop

I have a Primary table like
========================================
ID NAME logtime (date time colum)
========================================
1 cat dd/mm/yyyy 10.30
2 cat dd/mm/yyyy 9.20
3 cat dd/mm/yyyy 9.30
4 cat dd/mm/yyyy 7.20
Secondary Table like
---------------------
Name improvement
---------------------
cat 1
Now I want to create a loop
To calculate difference between first 2 rows, if the difference is >= 1 hr then update the secondary table as existing value +1(that is 2) else rest it.
Now calculate 2'nd and 3'rd rows,
To calculate difference between first 2 rows, if the difference is >= 1 hr then update the secondary table as existing value +1, here the answer is 2.
Then 3'rd and 4'th.
Once all row are calculated then exit from the loop.
can anyone give me a query for this?
declare #timediff table(id int identity(1,1),Name varchar(50),logtime datetime)
insert into #timediff Select Name,logtime from timediff
declare #datetimeDiff int
declare #i int=1
while(#i<=(Select count(*) from #timediff))
Begin
Select #datetimeDiff=datediff(hour,logtime,
(Select logtime from #timediff where id=#i+1))
from #timediff where id=#i
if(#datetimeDiff>=1)
BEGIN
Select #datetimeDiff
--You can write your update code here
END
Set #i=#i+2
END