心灵相待吧 关注:3贴子:118
  • 0回复贴,共1

建表视图存储过程增加字段等判断 总结

取消只看楼主收藏回复

--各种SQL语句判断
--查看系统中的所有仓库
select * from treetype where TypeSysid='A' -- and sysId like 'AB002%'
--向表中插入数据
if(not exists(select * from 表名 where 字段名='需插入的数据'))
insert into 表名(字段名)
select '需插入的数据'
go
--向表中增加字段
IF not EXISTS (
SELECT 1 FROM SYSOBJECTS T1
INNER JOIN SYSCOLUMNS T2 ON T1.ID=T2.ID
WHERE T1.NAME='表名' AND T2.NAME='增加的字段'
)
alter table 表名
Add 增加的字段 字段类型
go
--删除表外键
if exists (select 1
from sys.sysreferences r join sys.sysobjects o on (o.id = r.constid and o.type = 'F')
where r.fkeyid = object_id('表名') and o.name = '外键名称')
alter table 表名
drop constraint 外键名称
go
--增加表
if exists (select 1
from sysobjects
where id = object_id('表名')
and type = 'U')
drop table 表名
go
/*==============================================================*/
/* Table: 表名 */
/*==============================================================*/
create table 表名 (
Id int identity,
字段名 字段类型 null,
主键字段名 字段类型 not null,
constraint 主键名称 primary key (主键字段名)
)
go
--增加视图
if exists (select 1
from sysobjects
where id = object_id('视图名称')
and type = 'V')
drop view 视图名称
go
CREATE VIEW [dbo].[视图名称]
AS
SELECT A.表字段名称
FROM dbo.表名称 AS A
GO
--增加 存储过程
if exists (select 1
from sysobjects
where id = object_id('存储过程名称')
and type = 'P')
drop procedure 存储过程名称
go
--函数判断
if exists (select 1 from sysobjects where id=OBJECT_ID('函数名称')
and xtype in ('FN','IF','TF'))
drop function [dbo].[函数名称]
go


IP属地:广东1楼2016-08-10 11:03回复