0
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

SQLServerで個人的に忘れがちなクエリまとめ

Last updated at Posted at 2022-02-08

すぐ忘れてしまうのでメモ
ごちゃごちゃですみません。。

リネーム処理

リネーム
EXEC sp_rename 'スキーマ.リネーム前テーブル名', 'リネーム後テーブル名';

テーブル作成&更新日時の確認

テーブル日時
SELECT
	name AS 'テーブル名',
	create_date AS '作成日時',
	modify_date AS '更新日時'
FROM
	sys.tables 
WHERE name like '%検索したいテーブル名%'
ORDER BY 更新日時 DESC

テーブルロック確認

テーブルロック
--管理者権限でのログインが必要
SELECT
     resource_type AS type --オブジェクトの種類
    ,resource_associated_entity_id as entity_id  --エンティティID
    ,( CASE WHEN resource_type = 'OBJECT' THEN
          OBJECT_NAME( resource_associated_entity_id )
      ELSE
          ( SELECT
              OBJECT_NAME( OBJECT_ID )
           FROM
             sys.partitions
           WHERE
               hobt_id=resource_associated_entity_id )
      END)
        AS object_name
    ,request_mode --ロックの種類
    ,request_type --要求の種類
    ,request_status  --状態
    ,request_session_id AS Session_id --セッションID
        ,(SELECT hostname 
          FROM sys.sysprocesses 
          WHERE spid = request_session_id) AS ProcessName
FROM
    sys.dm_tran_locks
WHERE
    resource_type <> 'DATABASE'
ORDER BY
    request_session_id

重複データの件数チェック

重複データの件数

SELECT COUNT( DISTINCT CONCAT(Aカラム, Bカラム)) AS 件数  FROM テーブル名 WHERE LEFT(Bカラム,2) = '00';

カラム検索

カラム検索

SELECT   SCHEMA_NAME(t.schema_id) AS SchemaName, 
	 T.name AS TableName,
         C.name AS ColumnName,
         K.name AS ColumnType
FROM     sys.tables AS T
            INNER JOIN sys.columns AS C
               ON T.object_id = C.object_id
            INNER JOIN sys.types AS K
               ON C.user_type_id = K.user_type_id
WHERE    C.name LIKE '%カラム名%'
ORDER BY SchemaName,
	 TableName,
         ColumnName;

列単位の照合順序の確認

照合順序の確認

SELECT t.name TableName, c.name ColumnName, collation_name  
FROM sys.columns c  
inner join sys.tables t on c.object_id = t.object_id
WHERE t.name = 'テーブル名称' ;  

随時追加していきます!

0
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?