I have two tables called ALUMNO and ALUMNO_GENERAL.
I would like to create a stored procedure which automatically chooses which table will update by looking at the variable @campo declared as a parameter to the stored procedure.
When I execute the procedure below, it says 1 row affected, but does not update the value of the table.
exec updateAl 'CALLE','0015','ROJO' Stored procedure:
create procedure updateAl @campo varchar(30), @matr varchar(10), @newVal varchar(15) as begin if @campo IN ('AP_PATERNO' , 'AP_MATERNO', 'NOMBRE', 'GRUPO', 'TURNO' , 'SEMESTRE' , 'STATUS' , 'NUMEMPLEADO') begin select @campo declare @sql varchar (1000) select @sql= 'update alumno set '+ @campo+'='+@newVal +' where MATRICULA='+@matr exec(@sql) end else update ALUMNO_GENERAL set @campo =@newVal where MATRICULA=@matr end
elseisn't going to work as-is. So if your@campois causing it go to theelsethen you'll have to fix that up to be dynamic as well.QuoteName()to avoid problems with odd names, e.g.New Tablewith a space.