跳轉到內容

Granite WMS/SQL

來自華夏公益教科書,開放的世界,開放的書籍

SQL 技巧和竅門

[編輯 | 編輯原始碼]

資料庫

[編輯 | 編輯原始碼]

插入 ID

[編輯 | 編輯原始碼]
SET IDENTITY_INSERT tablename ON 
SET IDENTITY_INSERT tablename OFF

檢視 SQL 結果統計

[編輯 | 編輯原始碼]
SET STATISTICS IO ON
SET STATISTICS TIME ON

在資料庫中查詢觸發器

[編輯 | 編輯原始碼]
select * from sys.triggers

select [definition] from sys.sql_modules m
inner join sys.objects obj on obj.object_id=m.object_id 
where obj.type ='TR'

停用資料庫中的所有觸發器

[編輯 | 編輯原始碼]
EXEC sp_msforeachtable "ALTER TABLE ? DISABLE TRIGGER ALL"

活動監視器 - 釋放鎖

[編輯 | 編輯原始碼]

右鍵單擊 SQL 例項 開啟活動監視器 點選程序

有用的查詢

迭代/迴圈記錄的示例

[編輯 | 編輯原始碼]
declare @id bigint --id to loop
select @id = min(id) from OptionalFields where AppliesTo = 'MASTERITEM' --example get all optional fields for masteritems

while @id is not null
begin
	--do your thing insert update
	insert into OptionalFieldValues_MasterItem(Belongsto_id, OptionalField_id) 
    select ID, @id from MasterItem;
    --get next id
    select @id = min(id) from OptionalFields where ID > @id
end
華夏公益教科書