Scientific applications can use SQL Server to store information about electrical components; this information can be obtained by either a data-logging device or an external application importing electrical data into SQL Server. To determine the current and voltage across every component on an AC circuit, you can use Ohm's law and complex algebra to manipulate the data stored in SQL Server.

AC is a time-dependent value that is often represented by the expression V = Vp * sin(2 * f * t) where Vp is the peak voltage, f is the frequency in hertz, and t is the time. Electrical values in AC circuits are often represented by complex numbers (a + bi) or by a vector defined by its polar representation (r, θ), where r is the vector length and θ is the angle between the vector and the reference line. Two electrical values are in phase with each other if they have the same angle θ, even if they have different lengths.

Depending on the electrical component, the current and the voltage across each component will be either in phase or not in phase with each other. When you apply a sinusoidal voltage V, across a linear resistance R, the current I traversing the component is in phase with the voltage, so you can represent the circuit's resistance by a scalar value (or a complex number with only a real part: Z = R + 0i). Other electrical components have a phase difference with the voltage; this is the case with pure inductance or capacitances, where the difference in phase with the current is 90 degrees. For these components, the quotient between voltage and current is called reactance, which is represented by a scalar imaginary value (a complex number with only an imaginary part Z = 0 + Xi). This value is equivalent to (X, 90º) in its polar representation because the real and imaginary axes have 90 degrees difference.

Electrical components are often a combination of pure resistance, capacitance, and inductance components, so their impedance could have a value of Z = R + Xi in general, where R is the reactive component and X is the inductive component L multiplied by the frequency w of the current: X = (L * w). The complex number representation of the impedance is Z = R + jwL (in electricity, complex numbers use a + jb notation because i is used for current).

Because frequency can be considered fixed, you could store circuit components in a SQL Server table, using the Complex user-defined data type (UDDT) for the Impedance column. In this case, I'll store R + 0j for the resistance, 0 + wLj for inductance, and 0 - (1 / (wC))j for capacitance.

You can also use complex notation for voltage and current, to show the phase differences between them. Once you know the current across a component, you can find the voltage as well. Just multiply the impedance Z by the current I to obtain the voltage V: V = I * Z, as the T-SQL code in Listing A shows.

To obtain the current @I across a component when you know the impedance @Z and the voltage @V, you would use

`SET @I = dbo.cxDivide(@V, @Z)`

Or to obtain the impedance @Z of a given component when you know the current @I and voltage @V across the component:

`SET @Z = dbo.cxDivide(@V, @I)`

If you have a series of components in the same electric line, the total impedance is the sum of the impedances of each component because

`Vt = V1 + V2 + ... + Vn = I *                               (Z1 + Z2 + ... + Zn)`

According to the preceding formula, you would execute the following T-SQL code to obtain the total impedance value:

`                              DECLARE @TotZ AS complex                              SELECT @TotZ = dbo.cxAdd(@TotZ, Impedance)                              FROM Components                              WHERE Circuit = 12                              SELECT @TotZ AS 'Total Impedance' `

If you have several components connected in parallel in an electric circuit, the total current is the total of the current crossing every individual component. You can calculate the total impedance of the group of components as follows:

`It = I1 + I2 + ... + In = V *                               (1/Z1 + 1/Z2 + ... + 1/Zn) = V *                               1/Zt `

So:

`Zt = 1 / (1/Z1 + 1/Z2 + ... + 1/Zn)`

According to the preceding formula, you would use the following T-SQL code to calculate the total impedance of a group of components connected in parallel:

`                              DECLARE @TotZ as complex                              SELECT @TotZ = dbo.cxAdd(@TotZ, dbo.cxDivide('1 + 0i', Impedance))                              FROM Components                              WHERE Circuit = 12                              SELECT dbo.cxDivide('1+0i', @TotZ)`

In this example, we applied the Complex UDDT and some complex UDFs to perform calculations on SQL Server data about electrical components used on AC circuits.