I have a stored procedure in which I add a query to a variable (by topic of dynamic tables). The problem is that when you enter a date and the table does not exist, the PRC falls. That's why I need to handle mistakes and I can not do it and I wanted to see if you can help me.
I leave the code so they understand the idea better.
As I said before when entering a year and a month and the table does not exist, the prc falls
Alter PROCEDURE [dbo].[PRC_Reajuste]
@cAnio Varchar(4)
,@cMes Varchar(2)
,@Rut Varchar(12)
AS
BEGIN
Declare @Fecha Varchar(10) Set @Fecha = Ltrim((@cAnio)) + Ltrim((@cMes))
IF OBJECT_ID('dbo.Tmp_1') IS NOT NULL
BEGIN
DROP TABLE dbo.Tmp_1
IF (@@error <> 0)
BEGIN
ROLLBACK TRAN
RETURN
END END
Declare @Consulta varchar(800) Set @Consulta = 'Select * into Tmp_1 From [192.168.37.27].coop.dbo.GACPRO_ReajusteCapital_' + @Fecha + ' Where cRut= ' + '''' + @Rut + ''''
End