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
 Converting a SP to UDF

Author  Topic 

Vijaykumar_Patil
Posting Yak Master

121 Posts

Posted - 2007-05-22 : 04:21:37
Hi All,
I am in the process of changing the cursor based Proc with temp tables, I have one of the Procs which is beening used to get a value for the cursor, I am thinking if it is possible to change it to a function so that I can update the column at once.

[code]
CREATE PROCEDURE[dbo].[get_cre_tijd_id] (
@tijd_datum datetime,
@tijd_id int output
)
AS


DECLARE @datum_tekst VARCHAR(25)
DECALRE @datum_tijd DATETIME

SET @datum_tekst = CAST(@tijd_datum AS VARCHAR);
SET @datum_tijd = CONVERT(DATETIME, SUBTRING(@datum_tekst, 1, 12), 21 )
SET @tijd_id = (SELECT tijd_id FROM dim_tijd WHERE tijd_datum = @datum_tijd )

IF @tijd_id IS NULL
BEGIN
INSERT INTO dim_tijd (
tijd_datum
, jaar
, kwartaal
, maand
, dag
, periode
, week
, weekdag
)
VALUES ( @datum_tijd
, datepart(yy,@tijd_datum)
, datepart(q,@tijd_datum)
, datepart(m,@tijd_datum)
, datepart(d,@tijd_datum)
, ceiling(datepart(wk,@tijd_datum)/4.00)
,dbo.get_iso_week(@tijd_datum)
, datepart(dw,@tijd_datum)
)

SET @tijd_id = (SELECT tijd_id FROM dim_tijd WHERE tijd_datum = @datum_tijd)
END

Necessity is the mother of all inventions!

shallu1_gupta
Constraint Violating Yak Guru

394 Posts

Posted - 2007-05-22 : 05:53:42
Please make sure that a UDF cannot modify data in permanent tables.
Go to Top of Page

Vijaykumar_Patil
Posting Yak Master

121 Posts

Posted - 2007-05-22 : 08:38:54
Cannot I EXEC a Proc or something else to take care of the expection of the value not being in the table?

UDF gives

SET @tijd_id = (
SELECT tijd_id FROM dim_tijd
WHERE tijd_datum = @datum_tijd


So the Insert is what is getting missed. Any way I can handle that???

Necessity is the mother of all inventions!
Go to Top of Page

Dallr
Yak Posting Veteran

87 Posts

Posted - 2007-05-22 : 09:42:15
Wouldn't it be better if you put
SELECT @tijd_id = tijd_id FROM dim_tijd WHERE tijd_datum = @datum_tijd

Insert of
SET @tijd_id = (
SELECT tijd_id FROM dim_tijd
WHERE tijd_datum = @datum_tijd)

Go to Top of Page

Vijaykumar_Patil
Posting Yak Master

121 Posts

Posted - 2007-05-23 : 07:21:32
Ok Dallr
SELECT @tijd_id = tijd_id FROM dim_tijd WHERE tijd_datum = @datum_tijd
... But that was not my question.Is there any other way of getting it done.
IF @tijd_id IS NULL
BEGIN
INSERT INTO dim_tijd (
tijd_datum
, jaar
, kwartaal
, maand
, dag
, periode
, week
, weekdag
)
VALUES ( @datum_tijd
, datepart(yy,@tijd_datum)
, datepart(q,@tijd_datum)
, datepart(m,@tijd_datum)
, datepart(d,@tijd_datum)
, ceiling(datepart(wk,@tijd_datum)/4.00)
,dbo.get_iso_week(@tijd_datum)
, datepart(dw,@tijd_datum)
)

Necessity is the mother of all inventions!
Go to Top of Page
   

- Advertisement -