-- 批量操作所有表中的相同字段 select'ALTER TABLE [dbo].['+tab.name+'] DROP COLUMN '+ col.name +''as sqlstr from sys.tables tab join sys.columns col on tab.object_id = col.object_id and col.name like'trial%'
这里筛选条件需要根据实际需求调整
批量删除索引
进行结构同步时,由于字段名变化,需要把所有的索引删除。
1 2 3 4
-- 删除所有索引 select'drop index '+o.name+'.'+i.name+';' from sys.indexes i join sys.objects o on i.object_id=o.object_id where o.type<>'S'
这里操作的是sys.indexes,可以根据需求筛选要删除的索引类型等。
1
select*from sys.indexes
批量删除主外键
当数据库表中存在主外键关系时,会阻止我们进行更改操作,所以需要删除主外键关系。
1 2 3 4 5
select 'alter table '+ quotename(schema_name(schema_id)) +'.'+ quotename(object_name(parent_object_id)) + ' drop constraint '+quotename(name) +';' from sys.foreign_keys
-- 放要执行的sql集合 select'drop index '+o.name+'.'+i.name+';' from sys.indexes i join sys.objects o on i.object_id=o.object_id where o.type<>'S'and is_primary_key<>1
open OneCursor ---打开游标 declare @ItemName nvarchar(max);---声明2个变量用于填充 fetch next from OneCursor into @ItemName ---移动到游标的第一行用于填充 while @@FETCH_STATUS=0 ---若为0,则代表成功执行 begin print @ItemName EXECUTE sys.sp_executesql @ItemName fetch next from OneCursor into @ItemName ----移动到游标的下一行并填充到变量中 end close OneCursor ---关闭游标 deallocate OneCursor --删除游标
批量执行
执行全部SQL语句
1 2 3 4 5 6 7 8 9
declare@sql nvarchar(max) = ( select 'alter table '+ quotename(schema_name(schema_id)) +'.'+ quotename(object_name(parent_object_id)) + ' drop constraint '+quotename(name) +';' from sys.foreign_keys for xml path('') ); exec sp_executesql @sql;
SELECT (casewhen a.colorder=1then ddd.value else''end) as "表名(中文)",--如果表名相同就返回空 (casewhen a.colorder=1then d.name else''end) as 表名,--如果表名相同就返回空 (casewhen a.colorder=1then ddd.value else''end) as 表说明,--如果表名相同就返回空 a.colorder as 字段序号, a.name as 字段名, (casewhen COLUMNPROPERTY( a.id,a.name,'IsIdentity')=1then'√'else''end) as 是否自增标识, (casewhen (SELECTcount(*) FROM sysobjects--查询主键 WHERE (name in (SELECT name FROM sysindexes WHERE (id = a.id) AND (indid in (SELECT indid FROM sysindexkeys WHERE (id = a.id) AND (colid in (SELECT colid FROM syscolumns WHERE (id = a.id) AND (name = a.name) ) ) ) ) ) ) AND (xtype ='PK'))>0then'√'else''end) as 主键,--查询主键END b.name as 类型, a.length as 占用字节数, COLUMNPROPERTY(a.id,a.name,'PRECISION') as 长度, isnull(COLUMNPROPERTY(a.id,a.name,'Scale'),0) as 小数位数, (casewhen a.isnullable=1then'√'else''end) as 允许空, isnull(e.text,'') as 默认值, isnull(g.[value],'') AS 字段说明 FROM syscolumns a leftjoin systypes b on a.xtype=b.xusertype innerjoin sysobjects d on a.id=d.id and d.xtype='U'and d.name<>'dtproperties' LEFTOUTERJOIN( SELECT major_id, value FROM sys.extended_properties WHERE name='MS_Description'AND minor_id =0) as ddd ON a.id = ddd.major_id leftjoin syscomments e on a.cdefault=e.id leftjoin sys.extended_properties g on a.id=g.major_id AND a.colid = g.minor_id where d.name like'%' orderby a.id,a.colorder
SELECT TOP 100 [cpu_time], [session_id], [request_id], [start_time] AS'开始时间', [status] AS'状态', --[command] AS '命令', dest.[text] AS'sql语句', DB_NAME([database_id]) AS'数据库名', --[blocking_session_id] AS '正在阻塞其他会话的会话ID', der.[wait_type] AS'等待资源类型', [wait_time] AS'等待时间', [wait_resource] AS'等待的资源', [dows].[waiting_tasks_count] AS'当前正在进行等待的任务数', [reads] AS'物理读次数', [writes] AS'写次数', [logical_reads] AS'逻辑读次数', [row_count] AS'返回结果行数' FROM sys.[dm_exec_requests] AS der INNERJOIN [sys].[dm_os_wait_stats] AS dows ON der.[wait_type]=[dows].[wait_type] CROSS APPLY sys.[dm_exec_sql_text](der.[sql_handle]) AS dest --WHERE [session_id]>50 ORDERBY [cpu_time] DESC;
SELECT TOP 10 dest.[text] AS'sql语句' FROM sys.[dm_exec_requests] AS der CROSS APPLY sys.[dm_exec_sql_text](der.[sql_handle]) AS dest WHERE [session_id]>50 ORDERBY [cpu_time] DESC;
SELECT TOP 10 total_worker_time/execution_count AS avg_cpu_cost, plan_handle, execution_count, (SELECTSUBSTRING(text, statement_start_offset/2+1, (CASEWHEN statement_end_offset =-1 THEN LEN(CONVERT(nvarchar(max), text)) *2 ELSE statement_end_offset END- statement_start_offset)/2) FROM sys.dm_exec_sql_text(sql_handle)) AS query_text FROM sys.dm_exec_query_stats ORDERBY [avg_cpu_cost] DESC;
select net_address, hostname,hostprocess,program_name,loginame,login_time from master.dbo.sysprocesses where dbid = DB_ID('xsteam_datacenter') orderby hostprocess;
select p.object_id, object_name=object_name(p.object_id), p.index_id, buffer_pages=count(*) from sys.allocation_units a,sys.dm_os_buffer_descriptors b, sys.partitions p where a.allocation_unit_id=b.allocation_unit_id and a.container_id=p.hobt_id and b.database_id=db_id() groupby p.object_id,p.index_id orderby buffer_pages desc