SQL Server 2000 工作中常用查询语句
问题背景
最近维护一个老项目,帮助客户迁移同步一些数据,对方数据库是SQL Server 2000,我这里仅仅记录我在工作中用到的语句,可能不具代表性,仅仅自己备忘吧。
文章持续更新,直到不再维护。
常用语句
1、查询某些表的表结构
SELECT
表名 = d.name,--case when a.colorder=1 then d.name else '' end,
字段序号 = a.colorder,
字段名 = a.name,
类型 = b.name,
占用字节数 = a.length,
长度 = COLUMNPROPERTY(a.id,a.name,'PRECISION'),
小数位数 = isnull(COLUMNPROPERTY(a.id,a.name,'Scale'),0),
允许空 = case when a.isnullable=1 then '√'else '' end,
默认值 = isnull(e.text,'')
FROM
syscolumns a
left join
systypes b
on
a.xusertype=b.xusertype
inner join
sysobjects d
on
a.id=d.id and d.xtype='U' and d.name<>'dtproperties'
left join
syscomments e
on
a.cdefault=e.id
where
d.name='WCMDOCUMENT' or d.name='WCMAPPENDIX' or d.name='WCMCHNLDOC' --如果只查询指定表,加上此条件
order by
a.id,a.colorder
2、分页查询方法
/// <summary>
/// 已发布的公文正式文件库
/// </summary>
/// <param name="sql"></param>
/// <param name="order"></param>
/// <param name="field"></param>
/// <param name="pageIndex"></param>
/// <param name="pageSize"></param>
/// <returns></returns>
public DataTable ExecutePagingList(string sql, string order, string field, int pageIndex, int pageSize)
{
string mSql = string.Format(@"SELECT TOP {0} * FROM ({1}) TT1 WHERE {2} NOT IN
( SELECT TOP {3} {4} FROM ({5}) TT2 {6} ) {7} ", pageSize, sql, field, pageSize * (pageIndex - 1), field, sql, order, order);
SqlParameter[] parameters = {
new SqlParameter("@PAGESIZE", SqlDbType.Int) { Value = pageSize},
new SqlParameter("@TOTAL", SqlDbType.Int) { Value = pageSize*(pageIndex - 1)},
new SqlParameter("@FIELD", SqlDbType.NVarChar) { Value = field},
new SqlParameter("@SQL", SqlDbType.NVarChar) { Value = sql},
new SqlParameter("@ORDER", SqlDbType.NVarChar) { Value = order},
};
DataSet ds = Utils.DocDbHelper.Query(mSql, parameters);
if (ds != null && ds.Tables.Count > 0)
{
return ds.Tables[0];
}
else
{
return null;
}
}