sql server 删除所有表及所有存储过程、所有视图和递归查询、数字类型转为字符串

1、删除所有表

复制代码

复制代码
DECLARE c1 cursor for
select 'alter table ['+ object_name(parent_obj) + '] drop constraint ['+name+']; '
from sysobjects
where xtype = 'F'
open c1
declare @c1 varchar(8000)
fetch next from c1 into @c1
while(@@fetch_status=0)
begin
exec(@c1)
fetch next from c1 into @c1
end
close c1
deallocate c1
 
declare @sql varchar(8000)
while (select count(*) from sysobjects where type='U')>0
begin
SELECT @sql='drop table ' + name
FROM sysobjects
WHERE (type = 'U')
ORDER BY 'drop table ' + name
exec(@sql)
end
复制代码
复制代码

 

2、删除所有存储过程

declare @tname varchar(8000)
set @tname=''
select @tname=@tname + Name + ',' from sysobjects where xtype='P'
select @tname='drop proc ' + left(@tname,len(@tname)-1)
exec(@tname)
go

 

3、删除所有视图

declare @tname varchar(8000)
set @tname=''
select @tname=@tname + Name + ',' from sysobjects where xtype='V'
select @tname='drop view ' + left(@tname,len(@tname)-1)
exec(@tname)
go

 

4、递归查询 使用关键字with as

复制代码
 with temp ( [Id], [parentid])
    as
    (
        select Id, ParentId
        from SysLocation
        where ParentId = @ParentId
        union all
        select a.Id, a.ParentId
        from SysLocation a
        inner join temp on a.ParentId = temp.[Id]        
    )
    
    select s.Id, s.ParentId from SysLocation s where Id=@ParentId
    union all
    select * from temp
复制代码

 

5、数字类型转为字符串

select convert(varchar(11),convert(decimal(11,0),mo)) as m  from test

 

6、创建外键

alter table ResolvedOrderNew 
add constraint FK_ResolvedOrderNew_QuotaOrderNew
foreign key (QuoteOrderNewId) references QuoteOrderNew(Id);

 

posted @   大空白纸  阅读(411)  评论(0编辑  收藏  举报
编辑推荐:
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
阅读排行:
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· 【自荐】一款简洁、开源的在线白板工具 Drawnix
历史上的今天:
2015-04-19 asp.net mvc DropDownList
点击右上角即可分享
微信分享提示