阅读更多
-----------建表-----------------
IF OBJECT_ID ('SysUser', 'U') IS NULL
BEGIN
CREATE TABLE [dbo].[SysUser]
(
[Id] BIGINT IDENTITY (1, 1) NOT NULL, --主键用Id,整型,自增
[Name] NVARCHAR (32) NOT NULL,
[PhoneNumber] NVARCHAR (32) NOT NULL,
[Password] NVARCHAR (128) NOT NULL,
[UserName] NVARCHAR (256) NOT NULL,
[CreationTime] DATETIME NOT NULL,
[CreatorUserId] BIGINT NULL,
[LastLoginTime] DATETIME NULL,
[LastModificationTime] DATETIME NULL,
[LastModifierUserId] BIGINT NULL,
[IsDeleted] BIT NOT NULL,
[DeleterUserId] BIGINT NULL,
[DeletionTime] DATETIME NULL,
[IsActive] BIT NOT NULL
);
--添加聚集索引,根据业务仔细设定
CREATE CLUSTERED INDEX IX_SysUser_CreationTime
ON SysUser (CreationTime);
--添加非聚集索引
CREATE NONCLUSTERED INDEX IX_SysUser_CreatorUserId
ON SysUser (CreatorUserId);
--添加默认值
ALTER TABLE SysUser ADD CONSTRAINT DF_SysUser_CreationTime DEFAULT GetDate() FOR CreationTime
--添加主键
ALTER TABLE SysUser ADD CONSTRAINT PK_SysUser PRIMARY KEY (Id);
END
GO
--主键用Id,整型,自增; 视业务用Int或者BigInt
--业务允许时,字段尽量设置为not null,null值用空字符串代替
-----------修改表------------------
--新增字段
If Col_Length('Bas_Student','id') Is Null
Begin
Alter Table Bas_Student Add id Int IDENTITY(1,1) not Null
End
Go
--删除旧主键
IF EXISTS
(SELECT *
FROM SYSINDEXES
WHERE NAME = 'PK_WMS_INV_MATERIALSTORER')
ALTER TABLE Wms_Inv_MaterialStorer DROP CONSTRAINT PK_WMS_INV_MATERIALSTORER
--添加新主键
ALTER TABLE Wms_Inv_MaterialStorer
ADD CONSTRAINT PK_WMS_INV_MATERIALSTORER
PRIMARY KEY (CompanyID, StockID, StorerID, MaterialID, SizeID );
--添加唯一索引,来源单号
CREATE UNIQUE INDEX UI_EWS_Out_OrderMaster
ON EWS_Out_OrderMaster(CompanyID, StockID, ExtTradeId)
-----------建视图-----------------
--删除对象
IF OBJECT_ID('vwStudent', 'V') IS NOT NULL
DROP VIEW [vwStudent]
GO
--仓储作业往来单位
--Select * From vwStudent
CREATE VIEW [vwStudent]
AS
SELECT CompanyID, StockID ConsigneeID, StockName AS ConsigneeName,
CONVERT(INT, 1) AS ConsigneeType, Address, TelPhone
FROM Bas_Stock
GO
-----------对象是否存在判断-------------
--存储过程
IF OBJECT_ID('spWMS_XXXXX','P') IS NOT NULL
DROP PROCEDURE [dbo].[spWMS_XXXXX]
GO
--视图
IF OBJECT_ID('vwWMS_XXXXX', 'V') IS NOT NULL
DROP VIEW [dbo].[vwWMS_XXXXX]
GO
--临时表
IF OBJECT_ID('tempdb..#XXXXX') IS NOT NULL
DROP TABLE #XXXXX
------------调试模版-----------------
BEGIN TRY
BEGIN TRAN
ROLLBACK TRAN
END TRY
BEGIN CATCH
SELECT ERROR_MESSAGE();
ROLLBACK TRAN
END CATCH