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

Computed Columns Expand / Collapse
Author
Message
Posted Wednesday, November 10, 2010 9:25 PM
SSC-Enthusiastic

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

Group: General Forum Members
Last Login: Thursday, February 21, 2013 3:14 AM
Points: 134, Visits: 425
Hi Guys,

Is there a way to reference columns of other table in computed columns except triggers?

Thanks in Advance
Post #1019042
Posted Wednesday, November 10, 2010 11:41 PM


SSCertifiable

SSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiable

Group: General Forum Members
Last Login: Today @ 12:10 AM
Points: 6,997, Visits: 8,410
I don't think so. simple reason ... this cannot be "at run time" but must be persisted, so a trigger is the only way.

Johan


Don't drive faster than your guardian angel can fly ...
but keeping both feet on the ground won't get you anywhere

- How to post Performance Problems
- How to post data/code to get the best help


- How to prevent a sore throat after hours of presenting ppt ?


"press F1 for solution", "press shift+F1 for urgent solution"


Need a bit of Powershell? How about this

Who am I ? Sometimes this is me but most of the time this is me
Post #1019071
Posted Thursday, November 11, 2010 2:31 AM


SSCertifiable

SSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiable

Group: General Forum Members
Last Login: Friday, January 03, 2014 3:52 PM
Points: 6,066, Visits: 5,277
The answer is yes, sort of.. You can build a UDF that references another table in the same database, and use that UDF in the formula, I believe you can persist it.

If you can persist it DO! You don't want it hitting that UDF for every access to that column..

That UDF becomes bound to the table and cannot be changed without a mod to the table first to remove the reference.

so get it right the first time..

CEWII
Post #1019172
Posted Thursday, November 11, 2010 11:59 AM


SSCertifiable

SSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiable

Group: General Forum Members
Last Login: Today @ 12:10 AM
Points: 6,997, Visits: 8,410
great... now you've had two answers saying No and Yes ...

That's what makes these fora good, you'll get alternatives and test hints.

I don't have time right now, but go on and just test the alternatives.

Pleas post feedback, so others can use your refs for their tests

In both cases you'll have to take into account there is a hidden join for your every application doing stuff with your table, unless this column is persisted.


Johan


Don't drive faster than your guardian angel can fly ...
but keeping both feet on the ground won't get you anywhere

- How to post Performance Problems
- How to post data/code to get the best help


- How to prevent a sore throat after hours of presenting ppt ?


"press F1 for solution", "press shift+F1 for urgent solution"


Need a bit of Powershell? How about this

Who am I ? Sometimes this is me but most of the time this is me
Post #1019502
Posted Thursday, November 11, 2010 12:38 PM


SSChampion

SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

Group: General Forum Members
Last Login: Today @ 3:44 PM
Points: 11,970, Visits: 10,995
This was actually the topic of yesterday's QOTD.

Here is a link to the ensuing discussion.


_______________________________________________________________

Need help? Help us help you.

Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.

Need to split a string? Try Jeff Moden's splitter.

Cross Tabs and Pivots, Part 1 – Converting Rows to Columns
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs
Understanding and Using APPLY (Part 1)
Understanding and Using APPLY (Part 2)
Post #1019536
Posted Thursday, November 11, 2010 2:28 PM


SSCertifiable

SSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiable

Group: General Forum Members
Last Login: Today @ 12:10 AM
Points: 6,997, Visits: 8,410
Thanks for this addition Sean

OP: Keep in mind it is not because you can do something in a certain way, you should. Think about the consequences.


Johan


Don't drive faster than your guardian angel can fly ...
but keeping both feet on the ground won't get you anywhere

- How to post Performance Problems
- How to post data/code to get the best help


- How to prevent a sore throat after hours of presenting ppt ?


"press F1 for solution", "press shift+F1 for urgent solution"


Need a bit of Powershell? How about this

Who am I ? Sometimes this is me but most of the time this is me
Post #1019607
Posted Monday, November 15, 2010 5:30 AM


SSChampion

SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

Group: General Forum Members
Last Login: Today @ 8:58 PM
Points: 11,168, Visits: 10,930
ningaraju.n (11/10/2010)
Is there a way to reference columns of other table in computed columns except triggers?

You could use a UDF that references the other table, but please don't. Functions that perform data access cannot be persisted, so it would be re-evaluated (effectively by running a separate query) once for every row it touches, every time. Absolutely horrible.

The idea of computed columns referencing other tables sounds like a VIEW to me.

Perhaps you could explain the circumstances more and provide an example to demonstrate what you are trying to achieve.

Again, please consider every possible alternative to using a function that does data access in a computed column.

Paul




Paul White
SQL Server MVP
SQLblog.com
@SQL_Kiwi
Post #1020675
Posted Monday, November 15, 2010 6:02 AM


SSChampion

SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

Group: General Forum Members
Last Login: Today @ 8:58 PM
Points: 11,168, Visits: 10,930
Code to demonstrate the issue, and show that UDFs that access data cannot be persisted:

CREATE  TABLE dbo.Data
(
item INTEGER NOT NULL PRIMARY KEY,
value INTEGER NOT NULL,
);

INSERT dbo.Data (item, value)
SELECT V.number,
RAND(CHECKSUM(NEWID())) * 1000000
FROM master.dbo.spt_values V
WHERE V.type = N'P ';
GO
CREATE FUNCTION dbo.BadFunction(@item INTEGER)
RETURNS INTEGER
WITH SCHEMABINDING
AS
BEGIN
RETURN
(
SELECT D.value
FROM dbo.Data D
WHERE D.item = @item
)
END;
GO
-- Error if PERSITED keyword is uncommented:
-- Msg 4934, Level 16, State 3, Line 1
-- Computed column 'value' in table 'BadIdea' cannot be persisted
-- because the column does user or system data access.
CREATE TABLE dbo.BadIdea
(
row_id INTEGER PRIMARY KEY,
value AS
dbo.BadFunction (row_id)
--PERSISTED
);
GO
INSERT dbo.BadIdea(row_id)
SELECT V.number
FROM master.dbo.spt_values V
WHERE V.type = N'P ';
GO
-- Trace in Profiler to see separate function call
-- and query execution per row. (Does not show in
-- SSMS actual query plan).
-- Trace SQL:Batch Starting and SP:Starting
-- Shows 2048 separate calls to the UDF BadFunction.
-- Notice how long this simple query takes to run
-- on only 2048 rows.
SELECT MIN(value)
FROM dbo.BadIdea;
GO
DROP TABLE dbo.BadIdea;
DROP FUNCTION dbo.BadFunction;
DROP TABLE dbo.Data;





Paul White
SQL Server MVP
SQLblog.com
@SQL_Kiwi
Post #1020686
Posted Monday, November 15, 2010 6:13 AM


SSCertifiable

SSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiable

Group: General Forum Members
Last Login: Today @ 12:10 AM
Points: 6,997, Visits: 8,410
Paul White NZ (11/15/2010)


If this message still is not understood, there will be no other means except for the server to blow up

Thanks Paul for this clarification.

I still suffer SPMOS (SQLP*** Memory Overload Symptoms )
I'll need another couple of days until common sense returns.


Johan


Don't drive faster than your guardian angel can fly ...
but keeping both feet on the ground won't get you anywhere

- How to post Performance Problems
- How to post data/code to get the best help


- How to prevent a sore throat after hours of presenting ppt ?


"press F1 for solution", "press shift+F1 for urgent solution"


Need a bit of Powershell? How about this

Who am I ? Sometimes this is me but most of the time this is me
Post #1020689
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse