Archive for 十月, 2009

oracle获取表的记录数、翻页存储过程、创建表空间

--查询指定用户名下的表的记录数
select NUM_ROWS,TABLE_NAME from dba_all_tables where owner='用户名' order by NUM_ROWS desc
--删除重复的记录
delete from onetable where guid in(select guid from onetable group by guid Having count (guid)>1) and rowid not in(select min( rowid ) from onetable group by guid Having count (guid)>1)
CREATE OR REPLACE PROCEDURE GETRECORDFROMPAGE(
   tblName   in   varchar2,       -- 表名
   fldName   in  varchar2:='*',     --显示列
   PageSize  in   number := 20,           -- 页尺寸
   PageIndex  in   number := 1,            -- 页码
   strOrderSyntax  in varchar2, ---排序的具体语法
   strWhere   in   varchar2 := '',  -- 查询条件 (注意: 不要加 where)
   Onlyzd     in    varchar2 :='*',--唯一字段
   Total in out number,
  cur_OUT OUT sys_refcursor
)
as
   tempSql varchar2(8000):='';
   countSql varchar2(2000):='';
   strSql varchar2(8000):='';
   condition varchar2(1000):=strWhere;
   fieldsName varchar2(1000):='';

begin
   if condition='' or condition is null then
     condition := '1=1';
   end if;

   fieldsName := trim(fldName);
   if fieldsName != '*' then -- 不为 '*'
     tempSql := 'select '|| fldName ||' from '||tblName||' where '||condition||' '||strOrderSyntax;
   else
     tempSql := 'select  a.* from '||tblName||' a where '||condition||' '||strOrderSyntax;
   end if;

   --记录集

   strSql := 'select t.*,rownum as num from ('||tempSql||') t ';
   strSql := 'select * from ('|| strSql ||') tt where tt.num<='||CAST((PageSize*PageIndex) AS char)||' and tt.num>'||CAST((PageSize*(PageIndex-1)) AS char);

   --为了排序,重新组织语句
   --strSql := 'select * from ('|| strSql||') tt '||strOrderSyntax;
   --记录总数

   countSql := 'select DISTINCT '||Onlyzd||' from '||tblName||' where '||condition;
   countSql := 'select count(*) as Total from ('|| countSql ||') t';

   execute immediate countSql into Total;
  OPEN cur_OUT FOR strSql;
END GETRECORDFROMPAGE;
--创建表空间
create tablespace HzjwUiasDB
logging datafile 'd:\DBRunfile\HzjwUiasDB.dbf'
size 1024M autoextend on next 512M maxsize 20480M
extent management local;

Tags: , , ,

通过组织机构代码找到省或直辖市的名称

public static string GetProvinceByadministrativeArea(string code)
{
	code = code.Substring(0, 2);
	switch (code)
	{
		case "11": return "北京";
		case "12": return "天津";
		case "13": return "河北";
		case "14":
			return "山西";
		case "15":
			return "内蒙";
		case "21":
			return "辽宁";
		case "22":
			return "吉林";
		case "23":
			return "黑龙江";
		case "31": return "上海";
		case "32":
			return "江苏";
		case "33":
			return "浙江";
		case "34":
			return "安徽";
		case "35":
			return "福建";
		case "36":
			return "江西";
		case "37":
			return "山东";
		case "41":
			return "河南";
		case "42":
			return "湖北";
		case "43":
			return "湖南";
		case "44":
			return "广东";
		case "45":
			return "广西";
		case "46":
			return "海南";
		case "50": return "重庆";
		case "51":
			return "四川";
		case "52":
			return "贵州";
		case "53":
			return "云南";
		case "54":
			return "西藏";
		case "61":
			return "陕西";
		case "62":
			return "甘肃";
		case "63":
			return "青海";
		case "64":
			return "宁夏";
		case "65":
			return "新疆";
		case "71":
			return "台湾";
		case "81":
			return "香港";
		case "82":
			return "澳门";
	}
	return "";
}

Tags: ,

mssql中清空数据表、获取表的记录数、翻页存储过程、查询重复数据

–获取数据库中的表的记录数,并倒排序

