Thursday, July 17, 2014

Scripting the display of dependencies for an object for SQL Server 2008 R2

Scripting the display of dependencies for an object for SQL Server 2008 R2 is hard to know how to do. I've done a little research, and found that there is no one way to do it, so the best way to do it is to load the different ways into a temp table and display them that way.

If you want to see how Microsoft does it in SQL Server Management Studio (SSMS):
mssqltips.com/tipimages/1294_sqlcode.txt


DECLARE @TableName nvarchar(50)
DECLARE @TableNameSchema nvarchar(50)
DECLARE @TableNameDb nvarchar(50)
SET @TableName = N'Product'
SET @TableNameSchema = N'dbo.' + @TableName
SET @TableNameDb = N'DbNameHere.dbo.' + @TableName

CREATE TABLE #dependents (obj_name nvarchar(255), obj_type nvarchar(255), obj_source nvarchar(30))
CREATE TABLE #msdependencies (oType nvarchar(255), oObjName nvarchar(255), oOwner nvarchar(255), oSequence nvarchar(255))

INSERT INTO #msdependencies (oType, oObjName, oOwner, oSequence)
    EXEC sp_MSdependencies @TableNameSchema, null, 1315327

INSERT INTO #dependents (obj_name, obj_type, obj_source)
    SELECT
        oOwner + '.' + oObjName AS obj_name,
        CASE oType
            WHEN 1 THEN 'function'
            WHEN 4 THEN 'view'
            WHEN 8 THEN 'table'
            WHEN 16 THEN 'stored procedure'
            ELSE '(other - ' + CAST(oType as varchar) + ')'
            END AS obj_type,
        'sp_MSDependencies' AS obj_source
    FROM #msdependencies

DROP TABLE #msdependencies

INSERT INTO #dependents (obj_name, obj_type, obj_source)
    SELECT DISTINCT
        OBJECT_SCHEMA_NAME(so.id) + '.' + so.name,
        CASE so.[type]
            WHEN 'TF' THEN 'table function'
            WHEN 'IF' THEN 'inline function'
            WHEN 'FN' THEN 'scalar function'
            WHEN 'V' THEN 'view'
            WHEN 'P' THEN 'stored procedure'
            ELSE '(other - ' + so.[type] + ')'
            END AS obj_type,
        'sysComments'
    FROM syscomments sc
    INNER JOIN sysobjects so ON sc.id = so.id
    WHERE charindex(@TableName, text) > 0

INSERT INTO #dependents (obj_name, obj_type, obj_source)
    SELECT DISTINCT
        SPECIFIC_SCHEMA + '.' + SPECIFIC_NAME,
        CASE ROUTINE_TYPE
            WHEN 'FUNCTION' THEN 'function'
            WHEN 'PROCEDURE' THEN 'stored procedure'
            ELSE '(other - ' + ROUTINE_TYPE + ')'
            END AS obj_type,
        'information_schema.routines'
    FROM information_schema.routines ISR
    WHERE CHARINDEX(@TableNameSchema, ISR.ROUTINE_DEFINITION) > 0

INSERT INTO #dependents (obj_name, obj_type)
    EXEC sp_depends @objname = @TableNameDb
UPDATE #dependents SET obj_source = 'sp_depends' WHERE obj_source IS NULL

-- Also, less useful, but could be added:
-- SELECT * FROM sys.dm_sql_referencing_entities (N'dbo.Product', N'OBJECT')
-- SELECT * FROM sys.dm_sql_referenced_entities (N'dbo.Product', N'OBJECT')

--SELECT DISTINCT obj_name, obj_type, obj_source FROM #dependents ORDER BY obj_name, obj_type, obj_source
SELECT
    obj_name,
    CASE WHEN MIN(obj_type) != 'function' THEN MIN(obj_type)
         ELSE MAX(obj_type) END AS obj_type,
    CASE obj_type
        WHEN 'inline function' THEN 'function'
        WHEN 'scalar function' THEN 'function'
        WHEN 'table function' THEN 'function'
        ELSE obj_type
        END AS obj_category
FROM #dependents
GROUP BY
    obj_name,
    CASE obj_type
        WHEN 'inline function' THEN 'function'
        WHEN 'scalar function' THEN 'function'
        WHEN 'table function' THEN 'function'
        ELSE obj_type END
ORDER BY obj_category, obj_name

DROP TABLE #dependents


Thanks to Greg Robidoux:
mssqltips.com/sqlservertip/1294/listing-sql-server-object-dependencies/

Thanks to Pinal Dave:
blog.sqlauthority.com/2010/02/04/sql-server-get-the-list-of-object-dependencies-sp_depends-and-information_schema-routines-and-sys-dm_sql_referencing_entities/

Wednesday, July 16, 2014

How to use select2 to create a multiple item selector

How to use select2 to create a multiple item selector

<link href="//cdnjs.cloudflare.com/ajax/libs/select2/3.5.0/select2.css" rel="stylesheet" type="text/css" />
<script src="//cdnjs.cloudflare.com/ajax/libs/select2/3.5.0/select2.js" type="text/javascript"></script> 
 
<input type="hidden" id="e2"/>
<input type="button" value="Go" id="btnGO"/> 
 
<script type="text/javascript">
    container.Content.find("#e2").select2({
        createSearchChoice: function (term, data) {
            if ($(data).filter(function () {
                                return this.text.localeCompare(term) === 0;
                            }).length === 0) {
                return {
                    id: term,
                    text: term
                };
            }
        },
        multiple: true,
        width: '400px',
        data: [
            {id: "AL", text: "Alabama"},
            {id: "AK", text: "Alaska"},
            {id: "CA", text: "California"}
        ]
    });
 
    container.Content.find("#btnGO").click(function () {
        console.log("Val=" + container.Content.find('#e2').val());
    });
</script>
 

Tuesday, July 08, 2014

Adding TortoiseSVN commands to Visual Studio 2010

How to integrate TortoiseSVN (Subversion) into Visual Studio 2010

Tools --> External Tools... --> Add button

Title: Subversion Diff
Command: C:\Program Files\TortoiseSVN\bin\TortoiseProc.exe
Arguments: /command:diff /path:"$(ItemPath)"

Others I added:
Subversion Check for Modifications
Arguments: /command:repostatus /path:"$(SolutionDir)" /notempfile

Subversion Update
/command:update /path:"$(SolutionDir)" /notempfile

Subversion Show Log
/command:log /path:"$(SolutionDir)" /notempfile

Subversion Revert
/command:log /path:"$(ItemDir)" /notempfile

Adding these external tools should make them show in your tools menu. You may need to add them to the menu (using Tools --> Customize) if you have deleted other Add-Ons.




Additionally, you can add items to the context menus in the Solution Explorer or to the tabs of files:
















You can do this by adding the items using the Tools --> Customize. You can add to the context menu of files using "Item" and add to the context menu of folders using "Folder":



This site really helped me do this:
http://tortoisesvn.net/visualstudio.html

This site also helped:
http://3dmpengines.tistory.com/652