Making a Code Block from a Power BI Query Readable Using Notepad++

Power BI logo

Oftentimes, when looking at the Advanced Editor window on a Power BI query, you will see that the SQL query in one of the steps has been compressed into fewer lines – perhaps even only one line; minified with Power Query codes like “#(lf)” (line feed) and “#(tab)”.

To make this more readable, you can copy the code block from Power BI into a Notepad++ window, and replace these Power Query codes with escape sequences that will format the code into multiple lines that are easier to read.

Once the code is in Notepad++, open the Replace window with Ctrl+R, and select the Extended radio button in the Search Mode box in the lower left-hand corner.

Replace box in Notepad++

Put the offending Power Query codes into the “Find what” field, and the corresponding escape sequence into the “Replace with” field. Select Replace All.

Repeat this with each type of code that needs to be replaced. Afterwards, you can read and modify the code block, if you wish.

To convert the whole code block back into its original format, reverse the steps, putting the escape sequence in the “Find what” field and the original Power Query code in the “Replace with” field and select Replace All again.

Finding Column and Table Names in Amazon Redshift using SQL

Amazon Redshift logo

Finding metadata regarding table and column names in Redshift can be daunting, especially if you do not know if the tables are internal or external. Redshift has a table called SVV_EXTERNAL_COLUMNS that is useful for looking at external tables, and there are scripts to look at internal tables as well.

The SQL code I wrote below combines these and should make it easier to find what you’re looking for.

select * from (
select t.table_schema as schemaname,
       t.table_name as tablename,
       c.column_name as columnname,
       t.table_type
from information_schema.tables t
inner join information_schema.columns c 
           on c.table_name = t.table_name 
           and c.table_schema = t.table_schema
where t.table_schema not in ('information_schema', 'pg_catalog')      
union all
select schemaname,
       tablename,
       columnname,
       'EXTERNAL TABLE' as table_type
from svv_external_columns)
where columnname ilike '%whatever column%'
and tablename ilike '%whatever table%'
and schemaname ilike '%whatever schema%'
order by schemaname, tablename, columnname;

I have found this to be useful on numerous occasions, especially since the Databaser Explorer in SQL Workbench/J does not yet seem to have a way to search for column names.

Generating SQL Scripts on SQL Workbench/J to Create Amazon Redshift Stored Procedures

Amazon Redshift logo

The task of generating a SQL script for a stored procedure or function is fairly simple in Microsoft SQL Server using SQL Server Management Studio, or in Oracle Database, using SQL Developer.

However, if your database is Amazon Redshift, you’re probably using SQL Workbench/J to interact with your database, and unfortunately, its ability to script out stored procs is limited.

I recently discovered that, when selecting the stored procedure from a list in Database Explorer, the SQL script that is supposed to be able to re-create the stored proc does not work. It looks something like this:

DROP PROCEDURE schemaname.procname()
/

CREATE FUNCTION procname (null)
RETURNS -
VOLATILE
AS $$

	
	
begin

.... << stored proc code here >> ...

 end;
$$ LANGUAGE plpgsql
;

You might notice a few things about this code. First of all, this stored procedure is being called a function here rather than a procedure. Since it is, in reality, a stored procedure, it returns no value, but this script doesn’t seem to know how to do that. Also there happen to be no parameters for this stored proc, but the scripting engine puts “null” as the parameter data type rather than leaving empty parentheses. The code for this stored proc should be this:

CREATE OR REPLACE PROCEDURE schemaname.procname ()
AS $$

	
	
begin

.... << stored proc code here >> ...

 end;
$$ LANGUAGE plpgsql
;
/

I have, with a little help from the Amazon Redshift documentation and the PostgreSQL documentation, been able to write a script that will correctly script out your stored proc, with the exception of the datatypes for the parameters, if there are any. If there are any you will need to look at the pg_type table and the proargtype column in the pg_proc_info table.

 SELECT 'CREATE OR REPLACE ' || 
  CASE WHEN p.prokind='p' THEN 'PROCEDURE ' 
       WHEN p.prokind='f' THEN 'FUNCTION ' 
       ELSE '???? ' END ||
  n.nspname || '.' || proname || ' (' || 
  CASE WHEN p.proargtypes IS NULL OR p.proargtypes='' THEN ''
  ELSE ' ???? '
  END || 
  ')' ||  CHR(13) ||
  CASE WHEN p.prorettype=0 THEN ''  
       ELSE 'RETURNS ' || (SELECT TOP 1 typname FROM pg_type WHERE oid=p.prorettype) || CHR(13) END || 
  CASE WHEN p.prokind='p' THEN ''
       WHEN p.provolatile='i' THEN 'IMMUTABLE' 
       WHEN p.provolatile='s' THEN 'STABLE'  
       WHEN p.provolatile='v' THEN 'VOLATILE' END || 
  CHR(13) || 'AS $$' || CHR(13) || CHR(13) ||
  p.prosrc ||
  '$$ LANGUAGE plpgsql' || CHR(13) || ';' || CHR(13) || '/'
  FROM
    pg_catalog.pg_namespace n
JOIN pg_catalog.pg_proc_info p ON
    p.pronamespace = n.oid
JOIN pg_user b ON
    b.usesysid = p.proowner
WHERE
    n.nspname not in ('information_schema','pg_catalog') 
    and p.proname like '%whatever%;  --put a part or all of the function/proc name here with percent wildcards and remember that like is case-sensitive. Use ilike for case-insensitive searching.

The result should be a single column with the text for a script to re-create your stored procedure or function!