English 中文(简体)
无法治愈的哥伦
原标题:Computed Column cannot be Persisted

我有惯例,我正试图利用这一职能设立一栏。

它给我留下错误。

表格“可使用”中的组合式SSN无法维持,因为这个栏目不是决定性的。

职能如下:

ALTER FUNCTION [dbo].[FormatSSN]()
RETURNS VARCHAR(11)
AS
BEGIN
    return   ;
END

下面是用这一功能添加一栏的问题:

ALTER TABLE SomeTable
ADD FormattedSSN as dbo.FormatSSN() PERSISTED

请说明是否有任何办法。 感谢。

最佳回答

∗∗∗∗∗∗

ALTER FUNCTION [dbo].[FormatSSN]
(
@SSN    VARCHAR(9)
)
RETURNS CHAR(11)
WITH SCHEMABINDING
AS
BEGIN
  your stuff here
END

之后进行核实:

IF OBJECTPROPERTY (OBJECT_ID(N [dbo].[FormatSSN] ), IsDeterministic ) = 1
   PRINT  Function is detrministic. 
ELSE IF OBJECTPROPERTY (OBJECT_ID(N [dbo].[FormatSSN] ), IsDeterministic ) = 0
   PRINT  Function is NOT detrministic 
GO

页: 1

问题回答

2. 如何直接确定定义:

ALTER TABLE SomeTable
ADD FormattedSSN as
    case when len(EmployeeSSN) = 9 then
            substring(EmployeeSSN, 1, 3) +  -  +
            substring(EmployeeSSN, 4, 2) +  -  +
            substring(EmployeeSSN, 6, 4)
    else EmployeeSSN end
PERSISTED

不是说UDF,而是把计算出来的一栏表示为

Case When Len(EmployeeSSN) = 0 Then 
      SUBSTRING(EmployeeSSN, 1, 3) +  -  + 
      SUBSTRING(EmployeeSSN, 4, 2) +  -  + 
      SUBSTRING(EmployeeSSN, 6, 4)
    Else EmployeeSSN End

在制作表格时,请加一栏:

[NewColumnName]  As
   (Case When len([UpdateUserId])=(0) T
         Then (((substring([UpdateUserId],(1),(3))+ - )+
                 substring([UpdateUserId],(4),(2)))+ - )+
                 substring([UpdateUserId],(6),(4)) 
         Else [UpdateUserId] End) PERSISTED,

创建适当数据类型的非投入一栏。 创建“插入触发点”(如果数据发生变化,则更新触发点)。 然后,你可以把职能产出放在一栏中。

ALTER TABLE SomeTable ADD FormattedSSN VARCHAR(11)
GO

CREATE TRIGGER dbo.TR_I_SomeTable ON  dbo.SomeTable AFTER INSERT
AS 
BEGIN
    SET NOCOUNT ON;

    update s
    set s.FormattedSSN = dbo.FormatSSN()
    from SomeTable AS s
        join inserted i on i.id = s.id

END
GO




相关问题
SQL SubQuery getting particular column

I noticed that there were some threads with similar questions, and I did look through them but did not really get a convincing answer. Here s my question: The subquery below returns a Table with 3 ...

难以执行 REGEXP_SUBSTR

I m 查询Oracle 10g。 我有两张表格(样本数据见下文)。 i m 试图提取一些领域

SQL Query Shortcuts

What are some cool SQL shorthands that you know of? For example, something I learned today is you can specify to group by an index: SELECT col1, col2 FROM table GROUP BY 2 This will group by col2

PHP array callback functions for cleaning output

I have an array of output from a database. I am wondering what the cleanest way to filter the values is example array Array ( [0] => Array ( [title] => title 1 ...

OracleParameter and DBNull.Value

we have a table in an Oracle Database which contains a column with the type Char(3 Byte). Now we use a parameterized sql to select some rows with a DBNull.Value and it doesn t work: OracleCommand ...

Running numbers in SQL

I have a SQL-statement like this: SELECT name FROM users WHERE deleted = 0; How can i create a result set with a running number in the first row? So the result would look like this: 1 Name_1 2 ...

How to get SQL queries for each user where env is production

I’m developing an application dedicated to generate statistical reports, I would like that user after saving their stat report they save sql queries too. To do that I wrote the following module: ...

热门标签