This is probably more of a “best practices” question than a true code question.
I have a stored procedure to update a table, but needs a couple of values from another table.
the first two selects get the value from the table and then are used in the update statement.
The select statments:
Select @iStatusDropDownValueID = iDropDownValueID
From DropDownValue
Inner Join DropDownValueType On DropDownValue.iDropDownValueTypeID = DropDownValueType.iDropDownValueTypeID
Where DropDownValueType.vchDropDownValueTypeName = 'Status'
AND DropDownValue.vchDropDownValueName = 'Scheduled'
AND DropDownValue.tiRecordStatus = 1
And DropDownValueType.tiRecordStatus = 1
Select @iLastStatusDropDownValueID = iDropDownValueID
From DropDownValue
Inner Join DropDownValueType On DropDownValue.iDropDownValueTypeID = DropDownValueType.iDropDownValueTypeID
Where DropDownValueType.vchDropDownValueTypeName = 'Status'
AND DropDownValue.vchDropDownValueName = 'CriticalErrorOccurred'
AND DropDownValue.tiRecordStatus = 1
And DropDownValueType.tiRecordStatus = 1
The Update statement:
Update Schedule
Set
iStatusDropDownValueID = @iStatusDropDownValueID,
iLastStatusDropDownValueID = @iLastStatusDropDownValueID,
iCurrentLogID = @iCurrentLogID,
dtNextDate = @dtNextDate,
dtLastDate = @dtLastDate,
iRetryCount = @iRetryCount,
iFailedCount = @iFailedCount,
iBusyRetryCount = @iBusyRetryCount,
vchUpdateBy = @vchUpdateBy,
dtUpdateDate = @dtLastDate
WHERE
iScheduleID = @iScheduleID
First, the values that are retrieved by the first two select statements are always the same. So they could be passed in by the code itself. I don’t know that this will speed things up at all, just make the entire stored procedure better and easier to read.
Second, if the “Value Name” should change this store procedure will break (which is possible, but not often).
I am looking for any insight into the Best Practices for this situation.
Personally I always declare my variables and don’t use @vars.
@vars are like variants, they don’t protect you against typos.
If you use explicitly declared variables, MySQL will warn you against typos and your vars will be typed making your intentions clearer and your code slightly faster.
If you need to use the outcome of one stored procedure, why not make a stored function.
Then you can just write a query like: