Differences
This shows you the differences between two versions of the page.
Next revision | Previous revision | ||
qnd:sql_server [2021/12/28 14:44] – created mgupton | qnd:sql_server [2021/12/30 19:25] (current) – mgupton | ||
---|---|---|---|
Line 1: | Line 1: | ||
====== SQL Server ====== | ====== SQL Server ====== | ||
* [[qnd:SQL Server Security]] | * [[qnd:SQL Server Security]] | ||
+ | |||
+ | ====== Solutions ====== | ||
+ | * [[https:// | ||
+ | |||
+ | ====== Example Queries ====== | ||
+ | |||
+ | === Get Config Settings === | ||
+ | <code sql> | ||
+ | SELECT * | ||
+ | FROM sys.configurations | ||
+ | </ | ||
+ | |||
+ | === Select Into Variable === | ||
+ | <code sql> | ||
+ | USE master; | ||
+ | GO | ||
+ | |||
+ | declare @TblTmp table ([name] nvarchar(100), | ||
+ | |||
+ | insert into @TblTmp ([name], is_disabled) | ||
+ | select [name], is_disabled | ||
+ | FROM sys.sql_logins | ||
+ | WHERE principal_id = 1 | ||
+ | |||
+ | select * from @TblTmp | ||
+ | </ | ||
+ | |||
+ | === === | ||
+ | <code sql> | ||
+ | use [master] | ||
+ | go | ||
+ | |||
+ | declare @IsFinding bit | ||
+ | set @IsFinding = 0 | ||
+ | |||
+ | if exists (select [name] | ||
+ | from sys.databases | ||
+ | where [name] in (' | ||
+ | set @IsFinding = 1 | ||
+ | |||
+ | |||
+ | if @IsFinding = 1 | ||
+ | select @@SERVERNAME as [Server Name], ' | ||
+ | else | ||
+ | select @@SERVERNAME as [Server Name], ' | ||
+ | </ | ||
+ | |||
+ | === Execute Query Against Every Database === | ||
+ | * [[https:// | ||
+ | |||
+ | <code sql> | ||
+ | use [master] | ||
+ | |||
+ | go | ||
+ | |||
+ | if object_id(' | ||
+ | |||
+ | drop TABLE #database | ||
+ | |||
+ | go | ||
+ | |||
+ | create TABLE # | ||
+ | |||
+ | go | ||
+ | |||
+ | set nocount on | ||
+ | |||
+ | insert into # | ||
+ | |||
+ | select name | ||
+ | |||
+ | from sys.databases | ||
+ | |||
+ | where name like ' | ||
+ | |||
+ | and source_database_id is null | ||
+ | |||
+ | order by name | ||
+ | |||
+ | Select * | ||
+ | from #database | ||
+ | |||
+ | declare @id INT, @cnt INT, @sql NVARCHAR(max), | ||
+ | |||
+ | select @id = 1, @cnt = max(id) | ||
+ | from #database | ||
+ | |||
+ | while @id <= @cnt | ||
+ | |||
+ | BEGIN | ||
+ | |||
+ | select @currentDb = name | ||
+ | from #database | ||
+ | where id = @id | ||
+ | |||
+ | set @sql = ' | ||
+ | print @sql | ||
+ | exec (@sql); | ||
+ | print ' | ||
+ | set @id = @id + 1; | ||
+ | |||
+ | END | ||
+ | |||
+ | go | ||
+ | </ | ||