工作中遇到乙個需求,要查詢表結構以及索引、主鍵,本來搞定了,可突然又不好使了,因為我第一次寫的指令碼只能相容sql server 2005,用在sql server 2000裡面就報錯了,鬱悶,我再改,這次是通用的了,看它還敢給我報錯!
--查詢索引和主鍵
select
indexid = idx.indid,
indexname = idx.name,
columnname = col.name,
sort = case indexkey_property(idx.id, idx.indid, idxk.keyno, 'isdescending')
when 1 then 'desc'
when 0 then 'asc'
else '' end,
primarykey = case objpk.xtype
when 'pk' then '√'
else ''end
from
sysindexes idx
inner join sysobjects c
on idx.id=c.id
and c.xtype='u'
left join sysobjects objpk
on objpk.[name]=idx.[name]
left join sysindexkeys idxk
on idx.id = idxk.id
and idx.indid = idxk.indid
left join syscolumns col
on col.colid = idxk.colid
and col.id = idxk.id
where
c.name='tablename' --這裡改成表名
and col.name is not null
--查詢表結構
select
id = col.colorder,
[name] = col.name,
[systemtype] = types.name,
[length] = cast(case when types.name in (n'nchar', n'nvarchar') and col.length <> -1 then col.length/2
else col.length end
as int)
from
syscolumns col
left join systypes types
on col.xtype = types.xusertype
inner join sysobjects obj
on col.id = obj.id
and obj.xtype = 'u'
and obj.name <> 'dtproperties'
where
obj.name='tablename'--這裡改成表名
order by
col.colorder
SQLServer分頁查詢通用儲存過程
自開始做專案以來,一直在用。這段儲存過程的的原創者 sorry,忘記名字了 寫得這段sql 很不錯,我在這個基礎上,按照我的習慣以及思維方式,調整了 只做分頁查詢用。create procedure prcpageresult 獲得某一頁的資料 currpage int 1,當前頁頁碼 即top c...
使用SQL語句 匯出SQLServer表結構
select 表名 case when a.colorder 1 then d.name else end,表說明 case when a.colorder 1 then isnull f.value,else end,字段序號 a.colorder,欄位名 a.name,字段說明 isnull g...
詳解sqlserver查詢表索引
select 索引名稱 a.name 表名 c.name 索引欄位名 d.name 索引字段位置 d.colid from sysindexes a join sysindexkeys b on a.id b.id and a.indid b.indid join sysobjects c on b...