We help IT Professionals succeed at work.
Get Started
Private

Insert Record if no records found - SQL

232 Views
Last Modified: 2015-08-09
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

Comment
Watch Question
Database and Application Developer
Commented:
This problem has been solved!
Unlock 1 Answer and 4 Comments.
See Answer
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE