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.
Author |
Topic |
AskSQLTeam
Ask SQLTeam Question
0 Posts |
Posted - 2005-05-20 : 07:26:00
|
John writes "I have the following as asp.net code in page load:If Not Page.IsPostBack ThenDim Cmd As New SqlCommand("select dbo.getID(getDate())", cn)cn.Open()Dim dr As SqlDataReader = Cmd.ExecuteReader()'dr = Sql400Accessor.ExecuteReader(constr.ToString(), sqlqry.ToString(), CommandType.Text, "")If (dr.HasRows) ThenDo While dr.ReadOrderNum.Text = dr(0).ToString().Trim()LoopElseOrderNum.Text = "Cannot Get Details"End Ifdr.Close()cn.Close()cn = NothingEnd IfAnd the following is the function:CREATE FUNCTION GetID (@currDate datetime -- cannot execute getdate in UDF) RETURNS VARCHAR(10) AS BEGIN Declare @retID varchar(10) --- id to be returnedDeclare @maxID intDeclare @newint int-- get the max orderIDSelect @newint = COALESCE( max([OrderID]),1) from [Orders] -- if table has no ID set @retID = CAST (Year(@currDate)as varchar(5)) +-- add filler of "00" to start of string if new int < 9(CASE WHEN @newint < 10 THEN '/' + '00' + CAST(@newint as VARCHAR)-- add filler of "0" to start of string if new int < 99WHEN @newint < 100 THEN '/' + '0' + CAST(@newint as VARCHAR)ELSE '/' + CAST(@newint AS VARCHAR)END)RETURN @retIDENDMy Table is called Orders and the column is OrderID and I am writing the number that is generated to this column, but I am also reading the values from there to increment.When I go to generate a second record I keep getting the error: Cannot Get Details.Do you know what I am doing wrong, it is fine for generating the number the first time but after that get error.Thanks." |
|
AndrewMurphy
Master Smack Fu Yak Hacker
2916 Posts |
Posted - 2005-05-20 : 07:40:07
|
WHY are you not using the INBUILT function AUTOnumber available to be defined on the SQL column?You're reinventing a wheel.....save yourself the hassle |
 |
|
|
|
|