Código SQL:
Ver originalDECLARE @query Nvarchar(MAX)
SET @query=''
IF EXISTS (SELECT * FROM sysobjects WHERE name = 'TB_PRUEBA')
BEGIN
SET @query='SELECT * FROM TB_PRUEBA'
END
IF EXISTS (SELECT * FROM sysobjects WHERE name = 'TB_PRUEBA2')
BEGIN
IF @query<>''
SET @query=@query + 'Union All'
SET @query=@query + 'SELECT * FROM TB_PRUEBA2'
END
IF EXISTS (SELECT * FROM sysobjects WHERE name = 'TB_PRUEBA3')
BEGIN
IF @query<>''
SET @query=@query + 'Union All'
SET @query=@query + 'SELECT * FROM TB_PRUEBA3'
END
sp_executesql @query