Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase 12»»

Query-generated Column Expressions Expand / Collapse
Author
Message
Posted Monday, May 24, 2010 9:14 PM


SSC-Enthusiastic

SSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-Enthusiastic

Group: General Forum Members
Last Login: Thursday, December 12, 2013 1:09 PM
Points: 111, Visits: 541
Comments posted to this topic are about the item Query-generated Column Expressions

Bill Nicolich: www.SQLFave.com.
Daily tweet of what's new and interesting: AppendNow
Post #927173
Posted Tuesday, May 25, 2010 7:03 AM


SSC-Enthusiastic

SSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-Enthusiastic

Group: General Forum Members
Last Login: Thursday, December 12, 2013 1:09 PM
Points: 111, Visits: 541
Hey everyone. Not quite sure what happened, but the scripts in the article need a modification so they use a token for the column name: <column>.

INSERT INTO #ExpressionToDataTypeAndNull
( DATA_TYPE ,
IS_NULLABLE ,
CustomExpression
)
VALUES
( 'varchar','YES','LTRIM(RTRIM(ISNULL("<column>",''''))) AS "<column>",')
,( 'varchar','NO','LTRIM(RTRIM("<column>")) AS "<column>",')
,( 'nvarchar','YES','LTRIM(RTRIM(ISNULL("<column>",''''))) AS "<column>",')
,( 'nvarchar','NO','LTRIM(RTRIM("<column>")) AS "<column>",')
,( 'datetime','YES','CAST("<column>" AS varchar(max)) AS "<column>",')
,( 'datetime','NO','CAST("<column>" AS varchar(max)) AS "<column>",')
;

and

DECLARE @TableName nvarchar(128)
SET @TableName = 'FictionalEmployees'

SELECT
ISNULL(REPLACE(CustomExpression,'<column>', COLUMN_NAME), COLUMN_NAME + ' AS ' + COLUMN_NAME) AS ThisColumnExpression
FROM INFORMATION_SCHEMA.COLUMNS a
FULL OUTER JOIN #ExpressionToDataTypeAndNull b
ON a.DATA_TYPE = b.DATA_TYPE
AND a.IS_NULLABLE = b.IS_NULLABLE
WHERE TABLE_NAME = @TableName
ORDER BY ordinal_position ASC;

Once you have the token <column> replaced by the column name, your output includes the column names as advertised in the results.

Sorry about that! I'm not sure what happened there.


Bill Nicolich: www.SQLFave.com.
Daily tweet of what's new and interesting: AppendNow
Post #927424
Posted Tuesday, May 25, 2010 8:09 AM
SSC Rookie

SSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC Rookie

Group: General Forum Members
Last Login: Thursday, December 4, 2014 8:22 AM
Points: 29, Visits: 233
Try to specify the alises all in one place. Save yourself some typing. Also build the full select starement. Also could be used as a create view statement. Interesting idea though.

INSERT INTO #ExpressionToDataTypeAndNull
( DATA_TYPE ,
IS_NULLABLE ,
CustomExpression
)
VALUES
( 'varchar','YES','LTRIM(RTRIM(ISNULL("<column>",'''')))')
,( 'varchar','NO','LTRIM(RTRIM("<column>"))"')
,( 'nvarchar','YES','LTRIM(RTRIM(ISNULL("<column>",'''')))')
,( 'nvarchar','NO','LTRIM(RTRIM("<column>"))')
,( 'datetime','YES','CAST("<column>" AS varchar(max))')
,( 'datetime','NO','CAST("<column>" AS varchar(max))')
;

DECLARE @TableName nvarchar(128)
SET @TableName = 'FictionalEmployees'

DECLARE @SqlSelect NVARCHAR(MAX)

SELECT @SqlSelect = COALESCE(
@SqlSelect + ', ' + ISNULL(REPLACE(CustomExpression,'<column>', COLUMN_NAME), COLUMN_NAME )
, ISNULL(REPLACE(CustomExpression,'<column>', COLUMN_NAME), COLUMN_NAME )
) + ' AS "' + COLUMN_NAME + '"'
FROM INFORMATION_SCHEMA.COLUMNS a
FULL OUTER JOIN #ExpressionToDataTypeAndNull b
ON a.DATA_TYPE = b.DATA_TYPE
AND a.IS_NULLABLE = b.IS_NULLABLE
WHERE TABLE_NAME = @TableName
ORDER BY ordinal_position ASC;


