Description
Properties
| Name | Value |
|---|---|
| ANSI Nulls ON | True |
| Quoted Identifier ON | True |
| Encrypted | False |
| Execute As | |
| Assembly |
Parameters
| Name | Data Type | Length | Description |
|---|---|---|---|
| @PARAMETER | xml | -1 |
SQL Script
SET QUOTED_IDENTIFIER, ANSI_NULLS ON
GO
CREATE procedure dbo.sp_kurs_header_insert
@PARAMETER xml=''
---XencX---with encryption
as
SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED
declare @pemisah nvarchar(255)
declare @pemisah_replace nvarchar(255)
set @pemisah_replace='<br />'
--set @pemisah=CHAR(13) + CHAR(10) + '
'
set @pemisah = '
'
declare @resultX nvarchar(max)
declare @resultX2 nvarchar(max)
declare @XmlSts xml
declare @raw xml
declare @xStsnoheader nvarchar(2),
@xStsno nvarchar(2),
@xStsdes nvarchar(max)
set @xStsnoheader='07'
set @xStsno='00';
set @xStsdes='sukses'
BEGIN TRY
SET NOCOUNT ON
DECLARE @NOW datetime
set @NOW =GETDATE()
declare @data_by nvarchar(20)
,@no_skep nvarchar(255)
,@startdate date
,@enddate date
,@id bigint
,@file int
,@file_name nvarchar(255)
,@file_type nvarchar(255)
,@file_path nvarchar(500)
,@full_path nvarchar(1000)
,@raw_name nvarchar(255)
,@orig_name nvarchar(255)
,@client_name nvarchar(255)
,@file_ext nvarchar(255)
,@file_size nvarchar(255)
,@is_image nvarchar(255)
,@image_width nvarchar(255)
,@image_height nvarchar(255)
,@image_type nvarchar(255)
,@image_size_str nvarchar(255)
,@headerid bigint
SELECT
@data_by=tempTable.item.value('data_by[1]', 'nvarchar(255)')
,@no_skep=left(ltrim(rtrim(isnull(tempTable.item.value('no_skep[1]', 'nvarchar(285)'),''))),255)
,@startdate=isnull(tempTable.item.value('startdate[1]', 'nvarchar(285)'),'1900-01-01')
,@id=isnull(tempTable.item.value('id[1]', 'bigint'),0)
,@file=isnull(tempTable.item.value('file[1]', 'int'),0)
,@file_name=left(ltrim(rtrim(isnull(tempTable.item.value('file_name[1]', 'nvarchar(285)'),''))),255)
,@file_type=left(ltrim(rtrim(isnull(tempTable.item.value('file_type[1]', 'nvarchar(285)'),''))),255)
,@file_path=left(ltrim(rtrim(isnull(tempTable.item.value('file_path[1]', 'nvarchar(530)'),''))),500)
,@full_path=left(ltrim(rtrim(isnull(tempTable.item.value('full_path[1]', 'nvarchar(1030)'),''))),1000)
,@raw_name=left(ltrim(rtrim(isnull(tempTable.item.value('raw_name[1]', 'nvarchar(285)'),''))),255)
,@orig_name=left(ltrim(rtrim(isnull(tempTable.item.value('orig_name[1]', 'nvarchar(285)'),''))),255)
,@client_name=left(ltrim(rtrim(isnull(tempTable.item.value('client_name[1]', 'nvarchar(285)'),''))),255)
,@file_ext=left(ltrim(rtrim(isnull(tempTable.item.value('file_ext[1]', 'nvarchar(285)'),''))),255)
,@file_size=left(ltrim(rtrim(isnull(tempTable.item.value('file_size[1]', 'nvarchar(285)'),''))),255)
,@is_image=left(ltrim(rtrim(isnull(tempTable.item.value('is_image[1]', 'nvarchar(285)'),''))),255)
,@image_width=left(ltrim(rtrim(isnull(tempTable.item.value('image_width[1]', 'nvarchar(285)'),''))),255)
,@image_height=left(ltrim(rtrim(isnull(tempTable.item.value('image_height[1]', 'nvarchar(285)'),''))),255)
,@image_type=left(ltrim(rtrim(isnull(tempTable.item.value('image_type[1]', 'nvarchar(285)'),''))),255)
,@image_size_str=left(ltrim(rtrim(isnull(tempTable.item.value('image_size_str[1]', 'nvarchar(285)'),''))),255)
FROM @PARAMETER.nodes('param/field') tempTable(item)
set @enddate = DATEADD(Day,6,@startdate)
if DATEPART(weekday,@startdate) != 4
begin
set @xStsno='02';
set @xStsdes= ' Tanggal ' + convert(varchar(10),@startdate,121)+' bukan hari Rabu';
goto f
end
if exists (select * from dt_currencies_header where startdate = @startdate and id != @id)
begin
set @xStsno='02';
set @xStsdes= ' Tanggal ' + convert(varchar(10),@startdate,121)+' sudah ada di databaseaa';
goto f
end
declare @tmp_kurs table (
currencycode varchar(255),
rate decimal(38,4)
)
declare @tmp_kurs2 table (
currenciesid bigint
, rate decimal(18,4)
)
declare @str nvarchar(max) = ''
if @file =1
begin
set @str = @str + 'select F1, convert(decimal(30,4),convert(float,F2)) FROM OPENROWSET('
set @str = @str + '''Microsoft.ACE.OLEDB.12.0'','
set @str = @str + '''Excel 12.0;Database=' + @full_path +';HDR=NO'','
set @str = @str +
'''SELECT * FROM [Sheet1$A2:B]'') A WHERE F1 IS NOT NULL '
insert into @tmp_kurs(currencycode, rate)
exec(@str)
end
declare @currencycode varchar(255)
, @rate decimal(18,4)
, @currenciesid bigint
IF CURSOR_STATUS('global','loopkurs')>=-1
BEGIN
DEALLOCATE loopkurs
END
Declare loopkurs CURSOR FOR SELECT currencycode, rate from @tmp_kurs
OPEN loopkurs
FETCH NEXT FROM loopkurs INTO @currencycode, @rate
WHILE @@FETCH_STATUS = 0
BEGIN
if not exists (select * from dt_currencies_master where currenciescode = @currencycode)
begin
set @xStsno='02';
set @xStsdes= ' Valuta Code ' + convert(varchar(10),@currencycode)+' tidak ada di master';
goto f
end
select @currenciesid = id from dt_currencies_master where currenciescode = @currencycode
if exists (select * from @tmp_kurs2 where currenciesid = @currenciesid)
begin
update @tmp_kurs2 set rate = @rate where currenciesid = @currenciesid
end
else
begin
insert into @tmp_kurs2 (currenciesid, rate)
values (@currenciesid, @rate)
end
FETCH NEXT FROM loopkurs INTO @currencycode, @rate
END
CLOSE loopkurs
DEALLOCATE loopkurs
BEGIN TRANSACTION;
if @id != 0
begin
update dt_currencies_header set no_skep = @no_skep, editby = @data_by, editdate = getdate() where id = @id
MERGE dt_currencies AS A
USING (
Select @id as headerid, currenciesid, rate from @tmp_kurs2
) AS B
ON (A.headerid = B.headerid AND A.currenciesid = B.currenciesid)
WHEN NOT MATCHED THEN
INSERT(headerid, currenciesid, rate, createby, createdate) VALUES(B.headerid, B.currenciesid, B.rate, @data_by, getdate())
WHEN MATCHED THEN
UPDATE SET A.rate = B.rate , A.editby = @data_by, editdate = getdate();
end
else
begin
insert into dt_currencies_header(no_skep, startdate, enddate, createby, createdate)
values (@no_skep, @startdate, @enddate, @data_by, getdate())
set @headerid = SCOPE_IDENTITY()
insert into dt_currencies (headerid, currenciesid, rate, createby, createdate)
select @headerid, currenciesid, rate, @data_by, getdate() from @tmp_kurs2
end
COMMIT;
f:
set nocount off
END TRY
BEGIN CATCH
IF (@@TRANCOUNT > 0) ROLLBACK TRAN;
set @xStsno='88'
set @xStsdes= ERROR_MESSAGE()
END CATCH
g:
set @XmlSts=(select @xStsnoheader + @xStsno as 'no', @xStsdes as 'des' for xml path('sts'))
select CAST( (select @XmlSts,isnull(@raw ,'') for xml path ('result')
) as XML) as dt
GO
GO
CREATE procedure dbo.sp_kurs_header_insert
@PARAMETER xml=''
---XencX---with encryption
as
SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED
declare @pemisah nvarchar(255)
declare @pemisah_replace nvarchar(255)
set @pemisah_replace='<br />'
--set @pemisah=CHAR(13) + CHAR(10) + '
'
set @pemisah = '
'
declare @resultX nvarchar(max)
declare @resultX2 nvarchar(max)
declare @XmlSts xml
declare @raw xml
declare @xStsnoheader nvarchar(2),
@xStsno nvarchar(2),
@xStsdes nvarchar(max)
set @xStsnoheader='07'
set @xStsno='00';
set @xStsdes='sukses'
BEGIN TRY
SET NOCOUNT ON
DECLARE @NOW datetime
set @NOW =GETDATE()
declare @data_by nvarchar(20)
,@no_skep nvarchar(255)
,@startdate date
,@enddate date
,@id bigint
,@file int
,@file_name nvarchar(255)
,@file_type nvarchar(255)
,@file_path nvarchar(500)
,@full_path nvarchar(1000)
,@raw_name nvarchar(255)
,@orig_name nvarchar(255)
,@client_name nvarchar(255)
,@file_ext nvarchar(255)
,@file_size nvarchar(255)
,@is_image nvarchar(255)
,@image_width nvarchar(255)
,@image_height nvarchar(255)
,@image_type nvarchar(255)
,@image_size_str nvarchar(255)
,@headerid bigint
SELECT
@data_by=tempTable.item.value('data_by[1]', 'nvarchar(255)')
,@no_skep=left(ltrim(rtrim(isnull(tempTable.item.value('no_skep[1]', 'nvarchar(285)'),''))),255)
,@startdate=isnull(tempTable.item.value('startdate[1]', 'nvarchar(285)'),'1900-01-01')
,@id=isnull(tempTable.item.value('id[1]', 'bigint'),0)
,@file=isnull(tempTable.item.value('file[1]', 'int'),0)
,@file_name=left(ltrim(rtrim(isnull(tempTable.item.value('file_name[1]', 'nvarchar(285)'),''))),255)
,@file_type=left(ltrim(rtrim(isnull(tempTable.item.value('file_type[1]', 'nvarchar(285)'),''))),255)
,@file_path=left(ltrim(rtrim(isnull(tempTable.item.value('file_path[1]', 'nvarchar(530)'),''))),500)
,@full_path=left(ltrim(rtrim(isnull(tempTable.item.value('full_path[1]', 'nvarchar(1030)'),''))),1000)
,@raw_name=left(ltrim(rtrim(isnull(tempTable.item.value('raw_name[1]', 'nvarchar(285)'),''))),255)
,@orig_name=left(ltrim(rtrim(isnull(tempTable.item.value('orig_name[1]', 'nvarchar(285)'),''))),255)
,@client_name=left(ltrim(rtrim(isnull(tempTable.item.value('client_name[1]', 'nvarchar(285)'),''))),255)
,@file_ext=left(ltrim(rtrim(isnull(tempTable.item.value('file_ext[1]', 'nvarchar(285)'),''))),255)
,@file_size=left(ltrim(rtrim(isnull(tempTable.item.value('file_size[1]', 'nvarchar(285)'),''))),255)
,@is_image=left(ltrim(rtrim(isnull(tempTable.item.value('is_image[1]', 'nvarchar(285)'),''))),255)
,@image_width=left(ltrim(rtrim(isnull(tempTable.item.value('image_width[1]', 'nvarchar(285)'),''))),255)
,@image_height=left(ltrim(rtrim(isnull(tempTable.item.value('image_height[1]', 'nvarchar(285)'),''))),255)
,@image_type=left(ltrim(rtrim(isnull(tempTable.item.value('image_type[1]', 'nvarchar(285)'),''))),255)
,@image_size_str=left(ltrim(rtrim(isnull(tempTable.item.value('image_size_str[1]', 'nvarchar(285)'),''))),255)
FROM @PARAMETER.nodes('param/field') tempTable(item)
set @enddate = DATEADD(Day,6,@startdate)
if DATEPART(weekday,@startdate) != 4
begin
set @xStsno='02';
set @xStsdes= ' Tanggal ' + convert(varchar(10),@startdate,121)+' bukan hari Rabu';
goto f
end
if exists (select * from dt_currencies_header where startdate = @startdate and id != @id)
begin
set @xStsno='02';
set @xStsdes= ' Tanggal ' + convert(varchar(10),@startdate,121)+' sudah ada di databaseaa';
goto f
end
declare @tmp_kurs table (
currencycode varchar(255),
rate decimal(38,4)
)
declare @tmp_kurs2 table (
currenciesid bigint
, rate decimal(18,4)
)
declare @str nvarchar(max) = ''
if @file =1
begin
set @str = @str + 'select F1, convert(decimal(30,4),convert(float,F2)) FROM OPENROWSET('
set @str = @str + '''Microsoft.ACE.OLEDB.12.0'','
set @str = @str + '''Excel 12.0;Database=' + @full_path +';HDR=NO'','
set @str = @str +
'''SELECT * FROM [Sheet1$A2:B]'') A WHERE F1 IS NOT NULL '
insert into @tmp_kurs(currencycode, rate)
exec(@str)
end
declare @currencycode varchar(255)
, @rate decimal(18,4)
, @currenciesid bigint
IF CURSOR_STATUS('global','loopkurs')>=-1
BEGIN
DEALLOCATE loopkurs
END
Declare loopkurs CURSOR FOR SELECT currencycode, rate from @tmp_kurs
OPEN loopkurs
FETCH NEXT FROM loopkurs INTO @currencycode, @rate
WHILE @@FETCH_STATUS = 0
BEGIN
if not exists (select * from dt_currencies_master where currenciescode = @currencycode)
begin
set @xStsno='02';
set @xStsdes= ' Valuta Code ' + convert(varchar(10),@currencycode)+' tidak ada di master';
goto f
end
select @currenciesid = id from dt_currencies_master where currenciescode = @currencycode
if exists (select * from @tmp_kurs2 where currenciesid = @currenciesid)
begin
update @tmp_kurs2 set rate = @rate where currenciesid = @currenciesid
end
else
begin
insert into @tmp_kurs2 (currenciesid, rate)
values (@currenciesid, @rate)
end
FETCH NEXT FROM loopkurs INTO @currencycode, @rate
END
CLOSE loopkurs
DEALLOCATE loopkurs
BEGIN TRANSACTION;
if @id != 0
begin
update dt_currencies_header set no_skep = @no_skep, editby = @data_by, editdate = getdate() where id = @id
MERGE dt_currencies AS A
USING (
Select @id as headerid, currenciesid, rate from @tmp_kurs2
) AS B
ON (A.headerid = B.headerid AND A.currenciesid = B.currenciesid)
WHEN NOT MATCHED THEN
INSERT(headerid, currenciesid, rate, createby, createdate) VALUES(B.headerid, B.currenciesid, B.rate, @data_by, getdate())
WHEN MATCHED THEN
UPDATE SET A.rate = B.rate , A.editby = @data_by, editdate = getdate();
end
else
begin
insert into dt_currencies_header(no_skep, startdate, enddate, createby, createdate)
values (@no_skep, @startdate, @enddate, @data_by, getdate())
set @headerid = SCOPE_IDENTITY()
insert into dt_currencies (headerid, currenciesid, rate, createby, createdate)
select @headerid, currenciesid, rate, @data_by, getdate() from @tmp_kurs2
end
COMMIT;
f:
set nocount off
END TRY
BEGIN CATCH
IF (@@TRANCOUNT > 0) ROLLBACK TRAN;
set @xStsno='88'
set @xStsdes= ERROR_MESSAGE()
END CATCH
g:
set @XmlSts=(select @xStsnoheader + @xStsno as 'no', @xStsdes as 'des' for xml path('sts'))
select CAST( (select @XmlSts,isnull(@raw ,'') for xml path ('result')
) as XML) as dt
GO
Used By
No items found