开发者

Managing Defaults in Stored Procedures' Parameters

I am working in a DB which has about 10 DEFAULT constraints scattered by two or three different tables, and I need to write a stored procedure which receives as parameters values for all these attributes. However, if no values are received, I want these parameters to assume the default values in the corresponding table. Knowing that there is no easy way to do it, I thought about writing custom UDFs returning the default values, adding the constraints as DEFAULT (dbo.SomeFunction()) FOR [Attribute] and adding = dbo.SomeFunction() as the default value for each parameter. I would, however, like to ask what is the best way to accomplish this:

1) Write an UDF for every attribute, simplifying execution but ending up with 10 different functions; or

2) Write a single dbo.GetDefault(开发者_StackOverflow社区TableName NVARCHAR(32), AttributeName NVARCHAR(32) and use IF ... ELSE ... or SWITCH on the different table/attribute pairs.

What do you think?


You should have one udf per default.

  • datatypes won't be the same
  • as your code evolves you'll end up with dependencies in computed columns or schemabound views: it you need another it'll be messy to change
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