Wednesday 18 November 2015



Similar documents
Jason S Wong Sr. DBA IT Applications Manager DBA Developer Programmer M.S. Rice 88, MBA U.H. 94(MIS)

USING FILERELICATIONPRO TO REPLICATE SQL SERVER

How to schedule and automate backups of SQL Server databases in SQL Server Express Editions

SQL Server Database Coding Standards and Guidelines

Data Compression in Blackbaud CRM Databases

Access Queries (Office 2003)

Sophos Enterprise Console Auditing user guide. Product version: 5.2

Migrating from Sybase to SQL Server

Analyzing & Optimizing T-SQL Query Performance Part1: using SET and DBCC. Kevin Kline Senior Product Architect for SQL Server Quest Software

DB2 for z/os: Utilities and Application Development

Chancery SMS Database Split

1. Database basics. 2. Central or distributed database. MonitorMagic Database Reference Version 1.2, November 2004

Siemens Teamcenter Oracle -to-sql Server 2008 Migration Guide

Recovering the master Database

Dynamics NAV/SQL Server Configuration Recommendations

SoftwarePlanner Active Directory Authentication

Microsoft SQL connection to Sysmac NJ Quick Start Guide

Tutorial: How to Use SQL Server Management Studio from Home

Business Intelligence. 11. SSIS, ETL January 2014.

Using SQL Server Management Studio

TRAIN MONITORING DATA INTEGRITY MICHELLE LELEMPSIS, CAUSEIS

Auditing In SQL Server. SQL Saturday #486 - RVA Presented By Brad McKuhen

Forms Printer User Guide

SQL Server An Overview

FmPro Migrator - FileMaker to SQL Server

Handle Tool. User Manual

QAD Enterprise Applications. Training Guide Demand Management 6.1 Technical Training

Welcome to the topic on queries in SAP Business One.

CINCH Software Installation Instructions

From Firebird 1.5 to 2.5

Juris Year-End Checklist 2009

IBM WebSphere Adapter for PeopleSoft Enterprise Quick Start Tutorials

Developing Web Applications for Microsoft SQL Server Databases - What you need to know

Virto Pivot View for Microsoft SharePoint Release User and Installation Guide

Ingres Backup and Recovery. Bruno Bompar Senior Manager Customer Support

Microsoft SQL Server OLTP Best Practice

Server & Workstation Installation of Client Profiles for Windows

Database Query 1: SQL Basics

Introduction. Part I: Finding Bottlenecks when Something s Wrong. Chapter 1: Performance Tuning 3

On-Premises Feature Creating Custom Reports and Dashboards

Bidirectional replication for InterBase and Firebird

Microsoft Dynamics GP Audit Trails

SQL Server Maintenance Plans

StruxureWare Power Monitoring Database Upgrade FAQ

Dr.Backup Release Notes - Version

SQL. Short introduction

MYSQL DATABASE ACCESS WITH PHP

Backing Up and Restoring Data

Toad for Oracle Installation Guide

Database Migration : An In Depth look!!

Intro to Databases. ACM Webmonkeys 2011

VeriCentre 3.0 Upgrade Pre-Installation and Post Installation Guidelines

Measuring Firebird Disc I/O

Database Studio is the new tool to administrate SAP MaxDB database instances as of version 7.5.

NovaBACKUP. User Manual. NovaStor / November 2011

Database Design Standards. U.S. Small Business Administration Office of the Chief Information Officer Office of Information Systems Support

How To Hack A Sql Server Rootkit With A Rootkit

news from Tom Bacon about Monday's lecture

PROCESSES LOADER 9.0 SETTING. Requirements and Assumptions: I. Requirements for the batch process:

Migrate Topaz databases from One Server to Another

Introduction This document s purpose is to define Microsoft SQL server database design standards.

Microsoft. MCSA upgrade to SQL Server 2012 Certification Courseware. Version 1.0

A Document Retention System for Eye Care Practices. Release Notes. Version 7.5 October A Milner Technologies, Inc. Solution

NSSRS Desktop Database Version 11.0 User Guide Version 1.0 July 15, 2015

Online School Payments (OSP) User Guide

Vault Project - Plant Database Replication. Contents. Software Requirements: AutoCAD Plant 3D 2016 and AutoCAD P&ID 2016

A basic create statement for a simple student table would look like the following.

Troubleshooting / FAQ

White Paper

SAP InfiniteInsight Explorer Analytical Data Management v7.0

TestManager Administration Guide

