Showing posts with label table2. Show all posts
Showing posts with label table2. Show all posts

Monday, March 12, 2012

Referencing a field in another table as part of a formula in a computed column

Hi guys, I am new to all of this stuff so please bare with me. I will try to explain this as simply as I can. I have 2 tables.. Table1 and Table2 they are linked using both the STOCKCODE and SUPPLIER fields. In Table 1 is the STOCKCODE, LENGTH AND SUPPLIER fields (there are others but these are the 3 that I need to use) In Table2 is STOCKCODE, SUPPLIER and SUPPLER_MASS. What I want to do is put a calculated column into Table2. The formula I need is fairly simple it is: 1/(LENGTH*SUPPLIER_MASS)

Ok so here is what I would like:

1/(SUPPLIER_MASS*(SELECT Table1.LENGTH FROM Table1 WHERE Table1.STOCKCODE=Table2.STOCKCODE AND Table1.SUPPLIER=Table2.SUPPLIER)

From what I understand I can't have a query in a calculated column's "formula" field?

So how would I do this? I don't want to run a query because LENGTH or SUPPLIER_MASS could change on a day to day basis meaning I will have to run the query everything one of these changes. The benefit of having the calculated column is that it will update as soon as LENGTH or SUPPLIER_MASS changes.

Is there any way I can do this?

Thanks guys (and girls)You can have insert, update trigger which will update calculated column every time you update a record or insert a new one.

Thank you.

Friday, March 9, 2012

Reference 2 Databases in SQL Statement

I need to join a table with another table in a different database. Is this possible?

Assume Table1 is in DB1 and Table2 is in DB2.

Thanks!

Brian

Simple, just prefrace the table name with the database name.

e.g. SELECT * FROM database1..table1 JOIN database2..table2 ON ...

|||

Brian:

It should simply be something like:

Code Snippet

select <columnList>

from Db1.schema1.Table1

join Db2.schema2.Table2

...

|||

search for "linked servers"

Reference 2 Databases in SQL Statement

I need to join a table with another table in a different database. Is this possible?

Assume Table1 is in DB1 and Table2 is in DB2.

Thanks!

Brian

Simple, just prefrace the table name with the database name.

e.g. SELECT * FROM database1..table1 JOIN database2..table2 ON ...

|||

Brian:

It should simply be something like:

Code Snippet

select <columnList>

from Db1.schema1.Table1

join Db2.schema2.Table2

...

|||

search for "linked servers"