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);
分类:
SQL Server技术
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 开发者必知的日志记录最佳实践
· 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