SET @SqlSelect = 'SELECT ' + @SqlSelect + 'FROM ' + @TableName
PRINT @SqlSelect
-- EXECUTE sp_executesql @SqlSelect

Post #927494
Posted Tuesday, May 25, 2010 9:44 AM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Friday, September 24, 2010 3:25 AM
Points: 5, Visits: 16
Hi.

An interesting article! I just thought I'd point out that:

"FULL OUTER JOIN allows the query to return columns that don't have a match in the #ExpressionToDataTypeAndNull table"

is not 100% right. What you are describing is a LEFT outer join.

A FULL outer join would return results in the opposite case too - i.e. if there were row(s) in the #ExpressionToDataTypeAndNull table for which there is no match in INFORMATION_SCHEMA.COLUMNS.

However, as you are specifying in your WHERE clause that TABLE_NAME = @TableName, you are effectively converting your full outer join into a left outer join, (perhaps unknowingly!)

Just wanted to clarify that, in case other readers find it misleading!

Keep up the good work.

Cheers,
Nick
Post #927608
Posted Tuesday, May 25, 2010 1:25 PM


SSC-Enthusiastic

SSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-Enthusiastic

Group: General Forum Members
Last Login: Thursday, December 12, 2013 1:09 PM
Points: 111, Visits: 541
Nick Chadwick (5/25/2010)
Hi.

An interesting article! I just thought I'd point out that:



Thanks Nick! You put it nicely. It's very much the case here at SSC.COM that ones peers have enough passion and interest to find something if it's amiss or point out nuances. That's one reason why I like to contribute - for the challenge and learning experience of it.

If you don't believe me, put something in and give it a shot!

I didn't want to lengthen the article too much, but one point I'll add is that with this technique, one can build a little library of functions and keep it handy for those ETL, Ad-hoc or other situations.



Bill Nicolich: www.SQLFave.com.
Daily tweet of what's new and interesting: AppendNow
Post #927758
Posted Tuesday, May 25, 2010 2:11 PM
SSC-Enthusiastic

SSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-Enthusiastic

Group: General Forum Members
Last Login: Wednesday, January 26, 2011 1:32 PM
Points: 146, Visits: 327
Nick,

This seemed to work a little better for me:

CREATE TABLE #ExpressionToDataTypeAndNull
(
DATA_TYPE VARCHAR(15),
IS_NULLABLE VARCHAR(3) ,
CustomExpression VARCHAR(128)
)

INSERT INTO #ExpressionToDataTypeAndNull( DATA_TYPE , IS_NULLABLE , CustomExpression )
VALUES ( 'varchar','YES','LTRIM(RTRIM(ISNULL("<column>",''''))) AS "<column>"')

INSERT INTO #ExpressionToDataTypeAndNull( DATA_TYPE , IS_NULLABLE , CustomExpression )
VALUES ( 'varchar','NO','LTRIM(RTRIM("<column>")) AS "<column>"')

INSERT INTO #ExpressionToDataTypeAndNull( DATA_TYPE , IS_NULLABLE , CustomExpression )
VALUES ( 'nvarchar','YES','LTRIM(RTRIM(ISNULL("<column>",''''))) AS "<column>"')

INSERT INTO #ExpressionToDataTypeAndNull( DATA_TYPE , IS_NULLABLE , CustomExpression )
VALUES ( 'nvarchar','NO','LTRIM(RTRIM("<column>")) AS "<column>"')

INSERT INTO #ExpressionToDataTypeAndNull( DATA_TYPE , IS_NULLABLE , CustomExpression )
VALUES ( 'datetime','YES','CAST("<column>" AS varchar(max)) AS "<column>"')

INSERT INTO #ExpressionToDataTypeAndNull( DATA_TYPE , IS_NULLABLE , CustomExpression )
VALUES ( 'datetime','NO','CAST("<column>" AS varchar(max)) AS "<column>"')

DECLARE @TableName VARCHAR(128)
SET @TableName = 'EntityRelationshipCache'

