Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Can I set 'Default value or binding' to "TOP"

Author  Topic 

weitzhandler
Yak Posting Veteran

64 Posts

Posted - 2009-01-24 : 17:35:59
I want to set the defauult value or binding of col SortOrder (int) to

((TOP 1 SortOrder) + 1)

?

Shimmy

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2009-01-24 : 19:39:06
I don't understand your question. Could you show us a data example?

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog
Go to Top of Page

weitzhandler
Yak Posting Veteran

64 Posts

Posted - 2009-01-24 : 20:01:59
just simple
in table definition i want to set one column 'Default valu or binding' property to (Select top 1 [ColName]

Shimmy
Go to Top of Page

sunitabeck
Master Smack Fu Yak Hacker

5155 Posts

Posted - 2009-01-24 : 20:34:35
There may be better ways to achieve what you need, such as by using an identity column or perhaps calculating the column in a view or in the queries against the table.

However, if you did want to do this, one way to it would be using a UDF as follows:

Define a UDF:
create FUNCTION dbo.fn_Table1Col1MaxValue() returns int
as
begin
return (select isnull(max(col1+1),1) as maxVal from Table1);
end
GO


Then, use the UDF to define the constraint:
create  table Table1 (
col1 int default dbo.fn_Table1Col1MaxValue(),
col2 varchar(10)
)


The usual restrictions about UDF's will apply, and it is schema-bound, so if you need to alter it you will need to drop the constraint first etc.
Go to Top of Page

sunitabeck
Master Smack Fu Yak Hacker

5155 Posts

Posted - 2009-01-24 : 20:39:40
And, I suspect that this will work only for single row inserts. So I take it back. Don't use it. Someone else may have a better idea.
Go to Top of Page

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2009-01-25 : 00:15:59
I still don't understand what he wants. I have a feeling he's looking for an identity column though.

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2009-01-25 : 04:43:23
i think it make more sense when you start from here

http://www.sqlteam.com/forums/topic.asp?TOPIC_ID=118393
Go to Top of Page

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2009-01-25 : 08:09:48
http://weblogs.sqlteam.com/jeffs/archive/2008/05/13/question-needed-not-answer.aspx


E 12°55'05.63"
N 56°04'39.26"
Go to Top of Page
   

- Advertisement -