SQL
alter table LianTong_ContractApproveHistoryModels add CreateUserName nvarchar(max) NULL; EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'创建人名称' , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'TABLE',@level1name=N'LianTong_ContractApproveHistoryModels', @level2type=N'COLUMN',@level2name=N'CreateUserName' --修改字段 ALTER TABLE CisEmployee ALTER COLUMN EmployeeDescription nvarchar(max) NULL; sp_rename 'KYX_Employee.PK_KYX_Department_DepartmentName','PK_KYX_Department_DepartmentName1','column' /*ALTER TABLE 删除字段 */ ALTER TABLE TABLE_USER DROP COLUMN TEMP_COL; --按照字符串长度获取数据 SELECT [CompanyName] ,LEN(CompanyName) as lengths FROM [dbo].[OOF_Company] where LEN(CompanyName) <= 4 --按照字符串长度更新数据 update [dbo].[OOF_Company] set EnumCompanyStatus = '0' where LEN(CompanyName) <= 4 --查询当天数据 SELECT * FROM 表 WHERE datediff(day,列名,getdate())=0 --获取关联表的数据更新 UPDATE QST_OrderDetail SET ActualPrice = (SELECT CurrentPrice FROM QST_Product WHERE ID = QST_OrderDetail.PK_QST_Product_ProductName)
--一次性修改关联表的多个字段
update [dbo].[FarmC_Equipment]
set Province=b.Province,[City]=b.[City],[Village]=b.Village,Prefecture = b.Prefecture, Hamlet = b.Hamlet
from [FarmC_Equipment] a join FarmC_Cooperative b on a.PK_FarmC_Cooperative_CooperativeName=b.Id
--修改自增的初始值 DBCC CHECKIDENT ('TXCC_Assit', RESEED, 100000) --Values\s(\(N.\w*',) SELECT [Id] ,[Operator] ,[Message] ,[Result] ,[Type] ,[Module] ,[CreateTime] FROM [dbo].[SysLog] WHERE CreateTime >= Convert(datetime,'2018-05-02 17:20:00', 101) and CreateTime <= Convert(datetime,'2018-05-02 17:30:00', 101) update [GMGood] set [uploaddate] = REPLACE(SUBSTRING([uploaddate],6, 10),'.','-') where a=a --字符串值的长度 datalength(uploaddate) = 20 --字符串位置 Select CharIndex('a', '1235ab') --5 SELECT * FROM (select row_number()over(order by uploaddate desc)rownumber,* from GMGood) GMGood1 WHERE statusupload = '1' and rownumber between 31 and 60 SQL Server 字段设置默认值 1. 如果表字段已经建好 ALTER TABLE 表名 ADD CONSTRAINT DF_TABLEName_FieldName DEFAULT 1 FOR 字段名 2. 如果表字段没有建 alter table 表名 add 字段名 int default(1)
--修改表增加多字段唯一性校验,前提是这些个字段都必须是固定长度的,不能是nvarchar(max)了。
ALTER TABLE OA_Meeting ADD CONSTRAINT MeetingRoomTimeUni unique(PK_OA_MeetingRoom_RoomName,MeetingStart,MeetingEnd, EnumMeetingStatus,CancelSerialNo)