Archive

Archive for the ‘Database’ Category

SQL Server Database Iterator

February 14, 2012 Leave a comment

 

I had an issue the other day where I needed to inspect each database on a server.  I wrote this little snippet to do so. It gets a list of databases from the server (minus the first 4 system databases), then inspects them to see if a table exists, then queries that table if it exists. 

 

Feel free to use it / modify it however you wish.  If you have better ways of doing it – post them too.  Smile

SET NOCOUNT ON
GO
use master
go

Declare 
@dbname nvarchar(500),
@variable1 int,
@variable2 int,
@variable3 int,
@totaldb int = 0,
@totaldbonserver int = 0,
@totaldbwithmatches int = 0

-- Get non system databases
Declare mycursor CURSOR for select name, database_id from SYS.databases where database_id > 4 order by name desc

open mycursor

fetch next from mycursor into @dbname, @variable1

while (@@FETCH_STATUS <> -1)
    BEGIN        
        DECLARE @ParmDefinition NVARCHAR(500)
        Declare @mysql nvarchar(500) = 'select @variable2OUT = COUNT(*) from [' + @dbname + '].INFORMATION_SCHEMA.TABLES where Upper(TABLE_NAME) like ''MyTable''';
        SET @ParmDefinition = N'@variable2OUT int OUTPUT'
        set @totaldbonserver = @totaldbonserver + 1
        Execute sp_executesql @mysql, @ParmDefinition, @variable2 OUTPUT            
        
        if @variable2 = 1
        BEGIN
            DECLARE @ParmDefinition2 NVARCHAR(500)
            Declare @mysql2 nvarchar(500) = 'select @variable2OUT = COUNT(*) from [' + @dbname + '].dbo.MyTable';
            SET @ParmDefinition2 = N'@variable2OUT int OUTPUT'
            Execute sp_executesql @mysql2, @ParmDefinition2, @variable3 OUTPUT                
            set @totaldb = @totaldb + 1
            
            if @variable3 > 1
            BEGIN
                Print @dbname + ' matched the criteria'            
                set @totaldbwithmatches = @totaldbwithmatches + 1
            END
            ELSE
            Select 1    
        END    
        
        fetch next from mycursor into @dbname, @variable1
    END
    PRINT 'Total databases on server: '
    Print @totaldbonserver
    
    PRINT 'Total databases tested () : '
    Print @totaldb
    
    PRINT 'Total databases with matches: '
    Print @totaldbwithmatches
CLOSE mycursor
DEALLOCATE mycursor

How to setup a SQL Failover Cluster with VMware WorkStation

April 22, 2011 Leave a comment

I wanted to setup a SQL Cluster environment with VMware workstation just to play with it and check out some of the features.   I don’t have a SAN (SCSI) setup at home so I need to virtualize that.  What I do have though is a 6 core 16 GB Ram work station that is fully capable of running this so here we go…..

Minimum Requirements

Host Machine

  • Processor – 2 – 4 Cores
  • Memory  – 8 GB’s

Client Machines

Domain Controller  (Required for VSAN and Clustering)

  • 1 –2 Cores
  • 1 GB Ram

Virtual SAN (iSCSI)

  • 1 –2 Cores
  • 1 GB Ram
  • 200-500 GB Virtual Hard Disk
    SQL Servers (2)
  • 2 –4 Cores
  • 2 GB Ram
    Setup Operating Systems (Virtual Machines)

See my post here for details on how to setup a base virtual machine to make this process easier. 

1.  Setup a domain controller and create a domain (see here for tips on setting up the domain controller)

- create a domain level admin user account for usage later. Example – sqladmin

2.  If a Virtual SAN is required – see this post for detailed steps on setting this up.

3.  Create 1st SQL Virtual Machine

  • Create Linked Clone
  • Add a second host only network adapter
  • Rename machine to SQL1
  • Join to Domain
  • Enable the Failover Clustering Feature (Administrative Tools – Server Manager – Features – Add Feature – Failover Clustering)
  • Install the iSCSI client software but do not create the drives yet – details
    4.  Create 2nd SQL Virtual Machine using the above steps
    At this point all machines should be up and running.
      Setup the SAN Disks
      Reference this post for details on the steps below

    1.  3 Drives must be created on the iSCSI target (order is critical)

    • 5 GB drive – label Quorum
    • 10 GB drive – label MSDTC
    • 100 + GB Drive – label Data

    2.  Connect each of the drives to the SQL1 Machine and format NTFS

    3.  Connect each of the drives to the SQL 2 Machine (no formatting required)

    The disks should now be available for usage in the cluster

     

    Setup the Cluster

    We want to test the configuration before proceeding so we can fix any changes ahead of time. 

    Validate the cluster

    1.  Launch the cluster manager (Administrative Tools – Failover Cluster Manager)

    2.  Click on Validate a Configuration

    3.  Click next  – then enter the dns names of the two servers in the Select Servers Wizard

    4.  Once selected – run all of the tests and ensure they all pass.  If not – you will need to address these before continuing.

    5.  Only after validating the configuration should you continue. 

    Setup

    1.  Select the two servers for the cluster

    2.  Provide an access point for the cluster (Name that it can be accessed by)

    3.  Complete the wizard.  Once the cluster is created – move to the next step.

    4.  At this point the two nodes should appear in the cluster manager with a status of Up.  Under the Storage section you should see all 3 drives.  The first drive should by under the Quorum section with the other two drives not being used.  If they are not online – right click and select "bring this resource online”

    5.  Go to Services and Applications – right click and select “Configure a service or application”

    6.  Select the DTC and click next

    image

    7.  Give it a name, and then select the 2nd disk we created for storage.  Complete the wizard. 

    At this point we are ready to install SQL Server

    Setup SQL Server

    SQL 1

    1.  Put in your SQL Server 2008 R2 disk or mount the ISO file.

    2.  Launch the installation and go to the SQL Server Installation Center.

    3.  Select Installation – New SQL Server failover cluster installation

    image

    4.  Ensure all of the prerequisites pass. 

    5. Walk through the wizard

    • Select DB Features

    image

    • Set your instance configuration and your public Cluster Name

    image

    • Select your cluster disk

    image

    • Under Service Accounts – use the Domain user you created above
    • Complete the rest of the configuration based on your preferences
      Once the installation is complete – you can now connect to SQL server.  Time to setup the other half. 

    SQL 2

    1.  Put in your SQL Server 2008 R2 disk or mount the ISO file.

    2.  Launch the installation and go to the SQL Server Installation Center.

    3.  Select Installation – Add note to a SQL Server failover cluster

    image

    4.  Walk through the steps and select the cluster to attach to from the SQL 1 setup.

    5.  Once installation is complete – you now have a failover ready. 

    SQL Server should now show up under the Services and Applications section in the Failover Cluster Manager. 

    image

    Creating View in Access fails with “Unions not allowed in a subquery” error.

    February 23, 2011 Leave a comment

    I was attempting to create a view in Access with a long complicated query with unions.

    Something like below ( the query in question was complicated and could not be done with a single query – this is just an example.)

    select a.1, a.2  from some table a.1

    inner join another table a.2 …

    inner join another table …

    where a.1 <> 4

    union

    select a.1, a.2  from some table a.1

    inner join another table a.2 …

    inner join another table …

    where a.2 <> 3

     

    When trying to create this in access as below it would fail.

    Create view myView as

    [Long union queries go here]

     

    To get around this use the following

    Create view myView as

    select * from

    (

         [Long union queries go here]

    )

    Categories: Access, Database Tags: , , ,

    Oracle 11g2 does NOT export empty tables when using exp.exe

    February 10, 2011 3 comments

    Out of habit I have been using the older data import / export functions in lieu of using the Data Pump commands that Oracle recommends.  As of 11g2 I can no longer do this.  The exp.exe no longer exports tables that do not have an entry in DBA_SEGMENTS.  This is due to a new feature named “deferred segment creation”. 

    The syntax for the new Data Pump commands are different and require some additional setup / maneuvering on the Oracle Server itself to complete.  The files when importing now must be placed in the Data Pump Directory.  This can be found by running the following query against the database server.

    select owner, directory_name, directory_path from dba_directories

    Note that a custom directory can also be created but is not required.  An example for this is done can be found here.

    http://wiki.oracle.com/page/Data+Pump+Export+%28expdp%29+and+Data+Pump+Import%28impdp%29

    Old Functions

    imp.exe = Import a file

    imp system/password@instance  file="D:\filenameandpath.dmp" fromuser=originaluser  TOUSER = targetuser

    exp.exe = Export a file

    exp username/password@instance file=”D:\exportfilename.dmp” Log=”logfilename”

    New Functions

    impdp.exe = Import a file

    impdp system/password@instance DUMPFILE=”filename.dmp” remap_schema=ORIGNALUSER:NEWUSER LOGFILE=”LogFileName.log”

    expdp.exe = Export a file

    expdp system/password@instance DIRECTORY=DATA_PUMP_DIR DUMPFILE=myfilename.dmp SCHEMAS=userexportingfrom

    Oracle Database Management Tool – New and Improved

    February 23, 2009 3 comments

    FINISHED!!!!

    Over the past couple of months I have been working on an Oracle Management tool to help manage Oracle Database Servers in a Windows environment. The tool morphed into a decent sized application with over 10,000 executable lines of code. It is written entirely in C# utilizing the ADO.NET classes for data access.

    Upon opening the application a login screen is presented. The database connections are populated automatically from the tnsnames file being used by Oracle.

    The application has the concept of roles which are determined based on what system privileges a user has on the Oracle Database server. These roles determine what features are available in the management tool.

    Below are the features of the application:

    Query Editor

    This area lets a user execute standard SQL queries against any user with parameters. The results come back as an output window (number of records affect, returned, updated, etc) and select statements return a data grid.


    The ability to add parameters is also available. Parameters are detected automatically using the : or @ designated syntax.

    Create User

    Here one can create a user very quickly by typing in the user’s name, password, selecting a tablespace and give the user roles. The radio buttons on the left are the most common used in our environment and automatically select the related oracle system privileges. The privileges are loaded from the oracle database dynamically. There is validation for the username and password (existing, length, character usage).

    Edit User

    Edit user is similar to create user. Here one can change a user’s password, default tablespace, roles, and view the current roles / privileges assigned to a user.

    Delete User

    Delete user allows one to remove one or more users at a time. Due to the cascading options with oracle users this can be time consuming so the ability to select – click delete – and leave to complete is extremely useful as a time saver.

    Import Dump File

    Importing dump files for Oracle can be time consuming and painful. There are many requirements to importing a dump file and syntax rules for command line options that must be followed. To make this process easy now one must create a user via the Create User feature, browse to a dump file and click import.

    This feature is only available when the application is run directly on the Oracle Server as it requires the Import Utility provided from oracle (imp.exe).

    There are certain checks in place to ensure this will work without errors occurring. There is also a log file created. It is scanned after the import has completed and prompts a user to open it if errors are detected. It will not let you import if there are tablespace conflicts and will notify you of what needs to be changed.

    Export Dump File

    Exporting a dump file also must be run directly on the database server. This option exports the dump file with standard options enabled and then opens up the file location as to where the dump file / log file were created.

    TableSpace Management

    The tablespace management section lets you create tablespaces, view details about the tablespaces, and add datafiles to the tablespaces.

    Create Tablespace

    This feature makes creating tablespaces a breeze. This isn’t a widely used feature but will be helpful for development groups. Viewing tablespace data and adding datafiles also make tablespace management simple but effective. Note that all naming for data files is automatic and conforms to oracle standards.

    System Information


    System information provides a basic snapshot of the host operating system as well as the database server OS environmetn information. The parameters sections (Get Buttons) return parameters from the Oracle Database server.

    Help

    There is a help file in .mht format that describes each feature in detail as well as provide common Oracle best practices, troubleshooting methodology, and Oracle Database logical design.

    Retrospective

    This was my first major development project. While I bounced some conceptual ideas off some of my colleagues I did not seek any assistance in writing the code. This was a great learning experience for me. I am now extremely comfortable using C# / Winforms / ADO.NET. I gave a demo of my product to a development team (a group of highly experienced developers). They were all pleased with the tool and provided great feedback for me to use moving forward. The hardest part for me during the development of this tool was to find the time to do it. I started the tool on November 22nd and finished it January 28th. Not too bad considering I took two weeks off in December.

    My next project:

    Create a new tool the facilitates an automatic daily creation of multiple databases across multiple database platforms for testing and development purposes. I already wrote one of these programs a few months ago but it needs additional features. After looking at the code I wrote then – I realized it would be better to start fresh and create an application worthy of upgrading and progressing. This tool is about 30 percent complete and has the following major sections:

    • Customized data model (database) created using Visual Studio Database Edition (Data Dude) with 16 separate tables all with strict foreign and unique key constraints
    • UI Management tool for setting up database creation plans
    • Console Application to execute the database creation plans
    • Unit Tests (of course we can’t forget these!!!!)

    Once finished – I will post a snapshot of the UI and a retrospective of this process.

    Categories: ADO.net, C#, Database, Oracle
    Follow

    Get every new post delivered to your Inbox.