The following scripts will help you determine if you are a member of a group or role or create a list of group members in SQL Server without having to use SQL Server Management Studio. This is a particularly handy script in determining who might have access to the server through Active Directory groups.
The code below indicates whether the current user is a member
of the specified Microsoft Windows group or SQL Server database role.
A result of 1 = yes
,0 = no
,null = the group or role queried is not valid.
SELECT IS_MEMBER('[group or role]')
The code below will create a list of all the logins that are members
of a group.
@acctname = '[group]',
@option = 'members'
So you want to find out where the SQL Server Error Log file is located for a specific SQL Server instance?
The Error Log is a great place to start tracking down reasons why SQL Server might be experiencing problems.
The query below uses the extended stored procedure XP_READERRORLOG, which is typically used to read error logs directly, to return the location of the SQL Server Error Log files. The returned value is then cleaned up so only the directory location is provided. You can then copy and paste the result into your file explorer.
DECLARE @dir AS VARCHAR(MAX)
-- Command will create the temporary table in tempdb
CREATE TABLE [dbo].[#TmpErrorLog] (
[LogDate] DATETIME NULL
,[ProcessInfo] VARCHAR(20) NULL
,[Text] VARCHAR(MAX) NULL
-- Command will insert the errorlog data into temporary table
INSERT INTO #TmpErrorLog (
EXEC [master].[dbo].xp_readerrorlog 0
,N'Logging SQL Server messages in file'
-- retrieves the data from temporary table and writes it to a variable to remove everything but directory location
SET @dir = (
SELECT REPLACE(REPLACE(REPLACE([Text], 'Logging SQL Server messages in file ', ''), '''', ''), 'ERRORLOG', '')
SET @dir = LEFT(@dir, LEN(@dir) - 1)
DROP TABLE #TmpErrorLog
If you’ve been working towards a new deployment to a live database chances are you have written several scripts (possibly dozens) that have been developed/tested against the the development server.
Now the time has come to put the update live. Which would require executing each script against the live database.
This task can be automated by using a very handy batch script to run against the directory the files are saved in.
Caveat: This process does not take into account error handling or rollbacks, it’s just a simple example people can build on.
In order for this to work the files must have been named in a manner that the necessary order of execution corresponds to ascii sort order, i.e. 001_CreateTable.sql, 002_PopulateTable etc. This is standard practice for sql file naming conventions.
Simply create a .BAT file with the following command:
(Swap servername and databaseName for your required server and database names, TIP: SELECT @@servername can provide you with the full server name.)
for %%G in (*.sql) do sqlcmd /S servername /d databaseName -E -i"%%G"
Place this .BAT file in the directory from which you want the .SQL files to be executed, double click the .BAT file and the command will loop and execute every SQL script in the folder.
So here’s a quick out of the box solution for . . . you guessed it, dynamically pulling all text files from a folder into a SQL Server table. On it’s own this script isn’t very powerful but it can be paired with SQL queries to make some powerful functionality.
To use simply change the @path variable to the directory path you’re targeting and as it states in the comments . . .