还原数据库后附加原有用户
exec sp_change_users_login 'UPDATE_ONE','用户名','登录名'
将用户名映射为指定的登录名
例如:
sp_change_users_login 'update_one', 'admin30', 'admin30'
判断约束是否存在
IF EXISTS (SELECT * FROM sys.check_constraints WHERE object_id = OBJECT_ID(N'约束名) AND parent_object_id = OBJECT_ID(N'表名'))
ALTER TABLE 表名 ADD CONSTRAINT 约束名 DEFAULT ((0)) FOR [State]
GO
判断索引是否存在,不存在则创建
if not exists(select * from sysindexes where id=object_id('iSosDictionary') and name='IX_dictvalue')
CREATE UNIQUE NONCLUSTERED INDEX [IX_dictvalue] ON [iSosDictionary](
[DictValue] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, SORT_IN_TEMPDB = OFF, IGNORE_DUP_KEY = OFF, DROP_EXISTING = OFF, ONLINE = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
GO