1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 /* * 創建時間:2011-10-09 * liud * 使用目錄: * ============================================== * 01==查看表描述信息 * 02==添加列(可多列)修改列刪除列修改列名 * 03==刪除表字段描述 * 04==添加表字段描述(方法1、2) * 05==修改表字段描述 * 06==修改列允許為空或不為空 * 07==創建表主鍵外鍵索引修改主鍵 * 08==表列增加、修改默認值 * 09==函數Split * 10==FOR XML AUTO * 11==FOR XML PATH 多種使用方式 * 12==將指定字符替換 stuff('abc',2,1,'B') 結果:aBc * 13==聯合多表Update操作 * 14==日期函數 * 15==行列轉換(兩種方式) * 16==刪除/創建 表建立的約束(如默認值) * 17==With ** AS()使用 * 18==Update 時增加 @ERROR<>0 OR @ROWCOUNT<>cnt(影響行數) 判斷 * 19==大數據表循環刪除腳本 * ============================================== */ --111111111111111==============================查看表描述信息 SELECT * FROM ::fn_listextendedproperty (NULL, 'user', 'dbo', 'table', 'CONTRACT', 'column', NULL) --222222222222222==============================添加列(可多列)修改列刪除列 ALTER TABLE 表 ADD 列 int,可多列 逗號分隔 ALTER TABLE tableName ALTER column columnName varchar(4000) ALTER TABLE tableName drop column columnName EXEC sp_rename 'tableName.column1' , 'column2' --(把表名為tableName的column1列名修改為column2) --33333333333333==============================刪除表字段描述 -- EXEC sp_dropextendedproperty 'MS_Description','user',dbo,'table','CONTRACT',null,null EXEC sp_dropextendedproperty 'MS_Description','user',dbo,'table','CONTRACT','column',BrandId --4444444444444==============================添加表字段描述(方法1、2) --表描述 EXEC sp_addextendedproperty N'MS_Description', '添加表描述', N'user', N'dbo', N'table', N'表', NULL, NULL --方法1 EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'添加字段描述1' , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'TABLE',@level1name=N'表', @level2type=N'COLUMN',@level2name=N'列' GO --方法2 EXECUTE sp_addextendedproperty N'MS_Description', '添加字段描述2', N'user', N'dbo', N'table', N'表', N'column', N'列' GO --5555555555555==============================修改表字段描述信息 -- EXEC sp_updateextendedproperty 'MS_Description','修改表描述',N'user', N'dbo', N'table','表',null,null EXEC sp_updateextendedproperty 'MS_Description','修改字段描述',N'user', N'dbo', N'table','表','column',a1 --666666666666666==============================修改列允許為空或不為空 -- --允許為空 alter table 表 ALTER COLUMN 列 bit NULL alter table 表 ALTER COLUMN 列 BIT NOT NULL --77777777777777==============================創建表主鍵索引 -- --檢查索引是否存在,存在則刪除 if exists (select 1 from sysindexes where id = object_id('dbo.SCCLWayBill') and name = 'IX_SCCLWayBill_DispatchNo' and indid > 0 and indid < 255) drop index dbo.SCCLWayBill.IX_SCCLWayBill_DispatchNo GO --創建主鍵 CREATE TABLE tbname( id INT NOT NULL, --不為空 NAME NVARCHAR(20) NULL, --可為空 STATUS INT DEFAULT 0, --默認值 constraint PK_tbname primary key (id), --主鍵 waijian int foreign key(waijian) references A_tablename(AID)--外鍵 ) --修改主鍵 --1首先刪除主鍵 ALTER TABLE [tbname] DROP CONSTRAINT [PK_tbname] --2創建主鍵 ALTER TABLE [tbname] ADD CONSTRAINT [PK_tbname] PRIMARY KEY ( column1 ASC, column2 ASC, column3 ASC ) GO --創建索引 --聚集索引 create index IX_tbname_id on dbo.tbname ( id ASC ) go --非聚集索引 CREATE nonclustered index IX_tbname_id on dbo.tbname ( id ASC ) go --8888888888888==============================表列增加默認值 ALTER TABLE [tbName] ADD CONSTRAINT [約束名(自定義)] DEFAULT ((2)) FOR [ColumnName] ALTER TABLE [tbName] ADD DEFAULT ((1)) FOR [column] --如果需要修改默認值 ALTER TABLE [tbName] DROP CONSTRAINT [約束名(自定義)] --刪除約束 然後再新增 -- --999999999999999============================Split函數 CREATE Function [Split](@Sql varchar(8000),@Splits varchar(10)) returns @temp Table (a varchar(100)) As Begin Declare @i Int Set @Sql = RTrim(LTrim(@Sql)) Set @i = CharIndex(@Splits,@Sql) While @i >= 1 Begin Insert @temp Values(Left(@Sql,@i-1)) Set @Sql = SubString(@Sql,@i+1,Len(@Sql)-@i) Set @i = CharIndex(@Splits,@Sql) End If @Sql <> '' Insert @temp Values (@Sql) Return End --測試 --SELECT * FROM dbo.Split('L.I.U.D.O.N.G','.') -------------------10=========================FOR XML AUTO DECLARE @temT TABLE(ID INT,NAME NVARCHAR(50)) INSERT INTO @temT VALUEs(1,'liudong'); INSERT INTO @temT VALUEs(2,'shichunjie'); SELECT * FROM @temT FOR XML AUTO -------------------11=========================FOR XML PATH SELECT * FROM @temT FOR XML PATH SELECT * FROM @temT FOR XML PATH('T') SELECT * FROM @temT FOR XML PATH('') SELECT cast(id AS NVARCHAR(10))+',',NAME+'.' FROM @temT FOR XML PATH('') -------------------12=========================stuff('abc',2,1,'B') select stuff('abc',2,1,'B') --結果 aBc -- -------------------13=========================聯合多表Update操作 -- UPDATE t1 SET tclo=1 FROM t1,t2 WHERE t1.id=t2.id -------------------14=========================日期函數 -- --datediff(ex,begin,end) --時差--ex:表達式,begin:開始時間,end:結束時間,ex=end-begin SELECT DATEDIFF(hh,'2012-03-03 10:11','2012-03-04 09:11') --加時--ex:表達式,加時,時間 SELECT DATEADD(hh,24,'2012-03-03 10:11') -------------------15=========================行列互轉(兩種方式) --1 定義變量 DECLARE @result VARCHAR(255) SET @result = '' SELECT @result = @result + cast(tdd.[name] as varchar(255)) +',' FROM T tdd(NOLOCK) WHERE 1=1 IF(len(@result) - 1<0) SELECT '' AS [name] ELSE SELECT LEFT(@result,len(@result) - 1) AS [name] --2 使用 for xml path(''),使用stuff函數將第一個,號替換掉 SELECT [name]=stuff((select ','+cast(tt.[name] AS VARCHAR(10)) from T tt where 1=1 for xml path('')), 1, 1, '') FROM @temp -------------------16=========================查看/刪除/創建表約束 --查看表約束 sp_helpconstraint 表名 --刪除表約束 ALTER TABLE 表名 DROP CONSTRAINT 約束名 --創建表約束 ALTER TABLE 表名 ADD CONSTRAINT 約束名 DEFAULT ('') FOR 字段 -------------------17===========================With ** AS()使用 --- 臨時表 WITH tt AS ( SELECT * FROM tab ) -- 遞歸 WITH dept AS( SELECT cd.Code,cd.CodeName FROM CostDept cd(NOLOCK) WHERE cd.Code='0123' UNION ALL SELECT cd1.Code,cd1.CodeName FROM dept JOIN CostDept cd1(NOLOCK) ON dept.Code=cd1.ParentCode ) SELECT * FROM dept ------------------------------------------------------------------------------- SELECT tt.DeliveryOrderNO,tt.SysOrderType FROM ( SELECT T.DeliveryOrderNO, SysOrderType=stuff((select ','+cast(tt.SysOrderType AS VARCHAR(10)) from TMS_DeliveryOrderDetail tt where tt.DeliveryOrderNO=T.DeliveryOrderNO for xml path('')), 1, 1, '') FROM ( SELECT tdo.DeliveryOrderNO FROM TMS_DeliveryOrder tdo WHERE 1=1 and tdo.ReceivedDate>='2012-03-01 01:01' AND tdo.ReceivedDate<=GETDATE() AND tdo.OrderType=2 --AND tdo.DeliveryOrderNO='0103Y1203010001' ) T ) TT WHERE 1=1 -- and (charindex('13',SysOrderType)>0 OR charindex('13',SysOrderType)>0 OR charindex('16',SysOrderType)>0 OR charindex('17',SysOrderType)>0 OR charindex('18',SysOrderType)>0) --AND charindex('13',SysOrderType)=0 AND charindex('16',SysOrderType)=0 AND charindex('18',SysOrderType)=0 AND (charindex('10',SysOrderType)>0 OR charindex('17',SysOrderType)>0) AND (charindex('13',SysOrderType)=0 AND charindex('16',SysOrderType)=0 AND charindex('18',SysOrderType)=0) SELECT * FROM TMS_DeliveryOrderDetail tdod WHERE tdod.FactoryID IS NOT NULL -------------------18================Update 時增加 @ERROR<>0 OR @ROWCOUNT<>cnt(影響行數) 判斷 IF(@@ERROR<>0 OR @@ROWCOUNT<>1) BEGIN ROLLBACK PRINT 'RollBack' RETURN END PRINT 'Commit Start' COMMIT PRINT 'Commit Over' SELECT * FROM TMS_DeliveryOrderDetail tdod WHERE EXISTS( SELECT * FROM TMS_SYN_Redeploy tsr WHERE tsr.RedeployCode IN ( ) --------------------19=====================大數據表循環刪除腳本 DECLARE @icount INTEGER; DECLARE @StartDate date; DECLARE @EndDate date; SET @StartDate = CONVERT(date,'2012-08-01'); SET @EndDate = CONVERT(date,'2012-08-31'); ; WHILE 1=1 BEGIN DELETE TOP (20000) FROM [dbo].[ForecastBaseData] WHERE CreateDate>=@StartDate and CreateDate<=@EndDate ; SET @icount = @@ROWCOUNT ; IF @icount<>20000 BREAK ; WAITFOR DELAY '00:00:05' END --------------------19 END=====================