Skip to main content

Basic & Otherwise Useful Queries

Select all tables with <x> column

SELECT
    c.name as 'ColumnName',
    t.name as 'TableName'
FROM
    sys.columns c
JOIN
    sys.tables t ON c.object_id = t.object_id
WHERE
    c.name LIKE '%DSActionTypeID%'
ORDER BY
    TableName, ColumnName

Update a value manually

UPDATE
	<table>
SET
	<column> = <value>
WHERE
    <condition>

I can't remember the context for this, I think I had to batch update multiple rows in a table when I was doing manual testing on a device that provided LED sign content at one of my early jobs and I just wanted to have this somewhere I could just copy-paste it from.

Update a value manually using joins

UPDATE
	<table>
SET
	<table>.<column> = <value>
FROM
	<table> as t1
JOIN
	<secondTable> t2 on t2.<column> = t1.<column>
WHERE
    <condition>

Same as the other "update a value" query, had to update multiple rows except across tables for different types of sign text that I needed to test. I sourced this from https://stackoverflow.com/questions/1604091/update-a-table-using-join-in-sql-server/1604212#1604212