Procedimiento en T-SQL para generar consulta de insert o update e incluirla en Visual Basic 6 Fecha: 02/Oct/2004 (01/10/2004)
|
El procedimiento de m�s abajo permite generar una consulta insert o update para ser incluida en el c�digo de un proyecto Visual Basic 6 dando solo como par�metros la tabla de la base de datos MS-Sql Server y el tipo de consulta que se quiere generar.
Por ejemplo: Desde el Analizador de Consultas de MS-Sql Server escribe:
exec pa_gensql 'nombretabla',0 para generar una consulta insert
exec pa_gensql 'nombretabla',1 para generar una consulta update
A continuaci�n sigue c�digo en T-SQL:
--(c) 2004 Roberto Larronde Rybertt -- pa_gensql 'nombretabla',0 para cl�usula insert -- pa_gensql 'nombretabla',1 para cl�usula update CREATE proc pa_gensql @tblname varchar(50), @tipo int as if @tblname is null begin raiserror(15250,-1,-1) return(1) end -- validando @tblname declare @id int,@dbname sysname,@type char(2) if @tblname is not null begin select @dbname = parsename(@tblname, 3) if @dbname is not null and @dbname <> db_name() begin raiserror(15250,-1,-1) return (1) end if @dbname is null select @dbname = db_name() /* ** Intentando encontrar el objeto. */ select @id = null select @id = id, @type = xtype from sysobjects where id = object_id(@tblname) /* ** �El objeto existe? */ if @id is null begin raiserror(15009,-1,-1,@tblname,@dbname) return (1) end end declare @sqltext varchar(8000),@cadena varchar(8000) select @sqltext=' declare tblstru_crsr cursor for select a.name,a.xusertype from syscolumns a,sysobjects b where b.name like '''+@tblname+''' and b.id=a.id order by colid' exec (@sqltext) declare @name varchar(50),@xtipo smallint,@ultimo char(1) open tblstru_crsr fetch tblstru_crsr into @name,@xtipo set nocount on if(@tipo=0) --Insert set @cadena='sqlInsert="insert into '+ ltrim(rtrim(lower(@tblname))) + ' (' else --Update set @cadena='sqlUpdate="update '+ ltrim(rtrim(lower(@tblname))) + ' set ' while @@fetch_status= 0 begin if(@tipo=0) --Insert begin set @cadena=@cadena+ltrim(rtrim(lower(@name)))+',' end else begin if @xtipo=56 --Int begin set @cadena=@cadena+ltrim(rtrim(lower(@name)))+'=" & '+ltrim(rtrim(lower(@name)))+'-Int & ",' set @ultimo='n' end if @xtipo=61 --DateTime begin set @cadena=@cadena+ltrim(rtrim(lower(@name)))+'=''" & '+ltrim(rtrim(lower(@name)))+'-Fecha & "'',' set @ultimo='c' end if @xtipo=104 --Bit begin set @cadena=@cadena+ltrim(rtrim(lower(@name)))+'=" & '+ltrim(rtrim(lower(@name)))+'-Bit & ",' set @ultimo='n' end if @xtipo=106 --Decimal begin set @cadena=@cadena+ltrim(rtrim(lower(@name)))+'=" & '+ltrim(rtrim(lower(@name)))+'-Decimal & ",' set @ultimo='n' end if @xtipo=108 --Numeric begin set @cadena=@cadena+ltrim(rtrim(lower(@name)))+'=" & '+ltrim(rtrim(lower(@name)))+'-Numeric & ",' set @ultimo='n' end if @xtipo=167 --Varchar begin set @cadena=@cadena+ltrim(rtrim(lower(@name)))+'=''" & '+ltrim(rtrim(lower(@name)))+'-Varchar & "'',' set @ultimo='c' end if @xtipo=175 --Char begin set @cadena=@cadena+ltrim(rtrim(lower(@name)))+'=''" & '+ltrim(rtrim(lower(@name)))+'-Char & "'',' set @ultimo='c' end end fetch tblstru_crsr into @name,@xtipo end close tblstru_crsr deallocate tblstru_crsr if(@tipo=0) --Insert begin set @cadena=substring(@cadena,1,len(@cadena)-1)+')"'+char(10)+'sqlInsert=sqlInsert & " values(' declare @sqltext2 varchar(8000) select @sqltext2=' declare tblstru_crsr2 cursor for select a.name,a.xusertype from syscolumns a,sysobjects b where b.name like '''+@tblname+''' and b.id=a.id order by colid' exec (@sqltext2) declare @nombre varchar(50),@xusertype smallint open tblstru_crsr2 fetch tblstru_crsr2 into @nombre,@xusertype while @@fetch_status= 0 begin if @xusertype=56 --Int set @cadena=@cadena+'" & '+ltrim(rtrim(lower(@nombre)))+'-Int & ",' if @xusertype=61 --DateTime set @cadena=@cadena+'''" & '+ltrim(rtrim(lower(@nombre)))+'-Fecha & "'',' if @xusertype=104 --Bit set @cadena=@cadena+'" & '+ltrim(rtrim(lower(@nombre)))+'-Bit & ",' if @xusertype=106 --Decimal set @cadena=@cadena+'" & '+ltrim(rtrim(lower(@nombre)))+'-Decimal & ",' if @xusertype=108 --Numeric set @cadena=@cadena+'" & '+ltrim(rtrim(lower(@nombre)))+'-Numeric & ",' if @xusertype=167 --Varchar set @cadena=@cadena+'''" & '+ltrim(rtrim(lower(@nombre)))+'-Varchar & "'',' if @xusertype=175 --Char set @cadena=@cadena+'''" & '+ltrim(rtrim(lower(@nombre)))+'-Char & "'',' fetch tblstru_crsr2 into @nombre,@xusertype end close tblstru_crsr2 deallocate tblstru_crsr2 set @cadena=substring(@cadena,1,len(@cadena)-1)+')"' end else --Update begin if @ultimo='n' set @cadena=substring(@cadena,1,len(@cadena)-4)+char(10)+'sqlUpdate=sqlUpdate & " where "' else set @cadena=substring(@cadena,1,len(@cadena)-1)+'"'+char(10)+'sqlUpdate=sqlUpdate & " where "' end print @cadena return
Fichero con el c�digo de arriba: pa_gensql.zip - Tama�o 1.30 KB