Skip to content

Instantly share code, notes, and snippets.

View FembotDBA's full-sized avatar

Lara FembotDBA

  • California
View GitHub Profile
@FembotDBA
FembotDBA / gist:cc90750418ad807e0989
Created June 15, 2015 17:14
FizzBuzz answer using T-SQL
SELECT CASE
WHEN number % 3 = 0 AND number % 5 = 0 THEN 'FizzBuzz'
WHEN number % 3 = 0 THEN 'Fizz'
WHEN number % 5 = 0 THEN 'Buzz'
ELSE CONVERT(VARCHAR,number)
END AS FizzBuzz
FROM master.dbo.spt_values
WHERE number BETWEEN 1 AND 100
AND type='P'
@FembotDBA
FembotDBA / GenericNotSargable
Last active August 29, 2015 14:20
Generic Query Not SARGable
SELECT a.column1
, a.column2
, a.column3
, a.timePeriod
, a.column5
FROM schema.tablea a
LEFT JOIN schema.tableb AS b ON a.column2 = b.column1
LEFT JOIN schema.tablec AS c ON b.column2 = c.column1
WHERE CONVERT(VARCHAR(8),a.TimePeriod,112) >= @DateParameter
@FembotDBA
FembotDBA / Dynamically Update Statistics on All Databases on a server
Created September 16, 2014 22:15
Dynamically Updating Statistics on all Databases, With Sweet Potato Fries.
--statistics maintenance
--updates anything with more than 2% of changes
--or anything not updated within last 30 days
--This does full scan, because we have the time in our maintenance window
--This should be run after any index rebuilding as a statistics should get a update with a full scan
--Sidenote: If you have partitioned indexes and are running SQL 2012 or later, you should read up on how a index rebuild does not use fullscan
--And if you are using paritions, and switching partitions, you also may need to manually update your statistics
SET NOCOUNT ON;
@FembotDBA
FembotDBA / gist:599f757961d662011941
Created July 17, 2014 19:31
Declaring variables one line at a time.
--statement 2
DECLARE @a2 int;
DECLARE @b2 bit;
DECLARE @c2 varchar(50);
SET @a2 = 1989834;
SET @b2 = 0;
SET @c2 = 'Testing';
SELECT @a2, @b2, @c2;
@FembotDBA
FembotDBA / gist:0142f3a62b841b0adf53
Created July 17, 2014 19:29
Declaring multiple variables on the same line.
--statement 1
DECLARE @a int, @b bit, @c varchar(50);
SET @a = 1989834;
SET @b = 0;
SET @c = 'Testing';
SELECT @a, @b, @c;
@FembotDBA
FembotDBA / Refresh Orphaned Users
Created November 12, 2013 00:30
Refresh orphaned users in a database after restore.
IF EXISTS(SELECT * FROM tempdb..sysobjects WHERE id = object_id('tempdb..#t_users'))
drop table #t_users
CREATE TABLE #t_users ( [name] SYSNAME)
INSERT #t_users ( [name] )
SELECT [name]
FROM sysusers
WHERE
islogin = 1
@FembotDBA
FembotDBA / gist:5962630
Last active December 19, 2015 13:29
T-SQL Row Count of all user tables in database
SELECT o.name AS "Table Name", i.rowcnt AS "Row Count"
FROM sysobjects o, sysindexes i
WHERE i.id = o.id
AND indid IN(0,1)
AND xtype = 'u'
AND o.name <> 'sysdiagrams'
ORDER BY i.rowcnt DESC
COMPUTE SUM(i.rowcnt)
@FembotDBA
FembotDBA / gist:5835192
Created June 21, 2013 23:51
JQuery to widen table for SharePoint 2013 small edit windows
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.2.6/jquery.min.js" type="text/javascript"></script><script type="text/javascript">
$(function(){
$("table").css("width", "100%");
});
</script>
@FembotDBA
FembotDBA / gist:5778039
Last active December 18, 2015 11:49
find all objects in database with column name
--updated 6/27/2014 to include schema for each object
SELECT table_name AS [Table], column_name AS [Column], table_schema as [schema]
FROM information_schema.columns
WHERE column_name like '%' + @var + '%'
ORDER BY table_name, column_name
SELECT table_name as viewname, TABLE_SCHEMA as [schema]
FROM information_schema.views
WHERE view_definition like '%' + @var + '%'
@FembotDBA
FembotDBA / gist:5770396
Last active December 18, 2015 10:39
SQL Server output size of all tables in database
declare @SourceDB varchar(128)
set @sourceDB = 'jivesbs'
declare @sql varchar(128)
create table #tables(name varchar(128))
select @sql = 'insert #tables select TABLE_NAME from ' + @SourceDB + '.INFORMATION_SCHEMA.TABLES where TABLE_TYPE = ''BASE TABLE'''
exec (@sql)
create table #SpaceUsed (name varchar(128), rows varchar(11), reserved varchar(18), data varchar(18), index_size varchar(18), unused varchar(18))