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
Showing posts with label SQL. Show all posts
Showing posts with label SQL. 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
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();
Labels:
.NET,
Bulk Copy Data,
C#,
MSSQL Server,
SQL
Subscribe to:
Posts (Atom)