Avatar of Jeff S
Jeff SFlag for United States of America

asked on 

Insert Record if no records found - SQL

I need to insert a record if no records exist. In my example below, It returned no records; therefore I need it to return the current date for the created, for CreatedBy it can be  'N/A' and Description equal to 'No Correspondence Notes'.

DECLARE @PATIENTVISITID INT = 695

SELECT
	  pc.Created
	, ISNULL ( NULLIF ( REPLACE ( pc.CreatedBy , CHAR ( 0 ) , '' ) , '' ) , 'System' ) AS CreatedBy 
	, CASE
		 WHEN pc.Description = '**long**' THEN pc.DescriptionLong
		 ELSE pc.Description
	  END AS Description
FROM PatientCorrespondence pc
WHERE pc.PatientVisitID = @PATIENTVISITID;

Open in new window

Microsoft SQL ServerMicrosoft SQL Server 2008SQL

Avatar of undefined
Last Comment
Jeff S
Avatar of Mike Eghtebas
Mike Eghtebas
Flag of United States of America image

Is PatientVisitID PK? If so, then we need to add two more new lines to suppress identity effect.

Try:
DECLARE @PATIENTVISITID INT
As
Begin
DECLARE @Created dateTime = GEtDate();
DECLARE @CreatedBy varchar(50)= 'N/A';
DECLARE @Description varchar(50) ='No Correspondence Notes';
  
SELECT
	  pc.Created
	, ISNULL ( NULLIF ( REPLACE ( pc.CreatedBy , CHAR ( 0 ) , '' ) , '' ) , 'System' ) AS CreatedBy 
	, CASE
		 WHEN pc.Description = '**long**' THEN pc.DescriptionLong
		 ELSE pc.Description
	  END AS Description
FROM PatientCorrespondence pc
WHERE pc.PatientVisitID = @PATIENTVISITID;
)

if @@ROWCOUNT = 0
Begin

 Insert Into PatientCorrespondence (Created, CreatedBy, [Description], PatientVisitID) Values
                        (@Created, @CreatedBy, @Description, @PATIENTVISITID); 
End
End

Open in new window

Avatar of Jeff S
Jeff S
Flag of United States of America image

ASKER

Yes, PatientVisitId is the PK.
ASKER CERTIFIED SOLUTION
Avatar of Mike Eghtebas
Mike Eghtebas
Flag of United States of America image

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
Avatar of Jeff S
Jeff S
Flag of United States of America image

ASKER

Many thanks!
Microsoft SQL Server
Microsoft SQL Server

Microsoft SQL Server is a suite of relational database management system (RDBMS) products providing multi-user database access functionality.SQL Server is available in multiple versions, typically identified by release year, and versions are subdivided into editions to distinguish between product functionality. Component services include integration (SSIS), reporting (SSRS), analysis (SSAS), data quality, master data, T-SQL and performance tuning.

171K
Questions
--
Followers
--
Top Experts
Get a personalized solution from industry experts
Ask the experts
Read over 600 more reviews

TRUSTED BY

IBM logoIntel logoMicrosoft logoUbisoft logoSAP logo
Qualcomm logoCitrix Systems logoWorkday logoErnst & Young logo
High performer badgeUsers love us badge
LinkedIn logoFacebook logoX logoInstagram logoTikTok logoYouTube logo