SQL SERVER 语句技巧

-----查询 触发器
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--销毁游标
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值