Showing posts with label MSSQL Server. Show all posts
Showing posts with label MSSQL Server. Show all posts

08 October 2015

Search all tables in MSSQL for a specific value in all columns

I came across this little snippet which creates a stored procedure that can be used to query all the tables in MSSQL database to search for specific value. It may be useful to someone looking for a similar solution

 
CREATE PROC SearchAllTables
(
 @SearchStr nvarchar(100)
)
AS
BEGIN

    CREATE TABLE #Results (ColumnName nvarchar(370), ColumnValue nvarchar(3630))

    SET NOCOUNT ON

    DECLARE @TableName nvarchar(256), @ColumnName nvarchar(128), @SearchStr2 nvarchar(110)
    SET  @TableName = ''
    SET @SearchStr2 = QUOTENAME('%' + @SearchStr + '%','''')

    WHILE @TableName IS NOT NULL

    BEGIN
        SET @ColumnName = ''
        SET @TableName = 
        (
            SELECT MIN(QUOTENAME(TABLE_SCHEMA) + '.' + QUOTENAME(TABLE_NAME))
            FROM     INFORMATION_SCHEMA.TABLES
            WHERE         TABLE_TYPE = 'BASE TABLE'
                AND    QUOTENAME(TABLE_SCHEMA) + '.' + QUOTENAME(TABLE_NAME) > @TableName
                AND    OBJECTPROPERTY(
                        OBJECT_ID(
                            QUOTENAME(TABLE_SCHEMA) + '.' + QUOTENAME(TABLE_NAME)
                             ), 'IsMSShipped'
                               ) = 0
        )

        WHILE (@TableName IS NOT NULL) AND (@ColumnName IS NOT NULL)

        BEGIN
            SET @ColumnName =
            (
                SELECT MIN(QUOTENAME(COLUMN_NAME))
                FROM     INFORMATION_SCHEMA.COLUMNS
                WHERE         TABLE_SCHEMA    = PARSENAME(@TableName, 2)
                    AND    TABLE_NAME    = PARSENAME(@TableName, 1)
                    AND    DATA_TYPE IN ('char', 'varchar', 'nchar', 'nvarchar', 'int', 'decimal')
                    AND    QUOTENAME(COLUMN_NAME) > @ColumnName
            )

            IF @ColumnName IS NOT NULL

            BEGIN
                INSERT INTO #Results
                EXEC
                (
                    'SELECT ''' + @TableName + '.' + @ColumnName + ''', LEFT(' + @ColumnName + ', 3630) 
                    FROM ' + @TableName + 'WITH (NOLOCK) ' +
                    ' WHERE ' + @ColumnName + ' LIKE ' + @SearchStr2
                )
            END
        END    
    END

    SELECT ColumnName, ColumnValue FROM #Results
END

10 September 2015

Generic SQL Select Statement Executer in Java

In this post I will show you how to write a Java program that can allow you to run any SQL select statement against SQL Server and write output to csv file. While this is a simple task to use ResultSet and write output to file but imagine you need to keep on modifying output columns based on changes in business requirements or you need to output large number of SQL tables as csv files. In that case task gets little tedious and every time user request a new field to be added or removed you have to modify your class. This is where this trick comes handy.

You can download full source code from here : Generic SQL Select Statement Executer in Java

1. Lets create a new project in eclipse called SQLSelector and add a class file called GenericSelecor.java.

2. Next add sqljdbc4.jar and sqljdbc_auth.dll to your solution. This is required to connect to Microsoft SQL Server.

3. Add sqljdbc4.jar as a reference to your project by right clicking on project and going to properties and select java build path and libraries tab and click on Add Jar


4. Lets add two Properties file. One that is common for all SQL statements like database name,output folder etc and other one specific for current sql statement that will include Select statement, output file name and other specific information. I have Northwind database on my local machine and I want to select all customers from that database.

I have PROD.Properties file which contains database connection specific information.
Relative.Output.Folder=C\:\\SQLOutput\\
Server=localhost;instanceName=SQL2008
ODBC.DataSource=Northwind

I have Generic.Properties file which contains specific select statement and flag to indicate if string outputs should have double quotes
Statement=SELECT CustomerID ,CompanyName ,ContactName ,ContactTitle ,Address ,City ,Region ,PostalCode ,Country ,Phone ,Fax FROM Customers
OutputFileName = Customers.csv
StringOutputInDoubleQuote=Y

5. Now in GenericSelector class read those two properties files as arguments and read all the values in corresponding variables.

  
propertyFile = args[0];
statementFile= args[1];

Properties dpr = new Properties();
Properties spr = new Properties();

//Read all common variables from properties file
try 
{ 
 FileInputStream is = new FileInputStream(propertyFile);
 dpr.load(is);
 is.close();
   
 Server=dpr.getProperty("Server");
 DataSource=dpr.getProperty("ODBC.DataSource");
 
 OutputFolder=dpr.getProperty("Relative.Output.Folder");
 
}
catch(Exception e)
{
 e.printStackTrace();
 System.exit(1); 
}
// Read all specific variables from properties file
try 
{ 
 FileInputStream is = new FileInputStream(statementFile);
 spr.load(is);
 is.close();
 
 selectSatement =spr.getProperty("Statement"); // This is the select statement from external file
 outputFileName =spr.getProperty("OutputFileName");
 stringOutputInDoubleQuote = spr.getProperty("StringOutputInDoubleQuote").toUpperCase();
}
catch(Exception e)
{
 e.printStackTrace();
 System.exit(1);
 
}  


6.Load appropriate class driver and create connection to database. In this case I am using MSSQL server and connecting to Northwind database. Get a ResultSet by executing select statement. Get ResultSetMetaData from result set. This will help in identifying type of columns that are returned and name of those columns which then can be used to get the records from ResultSet without actually hard coding column names in application. ResultSetMetaData provides methods getColumnTypeName() and getColumnName() which are used to retrieve specific column information.
 
// Here we create a connection to database and run the query and then use metadata to generate csv output   
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
String sqlCmd = "";
con = DriverManager.getConnection("jdbc:sqlserver://"+Server+";databaseName="+DataSource+";integratedSecurity=true");

sqlCmd = selectSatement;
PreparedStatement stat = con.prepareStatement(sqlCmd);
ResultSet rs = stat.executeQuery(); // Actual Data
ResultSetMetaData rsmd = rs.getMetaData(); // Metadata from current result set.

int columnCount = rsmd.getColumnCount();

String[] columnTypes = new String[columnCount]; 
String[] columnNames = new String[columnCount];

StringBuilder sb = new  StringBuilder();
rows=0;
// The column count starts from 1
for (int i = 1; i < columnCount + 1; i++ ) 
{
 columnTypes[i-1] = rsmd.getColumnTypeName(i); // Get Column Type Names
 String name = rsmd.getColumnName(i);  // Get actual column names
 columnNames[i-1] = name;
 sb.append(name);
 sb.append( i==columnCount ? "\n" : ",");
}                       


7. Now Iterate through each records in ResultSet and for each record find value by using its column name and column type that was retrieved using ResultSetMetaData and write to output file. Here I have checked for type of the column so I can use specific get method. If you don't require any formatting then you can use getObject() method of result set without worrying about underlying record type.
 
String DATETIME_FORMAT_QRY = "dd/MM/yyyy";
java.text.SimpleDateFormat sdf2 = new java.text.SimpleDateFormat(DATETIME_FORMAT_QRY);
   
while(rs.next())
{
 
 rows++;
 // Based on column type call appropriate getXXX() method to get value from database
 // If you don't need specifc formatting based on data type you can use getObject() method instead of specific get method
 for (int i = 1; i < columnCount + 1; i++ ) 
 {
   // If output column type is string type then either output it in double quote or as is based on flag 
   if(columnTypes[i-1]== "nchar" || columnTypes[i-1]== "nvarchar")
    sb.append((rs.getString(columnNames[i-1]) == null ? "" : (stringOutputInDoubleQuote.equals("Y") ? "\""+ rs.getString(columnNames[i-1]).trim() + "\"" : rs.getString(columnNames[i-1]).trim())));
   else if(columnTypes[i-1]== "decimal")    
    sb.append(rs.getDouble(columnNames[i-1]));
   else if(columnTypes[i-1]== "int" || columnTypes[i-1]== "tinyint")   
    sb.append(rs.getInt(columnNames[i-1]));
   else if(columnTypes[i-1]== "datetime")   
    sb.append(rs.getDate(columnNames[i-1])==null ? "" : sdf2.format(rs.getDate(columnNames[i-1])));
   else  
    sb.append("ERROR IN INTERFACE : No check added for "+columnNames[i-1] );
  
  sb.append( i==columnCount ? "\n" : ",");
 }
 

 // Write to file
 if(rows>0)
 {
  if(rows==1) // Delete old file
  {
   File delfile = new File(OutputFolder+"/"+outputFileName);
   delfile.delete();
  }
  FileOutputStream outFile = new FileOutputStream(OutputFolder+"/"+outputFileName,true);
  outFile.write(sb.toString().getBytes());
  outFile.close();
  sb = new StringBuilder(); // Reset string builder for next iteration
  
 }
 
}                        


That is it!!! You have a fully functional java code that can create any table as csv output. All you need to do is change SQL in Generic.Properties file. Happy Coding !!!

14 May 2015

Copy Data between two different MSSQL Databases on different servers using C#

You can copy data from one SQL table to another using INSERT command with SELECT within same database or databases on same server but things gets little complicated when databases are on two different server. Here is a C# snipplet you can use to copy data between two desperate databases on two different servers. Code is self explanatory with comments.  Your source and destination table fields needs to match.


// Create source connection
SqlConnection source = new SqlConnection(ConfigurationManager.ConnectionStrings["SourceConnectionString"].ConnectionString);
// Create destination connection
SqlConnection destination = new SqlConnection(ConfigurationManager.ConnectionStrings["DestinationConnectionString"].ConnectionString);


// Open source and destination connections.
source.Open();
destination.Open();

// Select data from Products table
SqlCommand cmd = new SqlCommand(@"SELECT customer
         ,name
         ,address1
         ,address2
         ,address3
         ,address4
         ,address6
         ,address5
         ,fax
         ,territory
         ,region
         ,class
        FROM
        [dbo].[slcustm]", source);
// Execute reader
Console.WriteLine("Read data  from [dbo].[slcustm] table");
SqlDataReader reader = cmd.ExecuteReader();

Console.WriteLine("Write data  to DestinationCustomers table");
// Create SqlBulkCopy

SqlBulkCopy bulkData = new SqlBulkCopy(destination);
//If you are copying larger amount of data don't forget to set the timeout flag. Default value is 30 seconds. 0 = No limit
bulkData.BulkCopyTimeout = 0;

// Set destination table name
bulkData.DestinationTableName = "DestinationCustomers";
// Write data
bulkData.WriteToServer(reader);
// Close objects
bulkData.Close();
destination.Close();