ALTER PROC [dbo].[UpdateTableData]@TableName VARCHAR(255),@SearchField VARCHAR(255),@SearchValue VARCHAR(255),@WhereField VARCHAR(255),@WhereValue VARCHAR(255)AS BEGINDECLARE @sql VARCHAR(255)DECLARE @xtype INT=0SET @sql ='UPDATE '+@TableName; --获取SqlServer中表结构 SELECT @xtype=syscolumns.xtypeFROM syscolumns, systypes WHERE syscolumns.xusertype = systypes.xusertype AND syscolumns.id = object_id(@TableName)and syscolumns.name=@SearchFieldIF(@xtype=NULL)RAISERROR ('查询字段不存在', 16, 1)SET @sql=@sql+' SET '+@SearchField+'=';IF(@xtype=48 OR @xtype=52 OR @xtype=56 OR @xtype=62OR @xtype=127)BEGINSET @sql=@sql+@SearchValue;ENDELSE IF(@xtype=35OR @xtype=99 OR @xtype=167 OR @xtype=175 OR @xtype=231 OR @xtype=239)BEGINSET @sql=@sql+''''+@SearchValue+'''';ENDELSERAISERROR ('数据类型错误', 16, 1)SET @xtype=0SELECT @xtype=syscolumns.xtypeFROM syscolumns, systypes WHERE syscolumns.xusertype = systypes.xusertype AND syscolumns.id = object_id(@TableName)and syscolumns.name=@WhereFieldIF(@xtype=0)BEGINRAISERROR ('修改字段不存在', 16, 1)ENDSET @sql=@sql+' WHERE '+@WhereField+'='IF(@xtype=48 OR @xtype=52 OR @xtype=56 OR @xtype=62OR @xtype=127)BEGINSET @sql=@sql+@WhereValue;ENDELSE IF(@xtype=35OR @xtype=99 OR @xtype=167 OR @xtype=175 OR @xtype=231 OR @xtype=239)BEGINSET @sql=@sql+''''+@WhereValue+'''';ENDELSERAISERROR ('数据类型错误', 16, 1)EXEC (@sql)END
exec UpdateTableData '表名','修改字段','修改值','条件字段','条件值'