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?
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
2
Hares FakAuthor Commented:
Thanks but that's not what I'm looking for
0
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
0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

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
0
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

1

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
0
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

2
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)
1
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
0
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
0
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.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.