Skip to main content

Posts

How to Automatically Create SQL Server Views from MySQL Tables Using OPENQUERY (An alternative to ETL)

If you have a linked server from SQL Server to MySQL, you can automate importing data and creating views using dynamic SQL. This is useful when integrating external MySQL data into a Microsoft SQL Server reporting or analytics environment. 🔗 Setup: Linked Server to MySQL Make sure you have already set up your MySQL linked server in SQL Server (for example, named SB ), and that you can run queries like the following: SELECT * FROM OPENQUERY(SB, 'SELECT * FROM your_table'); ⚙️ Goal We want to dynamically create SQL Server views for all base tables in a MySQL database, using a format like: CREATE VIEW [dbo].[lnk_table_name] AS SELECT * FROM OPENQUERY(SB, 'SELECT * FROM table_name WHERE deleted_at IS NULL'); But not all MySQL tables have a deleted_at column. So, we will check whether the column exists before appending the WHERE clause. 🧠 Full SQL Script This SQL Server script loops through all MySQL tables and generates the appropriate view creation stat...
Recent posts

Converting SQL Server Binary Image Data to HTML Image URL

  When working with images stored as binary data in SQL Server, you might need to display them in a web application. A convenient way to do this is by converting the binary data into a Base64-encoded string and using it as a data URL in HTML. Best suited for small thumbnail photos or icons. Storing Images in SQL Server Images are often stored in SQL Server as VARBINARY(MAX) columns. For example, a table structure for storing user photos might look like this: CREATE TABLE UserPhotos ( UserID INT PRIMARY KEY, PhotoThumbnail VARBINARY(MAX) ); Converting the Binary Data to a Base64 Image URL SQL Server doesn’t provide built-in functions to encode binary data as Base64, but we can achieve this using FOR XML PATH combined with BINARY BASE64 . Below is a query to transform the binary image data into a Base64 string formatted as a data URL: SELECT CASE WHEN p.PhotoThumbnail IS NOT NULL THEN 'data:image/png;base64,' + REPLACE( RE...

How to Compare Table and View Structures to Avoid Insert Errors in SQL

  When working with databases, especially in complex systems, you’ll often need to insert data from one object, such as a view, into another, like a table. However, if the structures of these two objects are not perfectly aligned—due to differences in data types, column lengths, or precision—you might run into various issues such as data truncation, conversion errors, or even complete failures of your SQL queries. In this blog post, I’ll show you a handy SQL query that compares the column definitions between a table and a view, highlighting any mismatches that could potentially cause insert errors. Why Do We Need to Compare Table and View Structures? Imagine you have a view that gathers data from multiple tables, and you want to insert the data from this view into a table. If the columns in the view have different data types or lengths compared to the table, you can face problems like: Data truncation : When the view has longer string fields than the table, any data that exceeds th...

The Function: uFnRemoveTrailingNumerics

  Removing Trailing Numerics from a String in SQL Server In SQL Server, there are scenarios where you might need to remove a specified number of numeric characters from the end of a string. Whether it's for data cleaning, formatting, or other purposes, having a custom function to handle this can be incredibly useful. Here, I'll walk you through creating a user-defined function to achieve this. This function, uFnRemoveTrailingNumerics , takes two parameters: the string from which to remove the numerics and the number of trailing numerics to remove. Function Definition sql CREATE FUNCTION [dbo].[uFnRemoveTrailingNumerics] ( @field varchar (max), @numToRemove int ) RETURNS Varchar (Max) AS BEGIN -- Declare the return variable here DECLARE @Result Varchar (Max) DECLARE @NumericSuffixLength int -- Calculate the length of the trailing numeric substring SELECT @NumericSuffixLength = LEN( @field ) - PATINDEX( '%[^0-9]%' , REVERSE...