# SQL 語法
### 查詢所有 DB Table 資料筆數
```sql
SELECT S.NAME '結構描述', O.NAME '資料表名稱', P.ROWS '列總數'
FROM SYS.OBJECTS O INNER JOIN SYS.SCHEMAS S
ON O.SCHEMA_ID = S.SCHEMA_ID
INNER JOIN SYS.PARTITIONS P
ON O.OBJECT_ID = P.OBJECT_ID
WHERE (O.TYPE = 'U') AND
(P.INDEX_ID IN (0,1))
ORDER BY P.ROWS DESC,S.NAME, O.NAME ASC;
```
## 分批刪除資料
```sql
DECLARE @Count INT = -1
SET NOCOUNT ON
WHILE @Count <> 0
BEGIN
BEGIN TRAN
DELETE TOP(1000)
FROM [dbo].[Table]
WHERE [條件]
OPTION(MAXDOP 1)
SET @Count = @@ROWCOUNT
COMMIT
END
SET NOCOUNT OFF
```
## 資料筆數
```sql
SELECT S.NAME '結構描述', O.NAME '資料表名稱', P.ROWS '列總數'
FROM SYS.OBJECTS O INNER JOIN SYS.SCHEMAS S
ON O.SCHEMA_ID = S.SCHEMA_ID
INNER JOIN SYS.PARTITIONS P
ON O.OBJECT_ID = P.OBJECT_ID
WHERE (O.TYPE = 'U') AND
(P.INDEX_ID IN (0,1))
ORDER BY P.ROWS DESC,S.NAME, O.NAME ASC;
```
## SQL 取得 Table Model
```sql
declare @TableName sysname = 'MutualSupport'
declare @Result varchar(max) = 'public class ' + @TableName + 'Model
{'
select @Result = @Result + '
public ' + ColumnType + NullableSign + ' ' + ColumnName + ' { get; set; }
'
from
(
select
replace(col.name, ' ', '_') ColumnName,
column_id ColumnId,
case typ.name
when 'bigint' then 'long'
when 'binary' then 'byte[]'
when 'bit' then 'bool'
when 'char' then 'string'
when 'date' then 'DateTime'
when 'datetime' then 'DateTime'
when 'datetime2' then 'DateTime'
when 'datetimeoffset' then 'DateTimeOffset'
when 'decimal' then 'decimal'
when 'float' then 'double'
when 'image' then 'byte[]'
when 'int' then 'int'
when 'money' then 'decimal'
when 'nchar' then 'string'
when 'ntext' then 'string'
when 'numeric' then 'decimal'
when 'nvarchar' then 'string'
when 'real' then 'float'
when 'smalldatetime' then 'DateTime'
when 'smallint' then 'short'
when 'smallmoney' then 'decimal'
when 'text' then 'string'
when 'time' then 'TimeSpan'
when 'timestamp' then 'long'
when 'tinyint' then 'byte'
when 'uniqueidentifier' then 'Guid'
when 'varbinary' then 'byte[]'
when 'varchar' then 'string'
else 'UNKNOWN_' + typ.name
end ColumnType,
case
when col.is_nullable = 1 and typ.name in ('bigint', 'bit', 'date', 'datetime', 'datetime2', 'datetimeoffset', 'decimal', 'float', 'int', 'money', 'numeric', 'real', 'smalldatetime', 'smallint', 'smallmoney', 'time', 'tinyint', 'uniqueidentifier')
then '?'
else ''
end NullableSign
from sys.columns col
join sys.types typ on
col.system_type_id = typ.system_type_id AND col.user_type_id = typ.user_type_id
where object_id = object_id(@TableName)
) t
order by ColumnId
set @Result = @Result + '
}'
print @Result
```
## RAISERROR SQL例外
```sql
UPDATE ApplyCar
SET DiasterId = DiasterId
WHERE Id > 9999
IF @@ROWCOUNT < 1
RAISERROR('更新失敗',11,0)
```
### 第一個引數: msg_id | msg_str | @local_variable
- msg_id:表示可以是一個sys.messages表中定義的訊息代號;
- 使用 sp_addmessage 儲存在 sys.messages 目錄檢視中的使用者定義錯誤訊息號。
- 使用者定義錯誤訊息的錯誤號應當大於 50000。
- msg_str:表示也可以是一個使用者定義訊息,該錯誤訊息最長可以有 2047 個字元;
- @local_variable:表示也可以是按照 msg_str 方式的格式化字串變數。
### 第二個引數:severity
- 使用者定義的與該訊息關聯的嚴重級別。(這個很重要)
- 任何使用者都可以指定 0 到 18 之間的嚴重級別。
- [0,10]的閉區間內,不會跳到catch;
- 如果是[11,19],則跳到catch;
- 如果[20,無窮),則直接終止資料庫連線;
### 第三個引數:state
- 如果在多個位置引發相同的使用者定義錯誤,
- 則針對每個位置使用唯一的狀態號有助於找到引發錯誤的程式碼段。
- 介於 1 至 127 之間的任意整數。(state 預設值為1)
- 當state 值為 0 或大於 127 時會生成錯誤!
### 第四個引數:argument
- 用於代替 msg_str 或對應於 msg_id 的訊息中的定義的變數的引數。
### 第五個引數:option
- 錯誤的自定義選項,可以是下表中的任一值:
- LOG :在錯誤日誌和應用程式日誌中記錄錯誤;
- NOWAIT:將訊息立即傳送給客戶端;
- SETERROR:將 @@ERROR 值和 ERROR_NUMBER 值設定為 msg_id 或 50000;
###### tags: `MSSQL`