# Wednesday, August 04, 2010
Tip of the Day: T-SQL Script to rename bulk columns
Wednesday, August 04, 2010 5:22:15 AM (GMT Daylight Time, UTC+01:00)  #    Disclaimer  |  Comments [0]  | 
# Thursday, March 11, 2010
Explore the INFORMATION_SCHEMA to get more useful metadata information on your database, much simpler than using sysobjects!
Thursday, March 11, 2010 4:02:00 AM (GMT Standard Time, UTC+00:00)  #    Disclaimer  |  Comments [0]  | 
# Friday, March 05, 2010
Script to ferret out those procs you forgot to create permissions for!
Friday, March 05, 2010 4:36:29 AM (GMT Standard Time, UTC+00:00)  #    Disclaimer  |  Comments [0]  | 
# Wednesday, November 11, 2009
Adding carriage returns to SSIS package annotations
Wednesday, November 11, 2009 5:13:49 PM (GMT Standard Time, UTC+00:00)  #    Disclaimer  |  Comments [0]  | 
# Wednesday, September 02, 2009

I can't take credit for this, as I took it from the article How To Get Table Row Counts Quickly And Painlessly on SqlServerCentral.com by Kendal Van Dyke. However, it such a good tip, I had to change it a bit, as I tend to use this a LOT.

First, I made the results a little more "printable" but reducing the table name column. Next, I made it a system proc because when I go onto client sites, I am looking at many databases. I probably don't need to create a procedure for it, but there have been many cases where I trend things, and it would be nice to have this handy. Finally, I added a parameter to allow me to change the sorting. By default, it sorts by table name. I added an optional parameter to allow me to pass in an integer (could have done it a million ways, I know, but I chose to do this solely for my preferences -- feel free to change to yours).

Usagesp_retrieveRowCounts    --- sorts by table name

            sp_retrieveRowCounts 1  ---sorts by row count, then table name

Here's the proc:


USE master
GO

IF (SELECT OBJECT_ID('sp_retrieveRowCounts','P')) IS NOT NULL
BEGIN
    DROP PROC sp_retrieveRowCounts
END
GO


CREATE PROC sp_retrieveRowCounts (@OrderByCount int = null)
AS
BEGIN

    DECLARE @OrderByParm int
    DECLARE @SQLStatement varchar(700)
    DECLARE @OrderByClause varchar(35)
    
    SELECT @OrderByParm = ISNULL(@OrderByCount,0)

    IF (@OrderByParm != 0)
        SELECT @OrderByClause = ' ddps.row_count DESC, o.NAME ASC'
    ELSE
        SELECT @OrderByClause = ' o.NAME ASC'

    SELECT @SQLStatement = 'SELECT TableName = LEFT(o.name,50), TotalRows = ddps.row_count FROM sys.indexes AS i
     INNER JOIN sys.objects AS o ON i.OBJECT_ID = o.OBJECT_ID
     INNER JOIN sys.dm_db_partition_stats AS ddps ON i.OBJECT_ID = ddps.OBJECT_ID
     AND i.index_id = ddps.index_id
    WHERE i.index_id < 2
     AND o.is_ms_shipped = 0
    ORDER BY '
+ @OrderByClause
    
    EXEC(@SQLStatement)

END

--Mark procedure as system object
EXEC sys.sp_MS_marksystemobject sp_retrieveRowCounts
GO


GRANT EXEC ON sp_retrieveRowCounts TO public
GO

 

Wednesday, September 02, 2009 6:34:10 PM (GMT Daylight Time, UTC+01:00)  #    Disclaimer  |  Comments [0]  | 
# Tuesday, September 01, 2009

When I go onto a client site, I usually don't have the luxury of designing everything from scratch. There are times (like on my current engagement) if I were given the choice, I'd say scrap it, but usually it doesn't happen that way. It is what it is... been saying that a lot lately!

So one of the tools I find useful is the ability to search for particular field names in the database. This is very similar to sp_findsp I use, which I have posted in the past off the net, but I have modified it so it gets registered as a system procedure.

So here's sp_findfields. It will return a fairly printable representation of the results of all fields which match the passed in string. It's registered as a system procedure in this script, so it is accessible to all databases.

Usage:

