Alternating way of checking records in Sql-Server.

Posted by Rajesh_Kumar under Sql Server category on | Points: 40 | Views : 372
Count function is used for checking whether record is there in table or not.It returns number of record count if record is present otherwise returns 0.

For Example:-

Declare @rec_cnt Int = 0;
Select @rec_cnt = count(code_id) FROM code_details WHERE code_value ='SUBM';
print @rec_cnt
if(@rec_cnt <= 0)
INSERT code_details(code_id,code_value,created_by,created_date)
VALUES (1,'SUBM','RAJESH.S',getdate())
else print 'Already Exists.';

Comments or Responses

Login to post response