I am writing some UPDATE sprocs. One is below.
When I execute the procedure, enter an EthnicityCode and a new description, I get back the error message "Incorrect syntax near keyword 'WHERE'". Note that the ALTER works OK. It's just when I execute that I get the error.
TIA,
Bill
When I execute the procedure, enter an EthnicityCode and a new description, I get back the error message "Incorrect syntax near keyword 'WHERE'". Note that the ALTER works OK. It's just when I execute that I get the error.
TIA,
Bill
Code:
set ANSI_NULLS ON
set QUOTED_IDENTIFIER ON
GO
ALTER procedure [dbo].[Admin_UpdateEthnicityCodes]
(@EthnicityCode varchar (2),
@Description varchar (50),
@Message varchar (50) output)
as
--declare variables
Declare
@Continue bit,
@SQL varchar (500)
SET @Continue = 1
SET @Message = ''
Begin TRANSACTION Admin_UpdateEthnicityCodes
--validate that the Ethnicity code entered by the user
--exists in the table
IF @EthnicityCode IS NULL OR datalength(@EthnicityCode) = 0
BEGIN
SET @Message = 'Please Enter an Ethnicity Code'
SET @Continue = 0
End
ELSE
BEGIN --start building the SQL string. Note UPDATE and SET keywords
SET @SQL = 'UPDATE tbl_Admin_EthnicityCodes SET '
--note single quotes
--note space before and after SET above
IF Datalength(LTRIM(@Description)) > 0 --there is a desc update
BEGIN --put new description info into @SQL variable
-- with first @SQL line from above
SET @SQL = @SQL + 'Description = ''' + @Description + ''','
END
--finish building the SQL string
Set @SQL = @SQL + 'WHERE EthnicityCode = ' + @EthnicityCode
--Execute the SQL String
EXECUTE(@SQL)
--check for errors
IF @@Error <> 0 --there is an error
Begin --if error, Rollback Tranaction
RollBack Transaction Admin_UpdateEthnicityCodes
RaisError ('Update Failed.', 18, 1)
Return 99
end
ELSE --no errors. COMMIT Transaction
Commit Transaction
Return 0
end