Granite WMS/SQL

From Wikibooks, open books for an open world
Jump to navigation Jump to search

SQL tips and tricks

[edit | edit source]

Database

[edit | edit source]

Insert ID

[edit | edit source]
SET IDENTITY_INSERT tablename ON 
SET IDENTITY_INSERT tablename OFF

View sql result statistics

[edit | edit source]
SET STATISTICS IO ON
SET STATISTICS TIME ON

Find triggers in database

[edit | edit source]
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'

Disable all triggers in database

[edit | edit source]
EXEC sp_msforeachtable "ALTER TABLE ? DISABLE TRIGGER ALL"

Activity monitor - Release locks

[edit | edit source]

Right click on the SQL instance Open Activity monitor Click processes

Helpful query

Example to iterate / loop over records

[edit | edit source]
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