Creating TSQL procedure for inserting into NULL values

Hi,

I'm trying to create a procedure to insert the current date if the value in a date column is null ~ in my example releasedate



my code however isn't working at all ~

create procedure releasepatient
@rel date = getdate
as
select patient.releaseDate  from patient
if releaseDate is null
insert into patient (releasedate) values (@rel) ;



any ideas on modifying this?
Hares FakAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Aneesh RetnakaranDatabase AdministratorCommented:
You can just add a DEFAULT value assigned to that column and update it

UPDATE patient
SET releaseDate  = GETDATE()
WHERE releaseDate  is Null
Hares FakAuthor Commented:
Thanks but that's not what I'm looking for
HuaMin ChenProblem resolverCommented:
1. Use getdate() instead
2. Do the similar to put getdate() into releasedate on insert and update trigger of table patient

Read
https://docs.microsoft.com/en-us/sql/t-sql/statements/create-trigger-transact-sql?view=sql-server-2017
Big Business Goals? Which KPIs Will Help You

The most successful MSPs rely on metrics – known as key performance indicators (KPIs) – for making informed decisions that help their businesses thrive, rather than just survive. This eBook provides an overview of the most important KPIs used by top MSPs.

Hares FakAuthor Commented:
create procedure releasepatient
@rel date = getdate()
as
select patient.releaseDate  from patient
if releaseDate is null
insert into patient (releasedate) values (@rel) ;

here we go ~ let me clarify ~ my problem is the column 'releaseDate' is not resolving after the if keyword
if releaseDate ~
because it is placed after the FROM
my basic question is if anyone has a fix for the syntax
HuaMin ChenProblem resolverCommented:
Try
create procedure releasepatient 
@rel date = getdate()
@dt datetime
as 
begin
select @dt=patient.releaseDate  from patient
if @dt is null 
begin
begin tran
	insert into patient (releasedate) values (@rel) ;
commit tran
end

end;

Open in new window

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Hares FakAuthor Commented:
Thanks
Mark WillsTopic AdvisorCommented:
INSERT will add new rows

UPDATE will amend existing rows

So, if you are checking an existing row, you will need to update that row, not insert.

Aneesh has the right idea - meaning his code snippet is what you need.

However, it will update everyone not yet released. So, incomplete in that regard.

For your Stored Proc that would be :
create procedure dbo.releasepatient @rel datetime = NULL 
as
begin 
   update patient set ReleaseDate = isnull(@rel,getdate())
   where ReleaseDate is null 
end
GO

Open in new window

Mark WillsTopic AdvisorCommented:
NO... the chosen solution by HuaMin Chen  DOES NOT WORK
Msg 102, Level 15, State 1, Procedure releasepatient, Line 3 [Batch Start Line 16]
Incorrect syntax near '('.
Msg 137, Level 15, State 1, Procedure releasepatient, Line 7 [Batch Start Line 16]
Must declare the scalar variable "@dt".
Msg 137, Level 15, State 2, Procedure releasepatient, Line 8 [Batch Start Line 16]
Must declare the scalar variable "@dt".
Msg 137, Level 15, State 2, Procedure releasepatient, Line 11 [Batch Start Line 16]
Must declare the scalar variable "@rel".

Open in new window


And it is INSERTING a row into Patient where the only value is ReleaseDate (unless you have an identity in there)
Hares FakAuthor Commented:
@Mark Willis my apologies, the chosen answer got me what I needed after modification ~ I'll post my solution when I get back around to this, the question was for testing reasons ~ in the meantime I'll mark it as unanswered and welcome additional input
Mark WillsTopic AdvisorCommented:
@Hares Fak,

I was most concerned that you may have considered it a workable solution....

A few things wrong with the approach in that post :

1) Using GETDATE() like that cannot happen - it too is a function - albeit built-in
2) Need to define @dt as a declarative
3) If you want to insert (say) an outpatient / discharge row, then it is quite a different process.
4) You need a Patient ID (or similar) because one discharge / release will action everyone else not yet released

e.g.
ALTER procedure dbo.releasepatient @patientID int, @rel datetime = NULL  
as
begin 
   update patient set ReleaseDate = isnull(@rel,getdate())
   where ReleaseDate is null
   and PatientID = @patientID
end
GO

Open in new window

Now I realise it was for a test case, but the basic structure of HuaMin Chen is fatally flawed and should not become a precedent for others to see let alone try.

I would like to see your solution. And by all means you can then select that as the 'Best' answer, along with any other posts that helped you along the way.

Cheers,
Mark Wills
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft SQL Server

From novice to tech pro — start learning today.