суббота, 20 июля 2013 г.

sql create function if not exists

IF object_id(N'[dbo].[GetTaskAmount]', N'FN') IS NULL
BEGIN
DECLARE @Function varchar(MAX)
SET @Function =
'CREATE FUNCTION [dbo].[GetTaskAmount]
(
@Id int
)
RETURNS decimal
AS
BEGIN
DECLARE @Amount decimal

SELECT @Amount = Quantity * (SELECT Price FROM TaskTypes WHERE Id = TypeId) FROM Tasks WHERE Id = @Id

RETURN @Amount;
END'
EXEC(@Function)
END

Комментариев нет: