Note that there are some explanatory texts on larger screens.

plurals
  1. POa simple way to generate SQL Server's "standard" form of an expression?
    primarykey
    data
    text
    <p>This relates to computed columns and default constraints (and possibly other expressions) in SQL Server 2005 (or above). Both of these use an arbitrary expression to generate a value, e.g. <code>(year+1)</code> for a computed column representing "next year" (this is obviously a simple and stupid example).</p> <p><strong>What I'm trying to do:</strong> I want to be able to determine whether an existing computed column (or default constraint) in some table matches the intended definition, where the latter is defined in a software-generated schema that was used to create the table. I can get the definition of a computed column using <code>sp_helptext</code>, and the definition of a default constraint from the <code>sys.default_constraints</code> catalog view.</p> <p><strong>What I'm having trouble with:</strong> The expressions I get back from the above sources are in a normalized/standard form that doesn't match the form used to create the column/constraint. In the example above, SQL normalizes the expression to <code>([year]+(1))</code>. Therefore, a simple string comparison between this form and the original form will not reliably determine whether they are the same.</p> <p><strong>Solutions I've thought of already:</strong></p> <ul> <li>Generate the original expressions so that they match SQL's form. This requires knowing the rules SQL uses to generate its form, which are not documented, so it's not a great solution.</li> <li>Parse both forms into ASTs and compare those. I already an AST for the original form, but I don't have a parser, and would rather not write one.</li> <li>Create a temporary table and add a computed column using the original expression, and then read back the normalized expression. This would be pretty reliable, but it feels dirty, since in theory this comparison should be a read-only operation.</li> </ul> <p>Can anyone think of another good option for handling this? My hope is that maybe someone knows of some debugging/diagnostic tool that will spit back the input expression in normalized/standard form.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload