Having trouble setting a computed column as not Null

Deadly 提交于 2019-12-10 13:33:28

问题


I'm having problems setting a computed column as not null.

What I want to achieve is C001,C002..., etc. and at the same time set it as not null.

I have read on a forum that this can be achieved by using the default value 0 for NULL values.
E.g., ISNULL(Price + Taxes, 0)

I have tried to apply to this formula:

('C'+right('000'+CONVERT([varchar](3),[ID],(0)),(3)))

But it didn't seem to work. Can anyone tell me what am I missing?

ALTER CreditCard accountNo AS ISNULL('C'+right('000'+CONVERT([varchar](3),[idCreditCard],(0)),(3)),0)

回答1:


I've finally found the solution to my problem!

The correct query should be:

ALTER TABLE CreditCard ADD accountNo AS ISNULL('C'+right('000'+CONVERT([varchar](3),[idCreditCard],(0)),(3)),0)

Thanks for the help guys!




回答2:


If this relates to SQL Server, you might be interested in this from MSDN.

'NOT NULL can be specified for computed columns only if PERSISTED is also specified.' http://msdn.microsoft.com/en-us/library/ms190273.aspx

... but after trying to reverse-engineering the question from the answer, I think that 'keenlearner wanted only to ensure that there was never a null value in the column without having the constraint.



来源:https://stackoverflow.com/questions/11133665/having-trouble-setting-a-computed-column-as-not-null

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!