sp_findfields '%Contact%'

This will find all fields with the string 'Item' in it. Note you need to provide the wildcard characters.

USE master
GO

IF (SELECT OBJECT_ID('sp_findfields','P')) IS NOT NULL
    BEGIN
        DROP PROCEDURE sp_findfields
    END
GO

CREATE PROCEDURE sp_findfields
(@searchText varchar(50))
AS
BEGIN
    
    SELECT SchemaName = LEFT(TABLE_SCHEMA,10),
        TableName = LEFT(TABLE_NAME, 35),
        FieldName = LEFT(COLUMN_NAME, 40),
        DataType = LEFT(DATA_TYPE, 13),
        DataSize = CHARACTER_MAXIMUM_LENGTH,
        AlloysNulls = IS_NULLABLE
    FROM INFORMATION_SCHEMA.columns
    WHERE column_name like @searchText
    ORDER BY TableName, FieldName
END
GO

--Mark procedure as system object
EXEC sys.sp_MS_marksystemobject sp_findfields
GO


GRANT EXEC ON sp_findfields TO public
GO

Tuesday, September 01, 2009 5:10:15 AM (GMT Daylight Time, UTC+01:00)  #    Disclaimer  |  Comments [0]  | 
# Tuesday, May 16, 2006
Get the name of the current db in t-sql
Tuesday, May 16, 2006 4:38:30 PM (GMT Daylight Time, UTC+01:00)  #    Disclaimer  |  Comments [1]  | 
# Friday, April 28, 2006
A new blog...
Friday, April 28, 2006 4:38:06 PM (GMT Daylight Time, UTC+01:00)  #    Disclaimer  |  Comments [0]  | 
# Tuesday, September 20, 2005
"Internal Query Processor Error: The query processor could not produce a query plan. Contact your primary support provider for more information" when passing in C# DateTime value...
Tuesday, September 20, 2005 11:50:34 PM (GMT Daylight Time, UTC+01:00)  #    Disclaimer  |  Comments [0]  | 
# Friday, May 27, 2005
Need a good developer?
Friday, May 27, 2005 12:33:35 AM (GMT Daylight Time, UTC+01:00)  #    Disclaimer  |  Comments [2]  | 
# Friday, March 11, 2005

Someone asked me today if I knew that you couldn't use GETDATE() in functions in SQL Server. Well, I did, so I dug out this solution I had found a while back, and decided to post it, since I am sure others have come across the same issue.

Basically, you use a view to do the work for you... here you go!

CREATE VIEW Function_Assist_GETDATE

/********************************************************
*
* A view to return one row, with one column, the current
* date/time from the built-in function GETDATE().  This
* view allows a UDF to bypass the restriction on access to
* the non-deterministic getdate() function.
*
* Attribution: Based on a newsgroup posting in by Mikhail
*   Berlyant in microsoft.public.sqlserver.programming
*
* Common Usage:
DECLARE @dtVar datetime
select @dtVAr = [GetDate] from Function_Assist_GETDATE
**********************************************************/

AS
    SELECT getdate() as [GetDate]

GO

GRANT SELECT on Function_Assist_GETDATE to PUBLIC
GO

Friday, March 11, 2005 9:45:00 PM (GMT Standard Time, UTC+00:00)  #    Disclaimer  |  Comments [0]  | 
# Friday, December 31, 2004
Clearing up the confusion when you need the return value from an insert.
Friday, December 31, 2004 3:25:39 PM (GMT Standard Time, UTC+00:00)  #    Disclaimer  |  Comments [1]  | 
# Thursday, December 30, 2004
Finding all tables where a field exists in SQL Server
Thursday, December 30, 2004 6:09:49 PM (GMT Standard Time, UTC+00:00)  #    Disclaimer  |  Comments [2]  | 
# Saturday, December 11, 2004
A costly mistake to forget...
Saturday, December 11, 2004 6:29:51 PM (GMT Standard Time, UTC+00:00)  #    Disclaimer  |  Comments [1]  | 
# Saturday, November 06, 2004
When you can't create a SQL diagram, use this to print usable field information
Saturday, November 06, 2004 4:45:22 PM (GMT Standard Time, UTC+00:00)  #    Disclaimer  |  Comments [1]  |