473,383 Members | 1,803 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,383 software developers and data experts.

T-SQL: Recursion and circular references

balabaster
797 Expert 512MB
Hi,

I have a couple of tables:

Expand|Select|Wrap|Line Numbers
  1. Units(
  2.   Unit_PKey Int Identity(1,1) Primary Key,
  3.   Unit_Name nvarchar(8),
  4.   Unit_Description nvarchar(32)
  5. )
1, lb, Pounds
2, cwt, Hundredweight
3, kg, Kilograms

Expand|Select|Wrap|Line Numbers
  1. Conversions(
  2.   Cnvrt_PKey Int Identity(1,1) Primary Key,
  3.   Cnvrt_FromUnit Int Foreign Key References Units.Unit_PKey,
  4.   Cnvrt_ToUnit Int Foreign Key References Units.Unit_PKey, 
  5.   Cnvrt_Formula
  6. )
I am trying to build a recursive stored procedure that will handle transitive relationships, but I'm having some issues accounting for potential circular references.

Say I have the following table data (I will display the unit names with their keys in brackets so it's easier to follow.)

1, lb (1), cwt (2), {FROM}/100
2, cwt (2), lb (1), {FROM}*100
3, lb (1), kg (3), {FROM}/2.2046
4, kg (3), lb (1), {FROM}*2.2046

Obviously there is no formula in this table accounting for the conversion from kg to cwt, but a transitive relationship exists, such that (kg*2.2046)/100 converts kg to lbs.

In an ideal world, a formula would be in the database that allows the conversion directly kg*0.022046, and for performance purposes, I would push the users of this data to have formulae for direct conversions, but in the event someone's asleep at the wheel and no direct formula exists, I would like the system to build this transitive relationship.

A recursive procedure is easy enough to build, but I'm wondering what would be the best way to handle potentially disasterous circular references... I thought of passing up a list of previously checked "FromUnit"s thus allowing a select where the Cnvrt_ToUnit is not found in that list, and thus avoiding that scenario... the question is, what is the best way to pass this list up through the call stack?

My first thought is a temporary table, but:
  • A global temp table (##MyTempTable for instance) is visible across the whole system... what happens if multiple people call this procedure at the same time?
  • A local temporary table #MyTempTable isn't visible to each level of the call stack
  • A table variable can neither be passed as an input or an output to a stored procedure...

So where do I go from here? Any thoughts would be most welcome.
Aug 21 '08 #1
0 2805

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: Christine McGavran | last post by:
I have created an xml format for describing a custom user interface. My application successfully parses and displays the UI described in xml files created in that format. Before today we were...
16
by: Kiuhnm | last post by:
Is there an elegant way to deal with semi-circular definitions? Semi-circular definition: A { B }; B { *A }; Circular reference: A { *B }; B { *A }; The problems arise when there are more...
2
by: Earth Worm Jim | last post by:
I have been able to get simple circular references to be serialized in xml by using the ImportTypeMapping method on the SoapReflectionImporter class. But I am unable to serialise circular...
8
by: Eric Eggermann | last post by:
I'm having a problem with really large file sizes when serializing the classes that describe my little document. There are some circular references which result in the same object getting written...
5
by: Gos | last post by:
Hi, It is known that .NET does not allow us to add circular references. Is there a way to workaround this problem by late-binding the objects at run time? Will this create any other problems? ...
6
by: Stephen Robertson | last post by:
We are currently in a dead end with a circular reference issue using vb.net, and are hoping someone might help us resolve it. Idea... We have frmmain calling frmperson (dim f as new frmperson)...
2
by: Lapu-Lapu | last post by:
I have authored a web service using ASP 2.0. The web services return objects that use generics and that also contain circular references. Programmatically, everything works well, as long as you...
5
by: Madhur | last post by:
Hello If I define two classes in the same cs file. And in each class, I define the object of other class as a member. Can anyone explain me how .NET or its compiler will resolve this kind of...
2
by: Dansk | last post by:
Hi all, I am currently writing some code that explores assemblies dependencies. I start loading the first assembly with Assmebly.LoadFrom which gives me an Assembly instance. Then, I...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.