问题
I have a table that contain column with VARBINARY(MAX)
data type. That column represents different values for different types in my c# DB layer class. It can be: int
, string
and datetime
. Now I need to convert that one column into three by it's type. So values with int
type go to new column ObjectIntValue
and so on for every new column.
But I have a problems with transmitting data to datetime
column, because the old column contains datetime
value as a long
received from C# DateTime.ToBinary
method while data saving.
I should make that in TSQL and can't using .NET for convert that value in new column. Have you any ideas?
Thanks for any advice!
回答1:
Using CLR in T_SQl
Basically you use Create Assembly to register the dll with your function(s) in it, Then create a user defined function to call it, then you can use it. There's several rules depending on what you want to do, but as basically you only want DateTime.FromBinary(), shouldn't be too hard to figure out.
Never done it myself, but these guys seem to know what they are talking about
CLR in TSQL tutorial
This is a one off convert right? Your response to @schglurps is a bit of a concern.
If I get you there would have to be break in your update script, ie the one you have woukld work up to when you implement this chnage, then you's have a one off procedure for this manouevre, then you would be updating from a new version.
If you want to validate it, just check for the existnec or non-existance of the new columns.
Other option would be to write a wee application that filled in the new columns from the old one and invoke it. Ugh...
If this isn't one off and you want to keep and maintain the old column, then you have problems.
来源:https://stackoverflow.com/questions/8464504/converting-logic-of-datetime-frombinary-method-in-tsql-query