Hares Fak
asked on
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?
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?
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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
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
ASKER
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
@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
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thanks
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 :
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
NO... the chosen solution by HuaMin Chen DOES NOT WORK
And it is INSERTING a row into Patient where the only value is ReleaseDate (unless you have an identity in there)
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".
And it is INSERTING a row into Patient where the only value is ReleaseDate (unless you have an identity in there)
ASKER
@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
@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.
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
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
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
ASKER