T-SQL – Search Stored Procedure Code for Text

Often I have to search for stored procedures in SQL Server to find pieces of text.   The motivation for this is to find procedures which use a table or to look for code which the developer has done silly things like hardcoded directories (Boris….).  Luckly you can query sys.procedures to generally find the information that you need.  Below is an example of query that searches for stored procedures for the word ‘c:\*’.

REFERENCES:

Technet – sys.procedures

C# Methods with Unlimited Parameters

To create a method that can take unlimited parameters, utilize the params keyword in the method declaration.  This allows you to specify a method parameter that take an argument where the number of arguments is variable.   You are not allowed to have any other method parameters declared after a params method parameter.

MSDN resource: params

Example:

PSGMUMGGBJAR

How to determine the version and edition of SQL Server

Connect to the instance of SQL Server, and then run the following query (this will work for all versions of SQL Server):

The result form the query will look like the following.

Microsoft SQL Server 2008 (SP1) – 10.0.2531.0 (X64)   Mar 29 2009 10:11:52   Copyright (c) 1988-2008 Microsoft Corporation  Express Edition (64-bit) on Windows NT 6.1 <X64> (Build 7600: )

For version of SQL Server 2005 and newer you also can use the SERVERPROPERTY function to retrieve additional information.  Executing the following will provide you with the SQL Server product version (i.e. 10.0.2531.0, product level (RTM) and edition information (i.e. Standard, Enterprise):

You can lookup the product version using the following table:

Release Product Version
SQL Server 2012 Service Pack 1 11.00.3000.00
SQL Server 2012 RTM 11.00.2100.60
SQL Server 2008 R2 Service Pack 1 10.50.2500.0
SQL Server 2008 R2 RTM 10.50.1600.1
SQL Server 2008 Service Pack 3 10.00.5500.00
SQL Server 2008 Service Pack 2 10.00.4000.00
SQL Server 2008 Service Pack 1 10.00.2531.00
SQL Server 2008 RTM 10.00.1600.22
SQL Server 2005 Service Pack 4 9.00.5000.00
SQL Server 2005 Service Pack 3 9.00.4035
SQL Server 2005 Service Pack 2 9.00.3042
SQL Server 2005 Service Pack 1 9.00.2047
SQL Server 2005 RTM 9.00.1399
SQL Server 2000 Service Pack 4 8.00.2039
SQL Server 2000 Service Pack 3 8.00.760
SQL Server 2000 Service Pack 2 8.00.534
SQL Server 2000 Service Pack 1 8.00.384
SQL Server 2000 RTM 8.00.194

 

Where to find information about the latest SQL Server builds use the following link: http://support.microsoft.com/default.aspx?scid=kb;EN-US;957826

How to get the column names of a table in SQL Server (T-SQL)

The following query can be run (substitute the ‘TableName’ in the query with the table name you want column information on) to get the column names for any table in T-SQL.

To get information on all the columns for all tables in a database use the following query:

SQL Server also provides a stored procedure (‘sp_help’) which is able to display information on any object listed in sysobjects.   To use this to get information on a table execute the following:

Enable xp_cmdshell using sp_configure

When you install a SQL Server instance, any feature that is not necessary for the core engine to run has been disabled by default. xp_cmdshell is a significant security risk because it allows a compromised SQL Server to elevate the attack to the operating system itself, and from there to the entire network.

You can enable or disable features within your instance by executing the system stored procedure sp_configure with the xp_cmdshell option.

When the xp_cmdshell feature is disabled you will see the following message when it is executed:

Msg 15281, Level 16, State 1, Procedure xp_cmdshell, Line 1

SQL Server blocked access to procedure ‘sys.xp_cmdshell’ of component ‘xp_cmdshell’ because this component is turned off as part of the security configuration for this server. A system administrator can enable the use of ‘xp_cmdshell’ by using sp_configure. For more information about enabling ‘xp_cmdshell’, see “Surface Area Configuration” in SQL Server Books Online.

You can verify that the xp_cmdshell feature featured is disabled by executing the following query:

If the results of the query is 0 then the feature is disabled; if 1 then it is enabled.

In order to enable xp_cmdshell execute the following:

%d bloggers like this: