欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

查询表数据通用存储过程

程序员文章站 2022-04-30 19:57:50
SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO ALTER PROC [dbo].[SearchTableData] @TableName VARCHAR(255), @Fields VARCHAR(255), @WhereField VARCHAR... ......
set ansi_nulls on
go
set quoted_identifier on
go

alter proc [dbo].[searchtabledata]
@tablename varchar(255),
@fields varchar(255),
@wherefield varchar(255),
@wherevalue varchar(255)
as 
begin
declare @sql varchar(255)
declare @xtype int=0
set @sql ='select '+@fields+' from '+@tablename;
select @xtype=syscolumns.xtype
from syscolumns, systypes 
where syscolumns.xusertype = systypes.xusertype 
and syscolumns.id = object_id(@tablename)and  syscolumns.name=@wherefield
if(@xtype=0)
begin
raiserror ('修改字段不存在', 16, 1)
end
set @sql=@sql+' where '+@wherefield+'='
if(@xtype=48 or @xtype=52 or @xtype=56 or @xtype=62or @xtype=127)
begin
set @sql=@sql+@wherevalue;
end
else if(@xtype=35or @xtype=99 or @xtype=167 or @xtype=175 or @xtype=231 or @xtype=239)
begin
set @sql=@sql+''''+@wherevalue+'''';
end
else
raiserror ('数据类型错误', 16, 1)
exec (@sql)
end 
exec searchtabledata '表名','*','countid','1'