求一条查询语句,很简单的。60分
如题,就是求一个可以通用的关于 select 的查询的存储过程。就是说 不论对什么表,或者字段 行 查询都可以使用一个存储过程。
------解决方案--------------------create procedure [dbo].[Pro_Select](
@cells varchar(1000)= '* ',
@tables varchar(1000),
@top int = -1,
@condition varchar(2000)= ' ',
@orderby varchar(200)=NULL
)
as
set nocount on
declare @rowcount int;
declare @SQL nvarchar(4000);
if(@top = -1)
set @SQL = N 'select ' + @cells + ' from ' + @tables + ' where 1=1 ' + @condition;
else
set @SQL = N 'select top ' + cast(@top as nvarchar(10)) + ' ' + @cells + ' from ' + @tables + ' where 1=1 ' + @condition;
if(@orderby is not NULL)
set @SQL = @SQL + N ' order by ' + @orderby;
exec (@SQL)
set nocount off
------解决方案--------------------CREATE PROCEDURE dbo.Query_Tab
@table nvarchar(1000),
@field nvarchar(1000)
AS
begin
declare @sql nvarchar(4000)
if @field <> ' '
begin
set @sql= 'select ' +@field + ' from ' +@table
end
else
begin
set @sql= 'select * from ' +@table
end
exec (@sql)
end
GO