0
Table:# result
Name      Issues    Years      Value
Rani    Critical    2018        0.90
Rani    Critical    2018        0.00

Expected Output:-
Name      Issues    Year       Value
Rani    Critical    2018       0.90
Rani    Critical    2018       0.00
Rani        Null   2019        0.00


select Name=’Rani’,Issues=' Critical', years, ,Value from #result;

Insert #result (Name,years,Value) values(‘Rani’,year(getdate()),0)  

I want to perform insert Operation on the #result table only when Current year(eg:-2019) Is not exists. If current year exists on #result table insert operation should not perform .

Srikanth
  • 29
  • 8
  • 1
    Possible duplicate of [SQL Server insert if not exists best practice](https://stackoverflow.com/questions/5288283/sql-server-insert-if-not-exists-best-practice). The question is for SQL Server but the answer is standard sql and applies here too. – Joakim Danielson Apr 16 '19 at 08:29

1 Answers1

2

You can use IF NOT EXISTS to carry out this operation.

DECLARE @year INT = YEAR(GETDATE())
IF NOT EXISTS(SELECT 1 FROM #result WHERE Year = @year )
BEGIN
Insert #result (Name,years,Value) values(‘Rani’,@year,0)  
END
Venkataraman R
  • 12,181
  • 2
  • 31
  • 58