22. Distinct Values
A SQL query will return all
records, including:
• Departments you
cannot see
• Template records
SELECT
DISTINCT ObjectName
FROM Objects
SELECT
ObjectName, COUNT(*)
FROM Objects
GROUP BY ObjectName
[HAVING COUNT(*) = 1]
[HAVING COUNT(*) > 1]
23. We need to find records that
use a different string format
24. Formatting
Query using wildcards:
single character (?) or multi-character (*)
Format TMS Search
(646) 733-2239 “(???) ???-????”
646.733.2239 *???.???.????*
+44 (0)207379 8188 +*
(510) 652-8950 ext 223 “* ext*”
Chad M. “* ?.”
Cheryl & Edward *&*
Cheryl and Edward “* and *”
25. Formatting
Query using wildcards:
single character (_) or multi-character (%)
Format TMS Search
(646) 733-2239 ( _ _ _ ) _ _ _ - _ _ _ _
646.733.2239 % _ _ _ . _ _ _ . _ _ _ _ %
+44 (0)207379 8188 +%
(510) 652-8950 ext. 223 % ext%
Chad M. % _.
Cheryl & Edward %&%
Cheryl and Edward % and %
28. Data Length
CREATE PROCEDURE [dbo].[DM_Cache_Integer4] AS
UPDATE ObjContext
SET ObjContext.Integer4 = DATALENGTH(Objects.Description)
FROM ObjContext
INNER JOIN Objects
ON ObjContext.ObjectID = Objects.ObjectID
WHERE
DATALENGTH(Objects.Description) <> ObjContext.Integer4
UPDATE ObjContext
SET Integer4 = 0
WHERE
Integer4 IS NULL
GO
Execute Stored Procedure as a Job
34. DATA SCRUBBING
“It seems as if an age of genius
must be succeeded by an age of endeavor;
riot and extravagance by cleanliness and hard work.”
– Virginia Woolf
42. Updating with SQL
-- SCRIPT FOR UPDATING OBJECT NAMES FROM AN OBJECT PACKAGE
DECLARE @ObjectName varchar(256), @login varchar(32), @numberRoot varchar(32), @objPackage
varchar(64), @id int, @old varchar(256)
DECLARE @tempTable TABLE(ObjectID int, Processed tinyint)
-- NEW OBJECT NAME
SET @ObjectName = ‘containers’
-- OBJECT PACKAGE NAME
SET @objPackage = ‘SQL update containers’
SET @login = 'chad.petrovay‘
-- DO NOT EDIT SCRIPT BELOW
SQL for updating ObjectName; including in Audit Trail
43. Updating with SQL
INSERT INTO @tempTable
SELECT ObjectID, 0
FROM ObjPkgList OPL
INNER JOIN ObjectPackages OP
ON OPL.ObjectPackageID = OP.ObjectPackageID
AND OP.Name = @objPackage
WHILE EXISTS ( SELECT * FROM @tempTable WHERE Processed = 0 )
BEGIN
SELECT @id = MIN(ObjectID) FROM @tempTable WHERE Processed = 0
SELECT @old = ObjectName FROM Objects WHERE ObjectID = @id
UPDATE Objects SET ObjectName = @ObjectName WHERE ObjectID = @id
INSERT INTO AuditTrail (ObjectID, ModuleID, TableName, ColumnName, OldValue, NewValue,
LoginID) VALUES (@id, '1', 'Objects', 'ObjectName', @old, @ObjectName, @login)
UPDATE @tempTable SET Processed = 1 WHERE ObjectID = @id
END
SQL for updating ObjectName; including in Audit Trail
49. Updating with SQL
-- SCRIPT FOR MIGRATING A TEXT ENTRY
DECLARE @OldContext INT, @NewContext INT, @TextTypeID INT
SET @OldContext = 108
SET @NewContext = 89
SET @TextTypeID = 165
-- DO NOT EDIT SCRIPT BELOW
UPDATE TextEntries SET TableID = @NewContext
WHERE TableID = @OldContext AND TextTypeID = @TextTypeID
UPDATE TextTypes SET TableID = @NewContext
WHERE TableID = @OldContext AND TextTypeID = @TextTypeID
SQL for changing the context of a Text Entry
51. Updating with SQL
• ALWAYS backup your database
before trying a SQL script
• If possible, try the SQL script in a
sandbox environment first
• Modifications made using SQL may
not be supported by GS
54. Audit Trail
• Proactively monitor
changes to the
database to quickly
resolve and fix errors.
• Identify “culprits” who
may require additional
training.
57. FINAL THOUGHTS
“When every physical and mental resources is focused,
one's power to solve a problem multiplies tremendously.”
– Norman Vincent Peale