CSI 2132 Lab 3. Outline 09/02/2012. More on SQL. Destroying and Altering Relations. Exercise: DROP TABLE ALTER TABLE SELECT

SPI Backup via Remote Terminal

Title. Syntax. stata.com. odbc Load, write, or view data from ODBC sources. List ODBC sources to which Stata can connect odbc list

DELETE DUPLICATE S IN THE EMC XTENDER ARCHIVE SYSTEM USING THE MSGIDCRACKER UTILITY

Execution Plans: The Secret to Query Tuning Success. MagicPASS January 2015

Big Data and Analytics by Seema Acharya and Subhashini Chellappan Copyright 2015, WILEY INDIA PVT. LTD. Introduction to Pig

Page 1 of 22. Frequently Asked Questions & Troubleshooting Tips for. AHIMA Virtual Lab THE HELP PAGE: A STUDENT S BEST FRIEND

Performance. Optimizing Performance. Microsoft Dynamics CRM 3.0. White Paper. Date: January 10,

Setting Up Person Accounts

TurboTag User Manager Software Version Setup and Operating Instructions. Table of Contents

GEPL Capital Mobile Trading App

USER GUIDE Appointment Manager

SharePoint 2010 Farm Restore

MOVES Batch Mode: Setting up and running groups of related MOVES run specifications. EPA Office of Transportation and Air Quality 11/3/2010

ICE for Eclipse. Release 9.0.1

Hands-On UNIX Exercise:

Wind River Financial iprocess Setup Guide for IOS Devices

Lenovo Online Data Backup User Guide Version

WhatsUp Gold v11 Features Overview

Guidelines for Installing SQL Server and Client (SQL Server Management Studio)

Database Fundamentals

NetSuite OpenAir Mobile for Android User Guide Version 1.3

Redundancy Options. Presented By: Chris Williams

Microsoft Dynamics GP. Extender User s Guide

AWS Schema Conversion Tool. User Guide Version 1.0

Backing Up TestTrack Native Project Databases

Managing User Accounts and User Groups

Search help. More on Office.com: images templates

Pharos Uniprint 8.4. Maintenance Guide. Document Version: UP84-Maintenance-1.0. Distribution Date: July 2013

Transcription:

TRAIN + NETWORK + LEARN STORED PROCEDURES AND TRIGGERS DOUG MORRIS, COMPUTER SYSTEM INNOVATIONS, INC. TRAIN + NETWORK+ LEARN AGENDA STORED PROCEDURES AND TRIGGERS Stored Procedures What When How Triggers What When How Tips to leave with Special Thanks to Bruce Wilson of RSM 1

STORED PROCEDURES - WHAT Saved script of SQL commands, run as a unit Similar to function call or subroutine in other languages Accepts parameters, returns parameters Can also produce result sets Stored procedures are saved inside the database STORED PROCEDURES - WHEN DEGREES OF INTRUSIVENESS 1. Assemble data and output as result set (more complex than a view can handle) 2. System administration 3. Insert/update/delete non-imis tables 4. Update imis tables 5. Insert or delete in imis tables 2

OUTPUT ONLY PROCEDURE Behavior is similar to a view Output can be sorted, can use "compute" May output more than one result set May not be joined to other tables May use parameters to limit scope of data being handled OUTPUT ONLY PROCEDURE create procedure csi_sp_showrecentusers as begin set nocount on select UserId, UserName, LastLogin from Users order by LastLogin desc end go grant exec on csi_sp_showrecentusers to IMIS 3

OUTPUT ONLY PROCEDURE create procedure csi_sp_showmembersnotregistered as begin declare @tpeople table (ID varchar(10)) insert @tpeople (ID) select n.id from Name n where n.member_record = 1 delete p from @tpeople p join Orders o on o.st_id = p.id join Order_Meet om on om.order_number = o.order_number where om.meeting = 'CONF08' select n.id, n.full_name, n.email from @tpeople p join Name n on n.id = p.id End and what is missing here? NOTES FROM THE FIELD TEMP TABLES #temp tables are created in tempdb, are slower but can be much larger @table vars live only in memory, faster but smaller Both can be used anywhere a table can Cannot contain "text" or "image" types Create all as early in the procedure as you can ##globaltemp tables are visible to all processes don't use 4

NOTES FROM THE FIELD DOUG LIKES #temp tables over @table vars Because that s what he grew up with! POPULATING A SUMMARY FIELD Create a Customizer field for your summary Calculate and overwrite the data Schedule procedure as needed to keep it fresh Example: populate Name_Demo.STAFF_SIZE by counting people linked to this company 5