create procedure alltablecount as
declare @name varchar(100)
declare tablecur cursor for select name from sysobjects where xtype= 'u '
create table #tablecount(tablename varchar(100),reccount int)
open tablecur
fetch next from tablecur into @name
while @@fetch_status!=-1
begin
exec ('insert into #tablecount select tablename='''+@name+''',reccount=count(1) from '+@name+' ')
fetch next from tablecur into @name
end
close tablecur
deallocate tablecur
select * from #tablecount order by reccount desc
go
exec alltablecount
drop procedure alltablecount

–清空数据表中的数据

DECLARE tables_cursor CURSOR
   FOR
   SELECT name FROM sysobjects WHERE type = 'U' --选择用户表名
OPEN tables_cursor --打开游标连接
DECLARE @tablename varchar(40)  -- 定义变量
FETCH NEXT FROM tables_cursor INTO @tablename   --结果集中一行一行读取表名
WHILE (@@FETCH_STATUS <> -1)  --判断游标状态
BEGIN
print @tablename
   if(@tablename like 'y_%') EXEC ('TRUNCATE TABLE ' + @tablename)   --清空表中的数据
   FETCH NEXT FROM tables_cursor INTO @tablename  --下一行数据
END
close tables_cursor
DEALLOCATE tables_cursor  --关闭游标
go

–效率最高的翻页存储过程

ALTER  PROCEDURE [dbo].[GetRecordFromPage]
(
@tblName varchar(500),--表名
@PageSize int=20,--页尺寸
@PageIndex int=1,--页码
@IsCount bit=0,--返回记录总数,非0值则返回
@strOrderSyntax varchar(800),---排序的具体语法
@strWhere varchar(5000)='',--查询条件(注意:不要加where)
@fldName varchar(800),
@Onlyzd varchar(50)='',--唯一字段
@Total int output
)
AS
Begin
declare @strSQL nvarchar(2000)--主语句
declare @strTmp varchar(110)--临时变量
declare @strOrder varchar(400)--排序类型
if (CHARINDEX(',',@strOrderSyntax,1)>0)
begin
if @strWhere!=''
set @strSQL=N'select top'+str(@PageSize)+' '+@fldName+ ' from '
+@tblName+' where '+@Onlyzd+'not in'+'(select top'+str((@PageIndex-1)*@PageSize)+' '+@Onlyzd+' from '
+@tblName+' where '+@strWhere+' '+@strOrderSyntax+') and '+@strWhere+ ' ' +@strOrderSyntax
else
set @strSQL=N'select top '+str(@PageSize)+' '+@fldName+' from '
+@tblName+' where '+@Onlyzd+' not in '+'(select top'+str((@PageIndex-1)*@PageSize)+' '+@Onlyzd+' from '
+@tblName+' '+@strOrderSyntax+')'+@strOrderSyntax;
exec sp_executesql @strSQL;
end
else
begin
declare @ordername varchar(100)
if(charindex('desc',@strOrderSyntax,1)>0)
set @strTmp='<(select min'
else
set @strTmp='>(select max'
set @ordername=replace(@strOrderSyntax,'order by','');
set @ordername=replace(@ordername,' desc','');
set @ordername=replace(@ordername,' asc','');
set @ordername=replace(@ordername,' ','');
set @strOrder=@strOrderSyntax
if @PageIndex=1
begin
if @strWhere!=''
set @strSQL='select top '+str(@PageSize)+' '+@fldName+' from '+@tblName+'  where '+@strWhere+' '+@strOrder
else
set @strSQL='select top '+str(@PageSize)+' '+@fldName+' from  '+@tblName+' '+@strOrder
end
else
begin--以下代码赋予了@strSQL以真正执行的SQL代码
set @strSQL='select top '+str(@PageSize)+' '+@fldName+' from '+@tblName+'  where '+@ordername+' '+@strTmp+' ([tt]) from(select top '+str((@PageIndex-1)*@PageSize)+' '+@ordername+' as tt from '+@tblName+' '+@strOrder+') as tblTmp)'+@strOrder
if @strWhere!=''
set @strSQL='select top '+str(@PageSize)+' '+@fldName+' from '+@tblName+'  where '+@ordername+' '+@strTmp+'([tt]) from (select top '+str((@PageIndex-1)*@PageSize)+' '+@ordername+' as tt from  '+@tblName+' where '+@strWhere+''+@strOrder+') as tblTmp ) and '+@strWhere+' '+@strOrder
end
end
print @strSQL
exec(@strSQL)
--用于计算当前记录数(在分页用到)
if	@strWhere!=''
set @strSQL=N'select @Total=count('+@Onlyzd+') from '+@tblName+' Where ('+@strWhere+')'
else
set @strSQL=N'select @Total=count('+@Onlyzd+') from '+@tblName
exec sp_executesql @strSQL,N'@Total int output',@Total output;
if @Total is Null
set @Total=0;
end

–以下是上面存储过程的原型是:

select top 页大小 *
from table1
where id >
(select max (id) from
(select top ((页码-1)*页大小) id from table1 order by id) as T
)
order by id
--查询重复的记录
select * from onetable where guid in
 (select guid from onetable group by guid having(count(guid))>1)

--查询不重复的记录
select * from onetable where guid not in
 (select guid from onetable group by guid having(count(guid))>1)

Tags: , , ,

pagepeel by webpicasso.de