T-SQL Notes
/*************************************************
SQL Tips
*************************************************/
/********** Rename **********/
USE WORK;
GO
EXEC sp_rename 'Sales.SalesTerritory', 'SalesTerr';
USE WORK;
ALTER SCHEMA dbo TRANSFER location.LOCATION_APPENDALL;
/********** Check Job ID **********/
select 'SPID = ' + cast(@@spid as varchar)
exec sp_who2 'active'
/********** Check Job status **********/
select
p.spid
, right(convert(varchar,
dateadd(ms, datediff(ms, P.last_batch, getdate()), '1900-01-01'),
121), 12) as 'batch_duration'
, P.program_name
from master.dbo.sysprocesses P
where P.spid > 50
and P.status not in ('background', 'sleeping')
and P.cmd not in ('AWAITING COMMAND'
,'MIRROR HANDLER'
,'LAZY WRITER'
,'CHECKPOINT SLEEP'
,'RA MANAGER')
order by batch_duration desc
/********** Check free space **********/
select * from tempdb.sys.objects
USE work
GO
sp_spaceused
/* Check active transaction */
IF OBJECT_ID('tempdb..#OpenTranStatus') IS NOT NULL DROP TABLE #OpenTranStatus
-- Create the temporary table to accept the results.
CREATE TABLE #OpenTranStatus (
ActiveTransaction varchar(25),
Details sql_variant
);
-- Execute the command, putting the results in the table.
INSERT INTO #OpenTranStatus
EXEC ('DBCC OPENTRAN WITH TABLERESULTS, NO_INFOMSGS');
-- Display the results.
SELECT * FROM #OpenTranStatus;
/********** Drop multiple table v1 **********/
Use WORK
declare @cmd varchar(4000)
declare cmds cursor for
SELECT CONCAT( 'DROP TABLE ', TABLE_CATALOG, '.', TABLE_SCHEMA, '.', table_name , ';' )
AS statement FROM information_schema.tables WHERE table_name LIKE '%[_]t'
open cmds
while 1=1
begin
fetch cmds into @cmd
if @@fetch_status != 0 break
exec(@cmd)
end
close cmds;
deallocate cmds
Use ANC
SELECT * FROM information_schema.tables
Use WORK
SELECT * FROM information_schema.tables WHERE table_name LIKE '%test_%' and TABLE_SCHEMA = 'dbo'
/********** Drop multiple table v2 **********/
Use WORK
declare @cmd varchar(4000)
declare cmds cursor for
SELECT CONCAT( 'DROP TABLE ', TABLE_CATALOG, '.', TABLE_SCHEMA, '.', table_name , ';' )
AS statement FROM information_schema.tables WHERE table_name LIKE '%test_%' and TABLE_SCHEMA = 'dbo'
open cmds
while 1=1
begin
fetch cmds into @cmd
if @@fetch_status != 0 break
exec(@cmd)
end
close cmds;
deallocate cmds
/************ Union table **************/
USE WORK;
select * into WORK.dbo.ROOFAGE_PREDICTION_JAN2016 from (
select * from WORK.dbo.test1
union all
select * from WORK.dbo.test2
union all
select * from WORK.dbo.test3
union all
select * from WORK.dbo.test4
union all
select * from WORK.dbo.test5
union all
select * from WORK.dbo.test6
union all
select * from WORK.dbo.test7
union all
select * from WORK.dbo.test8
union all
select * from WORK.dbo.test9
union all
select * from WORK.dbo.test10
union all
select * from WORK.dbo.test11
union all
select * from WORK.dbo.test12
union all
select * from WORK.dbo.test13
union all
select * from WORK.dbo.test14
union all
select * from WORK.dbo.test15
union all
select * from WORK.dbo.test16
) as tmp
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
다양한 언어의 JSONJSON은 Javascript 표기법을 사용하여 데이터 구조를 레이아웃하는 데이터 형식입니다. 그러나 Javascript가 코드에서 이러한 구조를 나타낼 수 있는 유일한 언어는 아닙니다. 저는 일반적으로 '객체'{}...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.