CSI_SP_UPDATESTAFFSIZE create procedure csi_sp_updatestaffsize as begin make a temp table Create Table #TotalStaff (ID varchar(10), STAFF_SIZE int) total up count of staff per ID, Member's only! Insert #TotalStaff(ID, STAFF_SIZE) Select CO_ID, count(*) From Name Where MEMBER_RECORD=1 and CO_ID > ' ' group by CO_ID CSI_SP_UPDATESTAFFSIZE Log your changes! insert Name_Log (DATE_TIME, LOG_TYPE, SUB_TYPE, USER_ID, ID, LOG_TEXT) Select getdate(), 'CHANGE', 'CHANGE', 'csi_sp_staffsize', nd.id, 'Name_Demo.STAFF_SIZE: ' + convert(varchar(10),nd.staff_size) + ' > ' + convert(varchar(10), t.staff_size) From Name_Demo nd Join #TotalStaff t on nd.id=t.id Where nd.staff_size<>t.staff_size 6

CSI_SP_UPDATESTAFFSIZE now update the actual data in imis Update nd Set nd.staff_size=t.staff_size From Name_Demo nd Join #TotalStaff t on nd.id=t.id Where nd.staff_size<>t.staff_size this line is important don't waste updates when not needed drop table #TotalStaff clean up! end Grant Exec on csi_sp_updatestaffsize to imis NOTES FROM THE FIELD - UPDATING IMIS Warning: no lifeguard on duty The reality is many organizations do it Pick your battles carefully and stay as close to the farm as you can Update in places imis isn't very particular Customizer, Activity, descriptive fields Mimic exactly unless you have a reason not to Log incessantly, think about how to "undo" Testing and backups are essential 7

NOTES FROM THE FIELD NAMING AND OTHER CONVENTIONS Table, view names are nouns, procedure names start with verbs, functions are usually nouns Don t start with sp_, create your own prefix (e.g. csi_ or abc_) Every name should be descriptive and unambiguous Table nicknames are the capital letters from table name (E.G. Name_Address = na) Remember, the person dealing with your stored procedure down the road, may not be you add comments and think about how it looks to the outside world sometimes an elegant script is not easy to read! Indent, space, keep it clean.. PARAMETERS Use the procedure for more than one thing Pass in information to control what the procedure does Which ID or Meeting to use What mode to work in Whether to update or just display Limit access to one row/group at a time for performance Use Crystal to prompt for the parameter 8

