SQL SERVER 语句技巧

来源:互联网 发布:淘宝网店店铺介绍范文 编辑:程序博客网 时间:2024/06/11 09:11

-----查询 触发器
select name from sysobjects where name=触发器名称 and xtype='TR'


----创建触发器
create trigger tr_tb_dailinotice_status
on catabill
for insert 
as
update tb_dailinotice_status set num=0
from tb_dailinotice_status,inserted
where
rtrim(ltrim(tb_dailinotice_status.username))=rtrim(ltrim(inserted.username))


---查询用户表

select name from sysobjects where xtype='U';


--批量创建表
declare @tempTag VARCHAR(100)
declare @count int
--多列定义 declare @tempTag1 VARCHAR(100)   
--多列.....   
declare tagCursor cursor for (select name from sysobjects where xtype='U')--读取行数据到游标   
open tagCursor--打开游标   
    fetch next from tagCursor INTO @tempTag--多列定义加,@temp   
    WHILE(@@FETCH_STATUS = 0) BEGIN--0:FETCH 语句成功。 -1:FETCH 语句失败或此行不在结果集中。 -2:被提取的行不存在。   
        print @tempTag   
        fetch next from tagCursor INTO @tempTag--多列定义加,@temp   
        select @count=count(*)  from sysobjects where xtype='U' and name=@tempTag+'_c'
IF(@count=0)exec('select *  into '+ @tempTag+'_c from '+@tempTag)
    END  
close tagCursor--关闭游标   
DEALLOCATE tagCursor--销毁游标 


----查询某用户的表
select so.name from sysobjects so,sysusers su where so.uid=su.uid and su.name='sg' and so.xtype='U';


-----批量删除表
declare @tempTag VARCHAR(100)
--多列定义 declare @tempTag1 VARCHAR(100)   
--多列.....   
declare tagCursor cursor for (select so.name from sysobjects so,sysusers su where so.uid=su.uid and su.name='sg' and so.xtype='U')--读取行数据到游标   
open tagCursor--打开游标   
    fetch next from tagCursor INTO @tempTag--多列定义加,@temp   
    WHILE(@@FETCH_STATUS = 0) BEGIN--0:FETCH 语句成功。 -1:FETCH 语句失败或此行不在结果集中。 -2:被提取的行不存在。   
        print @tempTag   
        fetch next from tagCursor INTO @tempTag--多列定义加,@temp   
exec('drop table  '+@tempTag)
    END  
close tagCursor--关闭游标   
DEALLOCATE tagCursor--销毁游标 
原创粉丝点击