One thing I have to remember is that INFORMATION_SCHEMA.ROUTINES.ROUTINE_DEFINITION only returns 4000 characters, which can mean that a search could return a false negative. I'm going to let this article speak for me on the deets.
Monday, October 11, 2021
Friday, May 7, 2021
How much space does all this take up?
Good posts on how to query the database to see how much space is used (specifically by tables, but also other types).
I like this query:
SELECT
t.NAME AS TableName,
s.Name AS SchemaName,
p.rows,
SUM(a.total_pages) * 8 AS TotalSpaceKB,
CAST(ROUND(((SUM(a.total_pages) * 8) / 1024.00), 2) AS NUMERIC(36, 2)) AS TotalSpaceMB,
SUM(a.used_pages) * 8 AS UsedSpaceKB,
CAST(ROUND(((SUM(a.used_pages) * 8) / 1024.00), 2) AS NUMERIC(36, 2)) AS UsedSpaceMB,
(SUM(a.total_pages) - SUM(a.used_pages)) * 8 AS UnusedSpaceKB,
CAST(ROUND(((SUM(a.total_pages) - SUM(a.used_pages)) * 8) / 1024.00, 2) AS NUMERIC(36, 2)) AS UnusedSpaceMB
FROM
sys.tables t
INNER JOIN
sys.indexes i ON t.OBJECT_ID = i.object_id
INNER JOIN
sys.partitions p ON i.object_id = p.OBJECT_ID AND i.index_id = p.index_id
INNER JOIN
sys.allocation_units a ON p.partition_id = a.container_id
LEFT OUTER JOIN
sys.schemas s ON t.schema_id = s.schema_id
WHERE
t.NAME NOT LIKE 'dt%'
AND t.is_ms_shipped = 0
AND i.OBJECT_ID > 255
GROUP BY
t.Name, s.Name, p.Rows
ORDER BY
TotalSpaceMB DESC, t.NameBut some other good ways were presented also, such as SSMS's built-in report, and a loop over 'sp_spaceused' with 'sp_msforeachtable'.
Thursday, April 8, 2021
SQL XML String Splitter
Wednesday, January 6, 2021
MS-DOS Batch File Copy with Date-Stamped Name
I created a batch file to copy the compiled exe of a Winform app, and then backup the source code to an archive folder named with the date/time stamp:
@echo off
cls
echo Date format = %date%
echo.
echo Time format = %time%
echo.
set Timestamp=%date:~10,4%%date:~4,2%%date:~7,2%_%time:~0,2%%time:~3,2%%time:~6,2%
set folder=_versions\wiki%Timestamp%\
xcopy app %folder% /s /e
copy app\Wiki\Wiki\obj\Debug\Wiki.exe _release\Wiki.exe
I got a lot of help for this from this StackExchange entry. I found this Steve Jansen article useful also.
Friday, February 28, 2020
SSMS Hotkey - Metadata Dump
Thursday, February 6, 2020
Var Assignment SELECT Gotcha
DECLARE @objid int = -1
SELECT @objid = [OBJECT_ID]
FROM sys.objects WHERE 1=0
SELECT @objid
I coded a script with the assumption that it would be null if the WHERE condition is not met, the same way a column would be null. But instead, it kept its original value.
To fix:
DECLARE @objid int = -1
SELECT @objid = CASE WHEN 1=0 THEN [OBJECT_ID] END
FROM sys.objects
SELECT @objid
This will provide the results we wanted.
Wednesday, January 8, 2020
SQL FOR XML for string concatenation
These posts go into the mechanics of the FOR XML, and some debate as to the pros and cons.
Monday, January 6, 2020
SQL Merge
Tuesday, December 24, 2019
Complicated Comments
DECLARE @x int
/*
-- */
SET @x = 1
-- /*
--*/ SET @x = 2
PRINT @x
Question: after running the statement above, what will the output be?
- 1
- 2
- Null
- An error occurs.
Monday, December 16, 2019
Removing 100% Duplicates
CREATE TABLE #src (x int, y int, z int)
GO
INSERT #src VALUES (1, 2, 3)
INSERT #src VALUES (1, 2, 3)
INSERT #src VALUES (1, 2, 3)
INSERT #src VALUES (10, 20, 30)
INSERT #src VALUES (100, 200, 300)
GO
SELECT * FROM #src
At this point, #src looks like this (I cannot seem to remove the odd white space in this table):
x y z
1 2 3
1 2 3
1 2 3
10 20 30
100 200 300
Now let's remove our duplicates:
DELETE TOP(2)
FROM #src
WHERE x = 1
SELECT * FROM #src
And we see the results:
x y z
1 2 3
10 20 30
100 200 300
In the DELETE TOP(n) part of the statement, n=#dupes - 1. In our case, we had 3 duplicates and removed 2 of them. Which brings up an important point - we used the "x=1" condition to identify the group, and we knew that it had exactly 3 rows in that dupe group. So if we generalize the problem, we may want to use dynamic SQL to build a solution:
; WITH DupeGroups AS (
SELECT
[x]
,[cnt] = COUNT(*)
FROM #src
GROUP BY [x]
HAVING COUNT(*) > 1
)
SELECT
This can then be executed using sp_SqlExec to remove the duplicates.
Wednesday, June 12, 2019
Default Schema
Saturday, May 25, 2019
Multiple UNPIVOTs
Wednesday, May 15, 2019
Conversion failed when converting the varchar value '*' to data type int.
Thursday, February 14, 2019
SSMS Slowness
Monday, February 11, 2019
CREATE TABLE Script for Temp Table
DECLARE @sql varchar(max); SET @sql = 'CREATE TABLE #capture_output (' + CHAR(13) + CHAR(10)
; WITH struct AS (
SELECT *
FROM TempDb.INFORMATION_SCHEMA.COLUMNS
WHERE [TABLE_NAME] = OBJECT_NAME(OBJECT_ID('TempDb..#tmp'), (SELECT Database_Id FROM SYS.DATABASES WHERE Name = 'TempDb'))
)
SELECT
@sql += CHAR(9) + ',' + QUOTENAME(struct.[COLUMN_NAME]) + ' ' + struct.[DATA_TYPE]
+ ISNULL('(' + CASE struct.[DATA_TYPE]
WHEN 'varchar' THEN CONVERT(varchar, struct.[CHARACTER_MAXIMUM_LENGTH])
WHEN 'decimal' THEN CONVERT(varchar, struct.[NUMERIC_PRECISION]) + ', ' + CONVERT(varchar, struct.[NUMERIC_SCALE])
END
+ ')', '')
+ CHAR(13) + CHAR(10)
FROM struct
SET @sql = REPLACE(@sql, '(' + CHAR(13) + CHAR(10) + CHAR(9) + ',', '(' + CHAR(13) + CHAR(10) + CHAR(9)) + ')'
PRINT @sql
Tuesday, November 27, 2018
Classic Mistake
https://dba.stackexchange.com/questions/218073/the-disappearing-act-of-the-invalid-length-parameter-passed-to-the-left-or-subs
Monday, September 10, 2018
DISTINCT with TOP n
DECLARE @x TABLE (gender char(1))
INSERT @x VALUES ('M')
INSERT @x VALUES ('M')
INSERT @x VALUES ('F')
INSERT @x VALUES ('F')
SELECT DISTINCT TOP 2 gender FROM @x ORDER BY gender
Monday, September 18, 2017
Clever workaround to the limitation of 900 byte index widths
Friday, June 23, 2017
View defined as SELECT * FROM Another View
CREATE VIEW V1 AS SELECT [object_id] FROM sys.objects
GO
CREATE VIEW V2 AS SELECT * FROM V1
GO
SELECT TOP 10 * FROM V2
GO

