sql server 系统表的作用

1:获取当前数据库中的所有用户表

select Name from sysobjects where xtype='u' and status>=0

  2:获取某一个表的所有字段

select name from syscolumns where id=object_id('表名')

  3:查看与某一个表相关的视图、存储过程、函数

select a.* from sysobjects a, syscomments b where a.id = b.id and b.text like '%表名%'

  4:查看当前数据库中所有存储过程

select name as 存储过程名称 from sysobjects where xtype='P'

  5:查询用户创建的所有数据库

select * from master..sysdatabases D where sid not in(select sid from master..syslogins where name='sa')

  或者

select dbid, name AS DB_NAME from master..sysdatabases where sid <> 0x01

  6:查询某一个表的字段和数据类型

select column_name,data_type from information_schema.columns where table_name = '表名'


比如说有A表的一个主键,作为B、C、D表的外键,可能你在某些时候修改A表中的某个记录,也许这个记录被BCD表已经用到,要修改很麻烦,昨天写了几个sql语句可以方便的实现这个要求,不过下班走的急,没来得及这里整理,周一上班再看贴上,初步计划是还有一个主要的应用,是在做子母表删除的时候,应该可以写一个函数,传入主键ID,然后删除所有子表的该记录,下周一实现。
实现上边所说功能的俩存储过程

1 None.gif -- 更新某主表下边对应的外键值 2 None.gif 3 None.gif CREATE PROCEDURE Proc_UpdateFroeKeyVal 4 None.gif @TblName varchar ( 50 ), 5 None.gif @FroeKeyName varchar ( 50 ), 6 None.gif @Value varchar ( 50 ) 7 None.gif AS 8 None.gif 9 None.gif BEGIN 10 None.gif declare @TblID varchar ( 50 ), 11 None.gif @ColuID varchar ( 50 ) 12 None.gif select @TblID = id from sysobjects where name = @TblName 13 None.gif select @ColuID = colid from syscolumns where id = @TblID and name = ' UserID ' 14 None.gif 15 None.gif select A. * ,B.Name as F_TblName,C.Name as F_ColName into #TmpTbl 16 None.gif from 17 None.gif sysforeignkeys as A,sysobjects as B, syscolumns as C 18 None.gif where 19 None.gif A.rkeyid = @TblID 20 None.gif And 21 None.gif A.rkey = @ColuID 22 None.gif And 23 None.gif B.id = fkeyid 24 None.gif And 25 None.gif C.id = fkeyid 26 None.gif And 27 None.gif C.colid = fkey 28 None.gif 29 None.gif declare @tabname varchar ( 40 ), @colName varchar ( 20 ), @sqlStr varchar ( 1024 ) 30 None.gif 31 None.gif DECLARE TabName_Cursor CURSOR FOR 32 None.gif SELECT F_TblName, F_ColName FROM #TmpTbl 33 None.gif OPEN TabName_Cursor 34 None.gif FETCH TabName_Cursor into @tabname , @colName 35 None.gif WHILE @@FETCH_STATUS = 0 36 None.gif BEGIN 37 None.gif select @sqlStr = ' Update ' + @tabname + ' Set ' + @colName + ' = ' + " ' "+@Value +" ' " 38 None.gif -- print @sqlstr 39 None.gif exec ( @sqlstr ) 40 None.gif FETCH TabName_Cursor into @tabname , @colName 41 None.gif END 42 None.gif CLOSE TabName_Cursor 43 None.gif DEALLOCATE TabName_Cursor 44 None.gif END 45 None.gif GO 46 None.gif

继续

1 None.gif -- 删除某主表对应子表记录 2 None.gif 3 None.gif CREATE PROCEDURE Proc_DelFroeKeyVal 4 None.gif @TblName varchar ( 50 ), 5 None.gif @FroeKeyName varchar ( 50 ), 6 None.gif @Value varchar ( 50 ) 7 None.gif AS 8 None.gif 9 None.gif BEGIN 10 None.gif declare @TblID varchar ( 50 ), 11 None.gif @ColuID varchar ( 50 ) 12 None.gif select @TblID = id from sysobjects where name = @TblName 13 None.gif -- select @TblID =OBJECT_ID ( 'T_User' ) 14 None.gif select @ColuID = colid from syscolumns where id = @TblID and name = ' UserID ' 15 None.gif 16 None.gif select A. * ,B.Name as F_TblName,C.Name as F_ColName into #TmpTbl 17 None.gif from 18 None.gif sysforeignkeys as A,sysobjects as B, syscolumns as C 19 None.gif where 20 None.gif A.rkeyid = @TblID 21 None.gif And 22 None.gif A.rkey = @ColuID 23 None.gif And 24 None.gif B.id = fkeyid 25 None.gif And 26 None.gif C.id = fkeyid 27 None.gif And 28 None.gif C.colid = fkey 29 None.gif 30 None.gif declare @tabname varchar ( 40 ), @colName varchar ( 20 ), @sqlStr varchar ( 1024 ) 31 None.gif 32 None.gif DECLARE TabName_Cursor CURSOR FOR 33 None.gif SELECT F_TblName, F_ColName FROM #TmpTbl 34 None.gif OPEN TabName_Cursor 35 None.gif FETCH TabName_Cursor into @tabname , @colName 36 None.gif WHILE @@FETCH_STATUS = 0 37 None.gif BEGIN 38 None.gif select @sqlStr = ' delete ' + @tabname + ' where ' + @colName + ' = ' + " ' "+@Value +" ' " 39 None.gif -- print @sqlstr 40 None.gif exec ( @sqlstr ) 41 None.gif FETCH TabName_Cursor into @tabname , @colName 42 None.gif END 43 None.gif CLOSE TabName_Cursor 44 None.gif DEALLOCATE TabName_Cursor 45 None.gif END
[@more@]

来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/7192349/viewspace-1001499/,如需转载,请注明出处,否则将追究法律责任。

转载于:http://blog.itpub.net/7192349/viewspace-1001499/

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值