Cześć, mam problem, którego nie oczekiwałem przy tworzeniu funkcji. Mój skrypt wygląda tak:
-- wcześniej deklaruje i pobieram wartość dla dbVer
if @dbVer=4.0
begin
if exists(select * from information_schema.routines
where specific_name = 'DateTimeToUnix' and routine_type = 'FUNCTION')
drop function DateTimeToUnix -- błąd 1
GO
CREATE function [dbo].[DateTimeToUnix] -- błąd 2
( @DAY datetime )
returns int
as
begin
declare @wkdt datetime
if @DAY < '1901-12-13 20:45:51.500' or @DAY > '2038-01-19 03:14:07.497'
return null
select @wkdt = dateadd(ms,round(datepart(ms,@DAY),-3)-datepart(ms,@DAY),@DAY)
if @wkdt >= 712 return datediff(ss,25567,@wkdt)
return -2147472000-datediff(ss,@wkdt,712)
end
end
I teraz tak. Jeśli nie mam warunku IF EXISTS, błąd wyskakuje na linijce z CREATE:
Incorrect syntax near the keyword 'function'.
Jeśli mam IF EXISTS, błąd występuje na linijce DROP:
Incorrect syntax near 'DateTimeToUnix'.
O co tu chodzi? Czemu to nie działa? Oczywiście, jeśli pozbędę się warunku IF @dbVer..., to jest ok :|