当前位置: 代码迷 >> Sql Server >> 这样的存储过程如何写呀
  详细解决方案

这样的存储过程如何写呀

热度:85   发布时间:2016-04-27 15:49:11.0
这样的存储过程怎么写呀
要创建一个用来查询的存储过程,数据库里有二十四张表,表名都是以月份命名的tabyyyymm(如tab200703),选择日期查询时可以是查询某一个月的,也可以跨月份的查询,比如查询2007年2月份至6月份的数据,这应该怎么办呢?

------解决方案--------------------
create table tab200701(id int)
insert into tab200701 select 1
create table tab200702(id int)
insert into tab200702 select 2
create table tab200703(id int)
insert into tab200703 select 3
create table tab200704(id int)
insert into tab200704 select 4

GO
create proc usp_test
@begin varchar(06),
@end varchar(06)= ' '
AS
if @end= ' '
set @[email protected]
declare @sql varchar(8000)
set @sql= ' '
select @[email protected]+ ' union all select * from [ '+name+ '] ' from sysobjects
where right(name,6) > = @begin
and right(name,6) <[email protected]
and name like 'tab% '
and len(name)=9
and xtype= 'u '
set @sql= stuff(@sql,1,11, ' ')
exec(@sql)
GO

exec usp_test '200702 ', '200704 '
/*
id
-----------
2
3
4
*/
exec usp_test '200702 '
/*
id
-----------
2
*/

drop table tab200701,tab200702,tab200703,tab200704
drop proc usp_test



------解决方案--------------------
使用两个单引号代替一个单引号
  相关解决方案