新聞中心
這里有您想知道的互聯(lián)網(wǎng)營銷解決方案
如何查詢SQLServer數(shù)據(jù)庫沒有主鍵的表并增加主鍵
SQL Server數(shù)據(jù)庫中,如果一個表沒有主鍵,我們該如何查詢呢?本文我們主要就介紹了如何查詢數(shù)據(jù)庫中沒有主鍵的表名并為其增加主鍵的方法,希望能夠?qū)δ兴鶐椭?/p>

讓客戶滿意是我們工作的目標,不斷超越客戶的期望值來自于我們對這個行業(yè)的熱愛。我們立志把好的技術(shù)通過有效、簡單的方式提供給客戶,將通過不懈努力成為客戶在信息化領(lǐng)域值得信任、有價值的長期合作伙伴,公司提供的服務(wù)項目有:域名與空間、虛擬主機、營銷軟件、網(wǎng)站建設(shè)、薩嘎網(wǎng)站維護、網(wǎng)站推廣。
該功能的實現(xiàn)代碼如下:
- declare @tablename sysname
- declare @strsql nchar(500)
- declare tableNameCursor cursor for
- select b.name from sysobjects b where xtype='U' and b.name not in
- (select object_name(a.parent_obj) from sysobjects a where xtype='PK' )
- open tableNameCursor
- fetch next from tableNameCursor into @tablename
- while @@FETCH_STATUS = 0
- begin
- print @tablename
- set @strsql= 'alter table ' + @tablename + ' add primary key (id) '
- print @strsql
- exec (@strsql)
- fetch next from tableNameCursor into @tablename
- end
- close tableNameCursor
- deallocate tableNameCursor
以上就是SQL Server數(shù)據(jù)庫中查詢沒有主鍵的表的名稱并為其增加主鍵的實現(xiàn)代碼,希望本次的代碼示例能夠給您帶來一些收獲,謝謝!
本文標題:如何查詢SQLServer數(shù)據(jù)庫沒有主鍵的表并增加主鍵
標題路徑:http://fisionsoft.com.cn/article/dhiochs.html


咨詢
建站咨詢