DECLARE @SqlSelect NVARCHAR(MAX)

SELECT @SqlSelect = COALESCE( @SqlSelect + ', ' + ISNULL(REPLACE(CustomExpression,'<column>', COLUMN_NAME), COLUMN_NAME )
, ISNULL(REPLACE(CustomExpression,'<column>', COLUMN_NAME), COLUMN_NAME ) )
FROM INFORMATION_SCHEMA.COLUMNS a
FULL OUTER JOIN #ExpressionToDataTypeAndNull b
ON a.DATA_TYPE = b.DATA_TYPE
AND a.IS_NULLABLE = b.IS_NULLABLE
WHERE TABLE_NAME = @TableName
ORDER BY ordinal_position ASC;

SET @SqlSelect = 'SELECT ' + REPLACE( @SqlSelect, '"', '') + ' FROM ' + @TableName

PRINT @SqlSelect

--EXECUTE ( @SqlSelect )

IF ( OBJECT_ID( 'TEMPDB..#ExpressionToDataTypeAndNull' ) IS NOT NULL )
BEGIN
DROP TABLE #ExpressionToDataTypeAndNull
END

Nice Concept,
Doug
Post #927783
Posted Tuesday, May 25, 2010 6:37 PM


SSCoach

SSCoachSSCoachSSCoachSSCoachSSCoachSSCoachSSCoachSSCoachSSCoachSSCoachSSCoach

Group: General Forum Members
Last Login: Yesterday @ 3:20 PM
Points: 18,064, Visits: 16,099
Nice concept. Thanks for sharing.



Jason AKA CirqueDeSQLeil
I have given a name to my pain...
MCM SQL Server, MVP


SQL RNNR

Posting Performance Based Questions - Gail Shaw
Post #927886
Posted Tuesday, May 25, 2010 7:42 PM


SSC-Dedicated

SSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-Dedicated

Group: General Forum Members
Last Login: Yesterday @ 7:27 AM
Points: 35,769, Visits: 32,437
Douglas Osborne-456728 (5/25/2010)
Nick,

This seemed to work a little better for me:


How so? Please describe.


--Jeff Moden
"RBAR is pronounced "ree-bar" and is a "Modenism" for "Row-By-Agonizing-Row".

First step towards the paradigm shift of writing Set Based code:
Stop thinking about what you want to do to a row... think, instead, of what you want to do to a column."

(play on words) "Just because you CAN do something in T-SQL, doesn't mean you SHOULDN'T." --22 Aug 2013

Helpful Links:
How to post code problems
How to post performance problems
Post #927893
Posted Tuesday, May 25, 2010 7:45 PM


SSC-Dedicated

SSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-Dedicated

Group: General Forum Members
Last Login: Yesterday @ 7:27 AM
Points: 35,769, Visits: 32,437
Bill Nicolich (5/25/2010)
Hey everyone. Not quite sure what happened, but the scripts in the article need a modification so they use a token for the column name: <column>.

...

Sorry about that! I'm not sure what happened there.


Bill, send a correction to Steve... he'll get it in. Thanks for taking the time to write this article. Code to write code is one of the essentials to make development a wee bit faster.


--Jeff Moden
"RBAR is pronounced "ree-bar" and is a "Modenism" for "Row-By-Agonizing-Row".

First step towards the paradigm shift of writing Set Based code:
Stop thinking about what you want to do to a row... think, instead, of what you want to do to a column."

(play on words) "Just because you CAN do something in T-SQL, doesn't mean you SHOULDN'T." --22 Aug 2013

Helpful Links:
How to post code problems
How to post performance problems
Post #927895
Posted Wednesday, May 26, 2010 3:28 AM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Friday, September 24, 2010 3:25 AM
Points: 5, Visits: 16
Jeff Moden (5/25/2010)
Douglas Osborne-456728 (5/25/2010)
Nick,

This seemed to work a little better for me:


How so? Please describe.


Doug has added in the "as <column>" on each expression, so that the results have named columns. Nice touch!

Cheers,
Nick
Post #928058
« Prev Topic | Next Topic »

Add to briefcase 12»»

Permissions Expand / Collapse