Tags

I finally got tired of constantly having to alias the same fields and tables over and over, so i decided to write a script that uses the UTHELP table in the M2MSYSTEM database to auto generate a select statement and use the M2M label to alias the columns.

 

-- **********************************  *************************************-- 
DECLARE @table VARCHAR(200) = 'inmast' 
-- **********************************  *************************************-- 
DECLARE @clm VARCHAR(8000) = '', 
        @cmd VARCHAR(8000) = '' 

SELECT @clm = @clm + clm 
FROM   (SELECT DISTINCT clm 
        FROM   (SELECT ' [' + fcfield + '] as [' + CASE Ltrim(Rtrim(fclabel)) 
                       WHEN '' 
                       THEN 
                               Ltrim(Rtrim( 
                       fcfield)) ELSE Ltrim(Rtrim(fclabel)) END + '], ' clm 
                FROM   m2msystem.dbo.uthelp 
                WHERE  fctable LIKE '%' + @table + '%') a) b 

SET @clm = LEFT(@clm, Len(@clm) - 3) 
SET @cmd = 'SELECT ' + @clm + ' FROM ' + @table 

PRINT @cmd 

0
0
0
s2sdefault