PARAMETERS create procedure csi_sp_showmembersnotregistered @psmeeting varchar(10) not NULL as begin... join Order_Meet om on om.order_number = o.order_number where om.meeting = @psmeeting WRITING TO A NON-IMIS TABLE Create the table outside of the procedure Don't insert using "select *" or without specifying target fields Don't use "select into" Use temp tables (@temp or #temp) for interim work tables 9

CREATING NON-IMIS TABLES Use a script, not the GUI Good basic fields: SEQN int identity (if you don't have a natural key) ADDED_DATE datetime not NULL default getdate() ADDED_BY nvarchar(128) not NULL default USER_NAME() STATUS int not NULL default 0 (can be useful to track records for other processing) Grant permissions NOTES FROM THE FIELD CREATING IMIS COMPATIBLE TABLES TableName in mixed case FIELD_NAME in upper with underscores not NULL except datetime and text Match imis lengths Define a primary key with clustered index Include relevant keys for appropriate linking Don't duplicate non-key data unless you want a history 10

MEMBERSHIP COUNTS SNAPSHOT Custom table with snapshot date, member type, category, chapter and count Procedure run monthly to capture numbers MEMBERSHIP COUNTS SNAPSHOT create table csi_membershipcounts ( SEQN int identity, SNAPSHOT_DATE datetime not NULL, -- required MEMBER_TYPE varchar(5) not NULL default '', CATEGORY varchar(5) not NULL default '', CHAPTER varchar(15) not NULL default '', QUANTITY int ) go alter table csi_membershipcounts add PK_MembershipCounts primary key clustered (SEQN) go create index imembershipcountssnapshot_date on csi_membershipcounts(snapshot_date) go 11

MEMBER COUNTS PROCEDURE create procedure csi_sp_capturemembershipcounts as begin declare @SnapshotDate datetime set @SnapshotDate = convert(varchar(8), getdate(), 112) -- date with no time insert csi_membershipcounts (SNAPSHOT_DATE, MEMBER_TYPE, CATEGORY, CHAPTER, QUANTITY) select @SnapshotDate, n.member_type, n.category, n.chapter, count(*) from Name n where @SnapshotDate not in (select distinct SNAPSHOT_DATE from csi_membershipcounts) group by n.member_type, n.category, n.chapter end ADDING AN ACTIVITY Activity is an area ripe for customization imis has low requirements for this table: SEQN from Counter table Valid ID Activity type Transaction Date MEMBER_TYPE and CO_ID preferred If fields have lookups, they must match 12

NOTES FROM THE FIELD COUNTER TABLE Must update and extract value in a way that prevents overlap sp_asi_getcounter is unsafe without a transaction For counters without checksums, there is an easier way update Counter set LAST_VALUE = LAST_VALUE + N, @myvar = LAST_VALUE + 1 where COUNTER_NAME = 'CounterYouNeed' It's OK to add new counters to the table CREATE COMMITTEE ACTIVITY create p_createcommitteeactivity (@psid varchar(10), @pscommittee varchar(15), @pxeffdate datetime, @pxthrudate datetime, @psposition varchar(40)) as begin declare @lseqn int update c set LAST_VALUE = c.last_value + 1, @lseqn = c.last_value + 1, LAST_UPDATED = getdate(), UPDATED_BY = 'CreateCommittee' from Counter c where c.counter_name = 'Activity' end insert Activity (SEQN, ID, ACTIVITY_TYPE, PRODUCT_CODE, OTHER_CODE, DESCRIPTION, EFFECTIVE_DATE, THRU_DATE, ACTION_CODES, MEMBER_TYPE, CO_ID) select @lseqn, @psid, p.product_code, p.product_minor, p.title, @pxeffdate, @pxthrudate, @psposition, n.member_type, n.co_id) from Name n cross join Product p where n.id = @psid and p.prod_type = 'COMMITTEE' and p.product_minor = @pscommittee 13

NOTES FROM THE FIELD WATCH OUT FOR Adding Name records to imis There are a lot of tables and fields that are touched Updating Member_Type Don t forget to set MEMBER_RECORD, COMPANY_RECORD, PREVIOUS_MT, MT_CHANGE_DATE and log your change in Name_Log! Using Begin Tran and End Tran These can lock your db if your script fails! There are better ways to do this with Try and Catch TRIGGERS Cue scary music. 14

TRIGGERS - WHAT Stored procedures in response to an insert, update or delete on a table Can examine before/after versions of the table A trigger will always run, so it had better do the right thing The trigger must deal correctly with multi-row updates Greater than normal chance of causing imis to misbehave Important not to allow rowcounts or other output Because a response will crash imis TRIGGERS WHEN Witness important changes to certain tables, insert ID in a "queue" table for later review and processing (find who and when that data is being deleted by) Additional change logging "Suppress" an illegal change instead of rolling it back. (Update table again with info from deleted) Risky because remember, you may not be around when something goes wrong! 15

TRIGGERS Triggers can be for Inserts, Deletes and Updates (see final slide for why updates are not reliable) Inserted is the data that was inserted treat it like a copy of your table Deleted is the data that was deleted treat it the same as Inserted in terms of structure. Here is sample code to track mysterious deletions on certain tables we use this at CSI TRIGGER SAMPLE CODE 1. Create the backup table 16

TRIGGER SAMPLE CODE 2. Create a DELETE trigger on the Activity table TRIGGER - COMMENTS ASI is using Triggers now for their new Panel Designer tables Please try to avoid using them unless writing to non-imis tables Even then make sure you do not return any data! (Set NOCOUNT On 17

TRIGGERS FROM THE FIELD A script calling a stored procedure, running every 5 minutes is almost always better than a trigger imis writes data in an order that you are not aware of and WHEN you are not aware of A trigger on a demographic table that updates the name table might have its data overwritten if imis writes to the demographic table first and then the Name table imis re-writes data when you don t expect it to e.g. all Activities are re-written when you post a batch! Update Triggers are not reliable to detect changing of data THANK YOU Doug Morris Computer System Innovations, Inc. 855 W Prairie Ave Wheaton, IL 60187 USA +1 630 681-1100 dmorris@csiinc.com @dougcsi (don t tweet ) 18

WITH THANKS TO OUR SPONSORS NIUG DISCOVERY CONFERENCE 19