procedure to insert dynamic table SQL Server

Technology

procedure to insert dynamic table SQL Server

Today in programming we are going to create a procedure, in which we can insert records in tables without need to wear conditional queries only with a code line we can insert the rows we want in the database.
procedure to insert dynamic table SQL Server
In the procedure, we are going to send the table name by parameters, and therefore the following columns. of the tables in this example we will take the name database [mabajava]

"@PI_NombreTabla" will be the name of the table, it will have a varchar 50
"@PI_IdEstadoProceso" will be the name of a column in table varchar 50
"@PI_IdSecuencia" will be the sequential of the table
"@PI_Duracion" will be an integer
"@PI_Error" will be a varchar of 50. and finally we insert the following code in sql server:
USE [mbajava]
GO
/****** Object:  StoredProcedure CREADO BY https://mbajava.com [dbo].[SP_RS_ActualizarCampos]    Script Date: 20/05/2020 6:21:14 p. m. ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
alter procedure [dbo].[SP_RS_ActualizarCampos] 
@PI_NombreTabla varchar(50),
@PI_IdEstadoProceso varchar(50),
@PI_IdSecuencia int,
@PI_Duracion int,
@PI_Error varchar(50)





AS
BEGIN
Declare @SQL VARCHAR(500)

 set @SQL= 'UPDATE '+@PI_NombreTabla+' set EstadoDelproceso='''+@PI_IdEstadoProceso+''',
 Fecha_Procesado=GETDATE(),Duracion='+Convert(varchar,@PI_Duracion)+', marca='''+@PI_Error+''' where Id_Secuencial='+Convert(varchar,@PI_IdSecuencia);



EXEC (@SQL)
End


we use the "Convert (varchar" to convert from int to varchar
to execute we just send an exec

exec dbo.SP_RS_ActualizarCampos 'Table_Name', 'processed', 1,23, 'no error'


Post a Comment

0 Comments