Files
Fuchs_Intranet/db/dbo.ocms_createPropertyItem.StoredProcedure.sql
T

97 lines
6.4 KiB
Transact-SQL

USE [site_fuchs]
GO
/****** Object: StoredProcedure [dbo].[ocms_createPropertyItem] Script Date: 02.12.2020 21:05:05 ******/
DROP PROCEDURE [dbo].[ocms_createPropertyItem]
GO
/****** Object: StoredProcedure [dbo].[ocms_createPropertyItem] Script Date: 02.12.2020 21:05:06 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author: <Author,,Name>
-- Create date: <Create Date,,>
-- Description: <Description,,>
-- =============================================
CREATE PROCEDURE [dbo].[ocms_createPropertyItem]
@name nvarchar(255),
@parent_iid bigint,
@ocms_pid bigint,
@property_key varchar(50),
@template_id bigint,
@locale varchar(5)
AS
BEGIN
SET NOCOUNT ON;
DECLARE @site bigint = (SELECT TOP(1) [ocms_iid] FROM [dbo].[ocms_items] as i WHERE i.[parent_iid] is null);
IF ISNULL(@name, '') = ''
THROW 61010, 'name missing', 1;
IF EXISTS (SELECT * FROM [dbo].[ocms_items] WHERE [name] = @name)
THROW 61010, 'name already exists', 1;
IF NOT EXISTS (SELECT * FROM [dbo].[ocms_items] WHERE [ocms_iid] = @parent_iid)
THROW 61010, 'parent does not exist', 1;
IF NOT EXISTS (SELECT * FROM [dbo].[ocms_templates] WHERE [ocms_tid] = @template_id)
THROW 61010, 'template does not exist', 1;
IF NOT EXISTS (SELECT * FROM [dbo].[ocms_properties] WHERE [ocms_iid] = @parent_iid AND ([ocms_pid] = @ocms_pid OR (@ocms_pid is null AND [key] = @property_key)))
BEGIN
DECLARE @newprop [dbo].[ocms_type_properties_base];
INSERT INTO @newprop VALUES(@parent_iid, @property_key, '', @locale);
EXECUTE [dbo].[ocms_merge_properties] @newprop; --makes sure is ok with template
END
SELECT TOP(1) @ocms_pid = [ocms_pid] FROM [dbo].[ocms_properties] WHERE [ocms_iid] = @parent_iid AND ([ocms_pid] = @ocms_pid OR (@ocms_pid is null AND [key] = @property_key));
--get last ocms_iid
DECLARE @lastiid int = ISNULL ( (SELECT MAX(ISNULL(p.[ocms_iid], ap.[ocms_iid]))
FROM [dbo].[ocms_items] as p FULL OUTER JOIN [ocms_archive__items] as ap on p.[ocms_iid] = ap.[ocms_iid])
,0);
IF NOT EXISTS (SELECT * FROM [dbo].[ocms_properties] WHERE [ocms_iid] = @parent_iid AND [ocms_pid] = @ocms_pid)
THROW 61010, 'parent''s property does not exist', 1;
IF @site is not null
BEGIN
MERGE [dbo].[ocms_items] as TARGET
USING (VALUES(
@parent_iid
,@ocms_pid
,@name
,0
,@template_id
,0
,999 -- append always
)) as SOURCE ([parent_iid]
,[ocms_pid]
,[name]
,[view]
,[template_id]
,[hide]
,[order])
ON TARGET.[name] = SOURCE.[name]
WHEN NOT MATCHED BY TARGET THEN
INSERT
([ocms_iid]
,[parent_iid]
,[ocms_pid]
,[name]
,[view]
,[template_id]
,[hide]
,[order]
,[DateCreated]
,[DateModified])
VALUES( @lastiid + 1, SOURCE.[parent_iid], SOURCE.[ocms_pid], SOURCE.[name], SOURCE.[view], SOURCE.[template_id], SOURCE.[hide], SOURCE.[order], GETUTCDATE(), GETUTCDATE())
OUTPUT inserted.[ocms_iid];
END
END
GO
ALTER AUTHORIZATION ON [dbo].[ocms_createPropertyItem] TO SCHEMA OWNER
GO