Update multiple substrings within a string - sql-server

I have a single text string stored in a SQL table which contains all of the text below. The format is XML but the field definition is varchar.
I am using SQL Server 2012 to query this data:
<?xml version="1.0" encoding="utf-16"?>
<SaveFileContext xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<Mappings>
<SaveFileModel Header="Model1" FullPath="Model1" ViewType="ModelView" />
<SaveFileModel Header="xyz" FullPath="\\server\directory\" ViewType="TradeView" />
<SaveFileModel Header="Model2" FullPath="Model2" ViewType="ModelView" />
<SaveFileModel Header="Model3" FullPath="Model3" ViewType="ModelView" />
<SaveFileModel Header="abc" FullPath="\\server\directory\" ViewType="TradeView" />
<SaveFileModel Header="def" FullPath="\\server\directory\" ViewType="TradeView" />
<SaveFileModel Header="ghi" FullPath="\\server\directory\" ViewType="TradeView"/>
</Mappings>
</SaveFileContext>
How can I update or remove the entire lines of text where viewtype="ModelView"?
I want to remove any lines where viewtype="ModelView" within this single string and replace it with a blank space. In the example above I want to remove 3 lines total and leave the rest.
In the end I want the string to look like below (keep in mind all lines are contained in 1 single string. I just separated them to make viewing them easier.
<?xml version="1.0" encoding="utf-16"?>
<SaveFileContext xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<Mappings>
<SaveFileModel Header="xyz" FullPath="\\server\directory\" ViewType="TradeView" />
<SaveFileModel Header="abc" FullPath="\\server\directory\" ViewType="TradeView" />
<SaveFileModel Header="def" FullPath="\\server\directory\" ViewType="TradeView" />
<SaveFileModel Header="ghi" FullPath="\\server\directory\" ViewType="TradeView"/>
</Mappings>
</SaveFileContext>
This is my current query that I am using to replace the lines but the values to replace are very specific. I'm basically entering the string to look for to replace.
IF OBJECT_ID('tempdb..#replacement') IS NOT NULL
DROP TABLE #replacement
CREATE TABLE #replacement (
string_pattern VARCHAR(100),
string_replacement VARCHAR(5)
);
INSERT INTO #replacement (
string_pattern,
string_replacement
)
VALUES
('<SaveFileModel Header="Model1" FullPath="Model1" ViewType="ModelView" />', ''),
('<SaveFileModel Header="Model2" FullPath="Model2" ViewType="ModelView" />', ''),
('<SaveFileModel Header="Model3" FullPath="Model3" ViewType="ModelView" />', ''),
('<SaveFileModel Header="Model4" FullPath="Model4" ViewType="ModelView" />', ''),
('<SaveFileModel Header="Model5" FullPath="Model5" ViewType="ModelView" />', ''),
('<SaveFileModel Header="Model6" FullPath="Model6" ViewType="ModelView" />', ''),
('<SaveFileModel Header="Model7" FullPath="Model7" ViewType="ModelView" />', ''),
('<SaveFileModel Header="Model8" FullPath="Model8" ViewType="ModelView" />', ''),
('<SaveFileModel Header="Model9" FullPath="Model9" ViewType="ModelView" />', ''),
('<SaveFileModel Header="Model10" FullPath="Model10" ViewType="ModelView" />', ''),
('<SaveFileModel Header="Model11" FullPath="Model11" ViewType="ModelView" />', ''),
('<SaveFileModel Header="Model12" FullPath="Model12" ViewType="ModelView" />', ''),
('<SaveFileModel Header="Model13" FullPath="Model13" ViewType="ModelView" />', ''),
('<SaveFileModel Header="Model14" FullPath="Model14" ViewType="ModelView" />', ''),
('<SaveFileModel Header="Model15" FullPath="Model15" ViewType="ModelView" />', ''),
('<SaveFileModel Header="Model16" FullPath="Model16" ViewType="ModelView" />', ''),
('<SaveFileModel Header="Model17" FullPath="Model17" ViewType="ModelView" />', ''),
('<SaveFileModel Header="Model18" FullPath="Model18" ViewType="ModelView" />', ''),
('<SaveFileModel Header="Model19" FullPath="Model19" ViewType="ModelView" />', ''),
('<SaveFileModel Header="Model20" FullPath="Model20" ViewType="ModelView" />', '');
DECLARE #string AS VARCHAR(MAX),
#userident varchar(20);
DECLARE userviewdef CURSOR FOR
SELECT
UserID
FROM
TableSettings where section = 'user view session'
OPEN userviewdef;
FETCH NEXT FROM userviewdef INTO
#userident;
WHILE ##FETCH_STATUS = 0
BEGIN
select #string = varcharvalue from tablesettings where section = 'user view session' and userid = #userident
-- Perform all replacements
SELECT #string = REPLACE(#string, string_pattern, string_replacement) FROM #replacement;
-- Return new string
--PRINT #string;
update tablesettings
set varcharvalue = #string
where section = 'user view session' and userid = #userident
FETCH NEXT FROM userviewdef INTO
#userident;
END;
CLOSE userviewdef;
DEALLOCATE userviewdef;
There are instances where a string could be what is seen below and it would not get removed since it doesn't fit any of the strings I coded to be removed. I want to find an easy way to remove any lines where ViewType="ModelView" since this is my criteria.
<SaveFileModel Header="ThisIsSomethingElse" FullPath="Model1" ViewType="ModelView" />
4/21/21:
After following the advice of #FrankPl to use XML here is what I came up with. The result returns a blank space. I might be coding the XML query piece incorrectly and need a bit of help:
DECLARE #stringXML XML,
#finalString varchar(max),
#userident varchar(20);
DECLARE userviewdef CURSOR FOR
SELECT
UserID
FROM
TableSettings where section = 'user view session';
OPEN userviewdef;
FETCH NEXT FROM userviewdef INTO
#userident;
WHILE ##FETCH_STATUS = 0
BEGIN
select #stringXML = varcharvalue from tablesettings where section = 'user view session' and userid = #userident
select #stringXML.query('
for $item in (/SaveFileContext/Mappings/SaveFileModel)
return if ($item[#ViewType = "ModelView"]) then string($item) else ""
')
select #finalString = convert(varchar(max), #stringXML)
-- Return new string
PRINT #finalstring;
FETCH NEXT FROM userviewdef INTO
#userident;
END;
CLOSE userviewdef;
DEALLOCATE userviewdef;

If you declare your #step variable as data type XML instead of varchar, you can use XQuery to process the data, in this case a FLOWR expression in curly braces within literal XML elements, the curly braces switch from literal mode to XPath mode:
SELECT #string.query('
<SaveFileContext>
<Mappings> {
for $item in (/SaveFileContext/Mappings/SaveFileModel)
return if ($item[#ViewType = "ModelView"]) then $item else ()
} </Mappings>
</SaveFileContext>
'
The XML declaration and the namespaces are swallowed by the SQLServer XML processor.
This solution assumes the data are valid XML fragments.
Please note that - as the whole query needs to be a SQL string (i. e. in single quotes), and XQuery accepts strings in double as well as in single quotes, for simplicity, I used double quotes here. Alternatively, I could have used doubled single quotes to escape them within the SQL string.

Related

Transact sql (2012) Using #xml.modify to insert 'elements using a variable

I need to add an element using a variable
This works --
declare #XML xml = '
<DWDocument>
<FileInfos>
<ImageInfos>
<ImageInfo id="0,0,0" nPages="0">
<FileInfo fileName="9b7b36ac-c705-49ba-bf91-a952bdb44576.eml" dwFileName="f0.eml" type="normal" length="0" />
</ImageInfo>
<ImageInfo id="1,0,0" nPages="0">
<FileInfo fileName="Response to Deficiency Letter.docx" dwFileName="f1.docx" type="normal" length="0" />
</ImageInfo>
</ImageInfos>
</FileInfos>
</DWDocument>
'
SET #xml.modify ('insert <ImageInfo id="2,0,0" nPages="0"></ImageInfo>
as last into (DWDocument/FileInfos/ImageInfos)[1]')
Gives me:
<ImageInfo id="2,0,0" nPages="0" /><ImageInfo>
BUT...
I need the '2' to be a variable
I tried:
declare #XML xml = '
<DWDocument>
<FileInfos>
<ImageInfos>
<ImageInfo id="0,0,0" nPages="0">
<FileInfo fileName="9b7b36ac-c705-49ba-bf91-a952bdb44576.eml" dwFileName="f0.eml" type="normal" length="0" />
</ImageInfo>
<ImageInfo id="1,0,0" nPages="0">
<FileInfo fileName="Response to Deficiency Letter.docx" dwFileName="f1.docx" type="normal" length="0" />
</ImageInfo>
</ImageInfos>
</FileInfos>
</DWDocument>
'
declare #ImageInfo_ID nvarchar(50) = '"2,0,0"'
declare #ImageInfo_nPages nvarchar(50) = '"0"'
SET #xml.modify ('insert <ImageInfo id={sql:variable("#ImageInfo_ID")}></ImageInfo>
as last into (DWDocument/FileInfos/ImageInfos)[1]')
gives me the error:
Msg 2225, Level 16, State 1, Line 46
XQuery [modify()]: A string literal was expected
SET #xml.modify ('insert <ImageInfo id{sql:variable("#ImageInfo_ID")}></ImageInfo>
as last into (DWDocument/FileInfos/ImageInfos)[1]')
gives me the error
Msg 2205, Level 16, State 1, Line 48
XQuery [modify()]: "=" was expected.
SET #xml.modify ('insert <ImageInfo>{sql:variable("#ImageInfo_ID")}</ImageInfo>
as last into (DWDocument/FileInfos/ImageInfos)[1]')
has no error but the data is wrong, gives me
<ImageInfo>"2,0,0" nPages="0"</ImageInfo>
the 'id=' is missing
How do I specify the attribute name? id=#ImageInfo_ID?
What I REALLY NEED is
<ImageInfo id=#ImageInfo_ID nPages=#ImageInfo_nPages></ImageInfo>
to get to
<ImageInfo id="2,0,0" nPages="0"></ImageInfo>
What am I missing?
To specify the attribute name and values using variables in the XQuery expression of the modify method, you can use the sql:variable function to concatenate the attribute name and value as a string. Here's an example that should work for you:
DECLARE #ImageInfo_ID NVARCHAR(50) = '2,0,0'
DECLARE #ImageInfo_nPages NVARCHAR(50) = '0'
SET #xml.modify('
insert <ImageInfo id={sql:variable("#ImageInfo_ID")} nPages={sql:variable("#ImageInfo_nPages")}></ImageInfo>
as last into (DWDocument/FileInfos/ImageInfos)[1]'
)
Please try the following solution.
It is using XQuery FLWOR expression to compose needed XML.
SQL
DECLARE #xml XML =
N'<DWDocument>
<FileInfos>
<ImageInfos>
<ImageInfo id="0,0,0" nPages="0">
<FileInfo fileName="9b7b36ac-c705-49ba-bf91-a952bdb44576.eml" dwFileName="f0.eml" type="normal" length="0"/>
</ImageInfo>
<ImageInfo id="1,0,0" nPages="0">
<FileInfo fileName="Response to Deficiency Letter.docx" dwFileName="f1.docx" type="normal" length="0"/>
</ImageInfo>
</ImageInfos>
</FileInfos>
</DWDocument>';
DECLARE #ImageInfo_ID NVARCHAR(50) = '2,0,0';
DECLARE #ImageInfo_nPages NVARCHAR(50) = '0';
SET #xml = #xml.query('<DWDocument><FileInfos><ImageInfos>
{
for $x in //DWDocument/FileInfos/ImageInfos
return if ($x[position() lt last()]) then $x
else $x, <ImageInfo id="{sql:variable("#ImageInfo_ID")}" nPages="{sql:variable("#ImageInfo_nPages")}"></ImageInfo>
}
</ImageInfos></FileInfos></DWDocument>');
-- test
SELECT #xml;
Output
<DWDocument>
<FileInfos>
<ImageInfos>
<ImageInfos>
<ImageInfo id="0,0,0" nPages="0">
<FileInfo fileName="9b7b36ac-c705-49ba-bf91-a952bdb44576.eml" dwFileName="f0.eml" type="normal" length="0" />
</ImageInfo>
<ImageInfo id="1,0,0" nPages="0">
<FileInfo fileName="Response to Deficiency Letter.docx" dwFileName="f1.docx" type="normal" length="0" />
</ImageInfo>
</ImageInfos>
<ImageInfo id="2,0,0" nPages="0" />
</ImageInfos>
</FileInfos>
</DWDocument>

SQL XML - Create a XML file from SQL Server for an invoice including invoice positions in one XML file

I create my xml file in this way (I do not show all output fields because there are very many fields):
DECLARE #ID_Rechnung int = 8;
WITH XMLNAMESPACES (
'urn:oasis:names:specification:ubl:schema:xsd:CommonExtensionComponents-2' as ext,
'urn:oasis:names:specification:ubl:schema:xsd:CommonBasicComponents-2' as cbc,
'urn:oasis:names:specification:ubl:schema:xsd:CommonAggregateComponents-2' as cac,
'http://uri.etsi.org/01903/v1.3.2#' as xades,
'http://www.w3.org/2001/XMLSchema-instance' as xsi,
'http://www.w3.org/2000/09/xmldsig#' as ds
)
SELECT
#XMLData = xmldat.xmldataCol
FROM
(
SELECT
(
SELECT
-- HIER XML Daten generieren
'' AS 'ext:UBLExtensions',
'' AS 'ext:UBLExtensions/ext:UBLExtension',
'' AS 'ext:UBLExtensions/ext:UBLExtension/ext:ExtensionContent',
'2.1' AS 'cbc:UBLVersionID',
'TR1.2' AS 'cbc:CustomizationID',
'' AS 'cbc:ProfileID',
Rechnungen.Nummer AS 'cbc:ID',
'false' AS 'cbc:CopyIndicator',
'' AS 'cbc:UUID',
CAST(Rechnungen.Datum AS Date) AS 'cbc:IssueDate'
FROM
rechnungen
WHERE
rechnungen.id = #ID_Rechnung
FOR XML PATH(''), ROOT('Invoice')
) AS xmldataCol
This works fine - i get the following XML:
<Invoice xmlns:ds="http://www.w3.org/2000/09/xmldsig#" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xades="http://uri.etsi.org/01903/v1.3.2#" xmlns:cac="urn:oasis:names:specification:ubl:schema:xsd:CommonAggregateComponents-2" xmlns:cbc="urn:oasis:names:specification:ubl:schema:xsd:CommonBasicComponents-2" xmlns:ext="urn:oasis:names:specification:ubl:schema:xsd:CommonExtensionComponents-2">
<ext:UBLExtensions>
<ext:UBLExtension>
<ext:ExtensionContent />
</ext:UBLExtension>
</ext:UBLExtensions>
<cbc:UBLVersionID>2.1</cbc:UBLVersionID>
<cbc:CustomizationID>TR1.2</cbc:CustomizationID>
<cbc:ProfileID />
<cbc:ID>R200001</cbc:ID>
<cbc:CopyIndicator>false</cbc:CopyIndicator>
<cbc:UUID />
<cbc:IssueDate>2020-06-29</cbc:IssueDate>
</Invoice>
But now i need the invoice positions in the same file.
This SQL should be included in the first one and the date should be as invoice line in the xml file:
SELECT
Rechnungpos.ID AS 'cac:InvoiceLine/cbc:ID',
Rechnungpos.Anzahl AS 'cac:InvoiceLine/cbc:InvoicedQuantity'
FROM
RechnungPos
WHERE
RechnungPos.id_Rechnung = #ID_Rechnung
The output should be this:
<Invoice xmlns:ds="http://www.w3.org/2000/09/xmldsig#" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xades="http://uri.etsi.org/01903/v1.3.2#" xmlns:cac="urn:oasis:names:specification:ubl:schema:xsd:CommonAggregateComponents-2" xmlns:cbc="urn:oasis:names:specification:ubl:schema:xsd:CommonBasicComponents-2" xmlns:ext="urn:oasis:names:specification:ubl:schema:xsd:CommonExtensionComponents-2">
<ext:UBLExtensions>
<ext:UBLExtension>
<ext:ExtensionContent />
</ext:UBLExtension>
</ext:UBLExtensions>
<cbc:UBLVersionID>2.1</cbc:UBLVersionID>
<cbc:CustomizationID>TR1.2</cbc:CustomizationID>
<cbc:ProfileID />
<cbc:ID>R200001</cbc:ID>
<cbc:CopyIndicator>false</cbc:CopyIndicator>
<cbc:UUID />
<cbc:IssueDate>2020-06-29</cbc:IssueDate>
<cac:InvoiceLine>
<cbc:ID>1<(cbc:>
<cbc:InvoicedQuantity>3</cbc:InvoicedQuantity>
</cac:InvoiceLine>
<cac:InvoiceLine>
<cbc:ID>5<(cbc:>
<cbc:InvoicedQuantity>1</cbc:InvoicedQuantity>
</cac:InvoiceLine>
<cac:InvoiceLine>
<cbc:ID>9<(cbc:>
<cbc:InvoicedQuantity>2</cbc:InvoicedQuantity>
</cac:InvoiceLine>
</Invoice>
Here is the Code to generate Test Data:
CREATE TABLE [dbo].[Rechnungen](
[id] [int] NOT NULL,
[Nummer] [nvarchar](20) NOT NULL,
[Datum] [datetime] NOT NULL
)
INSERT INTO Rechnungen (id, Nummer, Datum) VALUES (8, 'R200001', '29.06.2020')
CREATE TABLE [dbo].Rechnungpos(
[id] [int] NOT NULL,
[id_Rechnung] [int] NOT NULL,
[Anzahl] [float] NOT NULL
)
INSERT INTO RechnungPos (id, id_Rechnung, Anzahl) VALUES (1, 8, 3)
INSERT INTO RechnungPos (id, id_Rechnung, Anzahl) VALUES (5, 8, 1)
INSERT INTO RechnungPos (id, id_Rechnung, Anzahl) VALUES (9, 8, 2)
it has to run on different versions - my version is SQL Server 2019
How can i do that?
Thanks for help, Thomas.
Here is how to do it.
The only complexity is how to handle a child table and specially namespaces in the output XML. That's why XQuery and its FLWOR expression are in use.
SQL
-- DDL and sample data population, start
DECLARE #Rechnungen TABLE (id int , Nummer nvarchar(20), Datum datetime);
INSERT INTO #Rechnungen (id, Nummer, Datum) VALUES
(8, 'R200001', '2020-06-29');
DECLARE #Rechnungpos TABLE (id int, id_Rechnung int, Anzahl float);
INSERT INTO #RechnungPos (id, id_Rechnung, Anzahl) VALUES
(1, 8, 3),
(5, 8, 1),
(9, 8, 2);
-- DDL and sample data population, end
DECLARE #ID_Rechnung int = 8;
;WITH XMLNAMESPACES ('urn:oasis:names:specification:ubl:schema:xsd:CommonExtensionComponents-2' as ext
, 'urn:oasis:names:specification:ubl:schema:xsd:CommonBasicComponents-2' as cbc
, 'urn:oasis:names:specification:ubl:schema:xsd:CommonAggregateComponents-2' as cac
, 'http://uri.etsi.org/01903/v1.3.2#' as xades
, 'http://www.w3.org/2001/XMLSchema-instance' as xsi
, 'http://www.w3.org/2000/09/xmldsig#' as ds)
SELECT (
SELECT '2.1' AS [cbc:UBLVersionID],
'TR1.2' AS [cbc:CustomizationID],
'' AS [cbc:ProfileID],
p.Nummer AS [cbc:ID],
'false' AS [cbc:CopyIndicator],
'' AS [cbc:UUID],
CAST(p.Datum AS Date) AS [cbc:IssueDate],
(
SELECT c.id AS [cbc:ID]
, CAST(c.Anzahl AS INT) AS [cbc:InvoicedQuantity]
FROM #Rechnungpos AS c INNER JOIN
#Rechnungen AS p ON p.id = c.id_Rechnung
FOR XML PATH('r'), TYPE, ROOT('root')
)
FROM #Rechnungen AS p
WHERE p.id = #ID_Rechnung
FOR XML PATH(''), TYPE, ROOT('Invoice')
).query('<Invoice xmlns:ds="http://www.w3.org/2000/09/xmldsig#"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xades="http://uri.etsi.org/01903/v1.3.2#"
xmlns:cac="urn:oasis:names:specification:ubl:schema:xsd:CommonAggregateComponents-2"
xmlns:cbc="urn:oasis:names:specification:ubl:schema:xsd:CommonBasicComponents-2"
xmlns:ext="urn:oasis:names:specification:ubl:schema:xsd:CommonExtensionComponents-2">
<ext:UBLExtensions>
<ext:UBLExtension>
<ext:ExtensionContent/>
</ext:UBLExtension>
</ext:UBLExtensions>
{
for $x in /Invoice/*[local-name()!="root"]
return $x,
for $x in /Invoice/root/r
return <cac:InvoiceLine>{$x/*}</cac:InvoiceLine>
}
</Invoice>');
Output
<Invoice xmlns:ds="http://www.w3.org/2000/09/xmldsig#" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xades="http://uri.etsi.org/01903/v1.3.2#" xmlns:cac="urn:oasis:names:specification:ubl:schema:xsd:CommonAggregateComponents-2" xmlns:cbc="urn:oasis:names:specification:ubl:schema:xsd:CommonBasicComponents-2" xmlns:ext="urn:oasis:names:specification:ubl:schema:xsd:CommonExtensionComponents-2">
<ext:UBLExtensions>
<ext:UBLExtension>
<ext:ExtensionContent />
</ext:UBLExtension>
</ext:UBLExtensions>
<cbc:UBLVersionID>2.1</cbc:UBLVersionID>
<cbc:CustomizationID>TR1.2</cbc:CustomizationID>
<cbc:ProfileID />
<cbc:ID>R200001</cbc:ID>
<cbc:CopyIndicator>false</cbc:CopyIndicator>
<cbc:UUID />
<cbc:IssueDate>2020-06-29</cbc:IssueDate>
<cac:InvoiceLine>
<cbc:ID>1</cbc:ID>
<cbc:InvoicedQuantity>3</cbc:InvoicedQuantity>
</cac:InvoiceLine>
<cac:InvoiceLine>
<cbc:ID>5</cbc:ID>
<cbc:InvoicedQuantity>1</cbc:InvoicedQuantity>
</cac:InvoiceLine>
<cac:InvoiceLine>
<cbc:ID>9</cbc:ID>
<cbc:InvoicedQuantity>2</cbc:InvoicedQuantity>
</cac:InvoiceLine>
</Invoice>
Thanks, Yitzhak Khabinsky for help - heres the complete solution:
DECLARE #ID_Rechnung int = 8,
#XMLData xml;
WITH XMLNAMESPACES ('urn:oasis:names:specification:ubl:schema:xsd:CommonExtensionComponents-2' as ext
, 'urn:oasis:names:specification:ubl:schema:xsd:CommonBasicComponents-2' as cbc
, 'urn:oasis:names:specification:ubl:schema:xsd:CommonAggregateComponents-2' as cac
, 'http://uri.etsi.org/01903/v1.3.2#' as xades
, 'http://www.w3.org/2001/XMLSchema-instance' as xsi
, 'http://www.w3.org/2000/09/xmldsig#' as ds)
SELECT
#XMLData = xmldat.xmldataCol
FROM
(
SELECT (
SELECT
-- HIER XML Daten generieren
'' AS 'ext:UBLExtensions',
'' AS 'ext:UBLExtensions/ext:UBLExtension',
'' AS 'ext:UBLExtensions/ext:UBLExtension/ext:ExtensionContent',
'2.1' AS 'cbc:UBLVersionID',
'TR1.2' AS 'cbc:CustomizationID',
'' AS 'cbc:ProfileID',
Rechnungen.Nummer AS 'cbc:ID',
'false' AS 'cbc:CopyIndicator',
'' AS 'cbc:UUID',
CAST(Rechnungen.Datum AS Date) AS 'cbc:IssueDate',
'' AS 'cbc:InvoiceTypeCode',
Rechnungen.Bemerkung1 AS 'cbc:Note',
#Waehrung AS 'cbc:DocumentCurrencyCode',
#Waehrung AS 'cbc:TaxCurrencyCode',
Rechnungen.Auftrag AS 'cac:OrderReference/cbc:ID',
-- Verkaüfer
'' AS 'cac:AccountingSupplierParty/cac:Party/cbc:EndpointID',
'' AS 'cac:AccountingSupplierParty/cac:Party/cac:PartyIdentification/cbc:ID',
'' AS 'cac:AccountingSupplierParty/cac:Party/cac:PartyName/cbc:Name',
'' AS 'cac:AccountingSupplierParty/cac:Party/cac:PostalAddress/cbc:StreetName',
'' AS 'cac:AccountingSupplierParty/cac:Party/cac:PostalAddress/cbc:CityName',
'' AS 'cac:AccountingSupplierParty/cac:Party/cac:PostalAddress/cbc:PostalZone',
'' AS 'cac:AccountingSupplierParty/cac:Party/cac:PostalAddress/cac:Country/cbc:IdentificationCode',
'' AS 'cac:AccountingSupplierParty/cac:Party/cac:PartyTaxScheme/cbc:CompanyId',
'VAT' AS 'cac:AccountingSupplierParty/cac:Party/cac:PartyTaxScheme/cac:TaxScheme/cbc:ID',
'' AS 'cac:AccountingSupplierParty/cac:Party/cac:PartyLegalEntity/cbc:RegistrationName',
'' AS 'cac:AccountingSupplierParty/cac:Party/cac:PartyLegalEntity/cbc:CompanyID',
'' AS 'cac:AccountingSupplierParty/cac:Party/cac:PartyLegalEntity/cbc:CompanyLegalForm',
'' AS 'cac:AccountingSupplierParty/cac:Party/cac:Contact/cbc:Name',
'' AS 'cac:AccountingSupplierParty/cac:Party/cac:Contact/cbc:Telephone',
'' AS 'cac:AccountingSupplierParty/cac:Party/cac:Contact/cbc:ElectronicMail',
-- Käufer
'' AS 'cac:AccountingCustomerParty/cac:Party/cbc:EndpointID',
Rechnungen.DebKreNr AS 'cac:AccountingCustomerParty/cac:Party/cac:PartyIdentification/cbc:ID',
Rechnungen.DebBez01 + ' ' + DebBez02 AS 'cac:AccountingCustomerParty/cac:Party/cac:PartyName/cbc:Name',
Rechnungen.DebStrasse AS 'cac:AccountingCustomerParty/cac:Party/cac:PostalAddress/cbc:StreetName',
Rechnungen.DebOrt AS 'cac:AccountingCustomerParty/cac:Party/cac:PostalAddress/cbc:CityName',
Rechnungen.DebPLZ AS 'cac:AccountingCustomerParty/cac:Party/cac:PostalAddress/cbc:PostalZone',
Rechnungen.DebLandKFZ AS 'cac:AccountingCustomerParty/cac:Party/cac:PostalAddress/cac:Country/cbc:IdentificationCode',
Rechnungen.DebUMSTID AS 'cac:AccountingCustomerParty/cac:Party/cac:PartyTaxScheme/cbc:CompanyID',
'VAT' AS 'cac:AccountingCustomerParty/cac:Party/cac:PartyTaxScheme/cac:TaxScheme/cbc:ID',
Rechnungen.DebBez01 + ' ' + DebBez02 AS 'cac:AccountingCustomerParty/cac:Party/cac:PartyLegalEntity/cbc:RegistrationName',
'' AS 'cac:AccountingCustomerParty/cac:Party/cac:Contact/cbc:Name',
'' AS 'cac:AccountingCustomerParty/cac:Party/cac:Contact/cbc:Telephone',
'' AS 'cac:AccountingCustomerParty/cac:Party/cac:Contact/cbc:ElectronicMail',
-- Kontoverbindung Verkäufer
'' AS 'cac:PaymentMeans/cbc:PaymentMeansCode',
'' AS 'cac:PaymentMeans/cac:PayeeFinancialAccount/cbc:ID',
'' AS 'cac:PaymentMeans/cac:PayeeFinancialAccount/cbc:Name',
'' AS 'cac:PaymentMeans/cac:PayeeFinancialAccount/cac:FinancialInstitutionBranch/cbc:ID',
--'' AS 'cac:PaymentTerms/cbc:Note',
-- Steuern
#Waehrung AS 'cac:TaxTotal/cbc_TaxAmount/#currencyID',
CAST(Rechnungen.BetragMWST AS nvarchar(15)) AS 'cac:TaxTotal/cbc_TaxAmount',
#Waehrung AS 'cac:TaxTotal/cac:Taxubtotal/cbc:TaxableAmount/#currencyID',
CAST(Rechnungen.BetragNetto AS nvarchar(15))AS 'cac:TaxTotal/cac:Taxubtotal/cbc:TaxableAmount',
#Waehrung AS 'cac:TaxTotal/cac:Taxubtotal/cbc:TaxAmount/#currencyID',
CAST(Rechnungen.BetragMWST AS nvarchar(15))AS 'cac:TaxTotal/cac:Taxubtotal/cbc:TaxAmount',
'' AS 'cac:TaxTotal/cac:Taxubtotal/cac:TaxCategory/cbc:ID',
CAST(Rechnungen.MWST AS nvarchar(2)) AS 'cac:TaxTotal/cac:Taxubtotal/cac:TaxCategory/cbc:Percent',
'VAT' AS 'cac:TaxTotal/cac:Taxubtotal/cac:TaxCategory/cac:TaxScheme/cbc:ID',
#Waehrung AS 'cac:LegalMonetaryTotal/cbc:LineExtensionAmount/#currencyID',
CAST(Rechnungen.BetragNetto AS nvarchar(15))AS 'cac:LegalMonetaryTotal/cbc:LineExtensionAmount',
#Waehrung AS 'cac:LegalMonetaryTotal/cbc:TaxExclusiveAmount/#currencyID',
CAST(Rechnungen.BetragNetto AS nvarchar(15))AS 'cac:LegalMonetaryTotal/cbc:TaxExclusiveAmount',
#Waehrung AS 'cac:LegalMonetaryTotal/cbc:TaxInclusiveAmount/#currencyID',
CAST(Rechnungen.BetragBrutto AS nvarchar(15))AS 'cac:LegalMonetaryTotal/cbc:TaxInclusiveAmount',
#Waehrung AS 'cac:LegalMonetaryTotal/cbc:PayableAmount/#currencyID',
CAST(Rechnungen.BetragBrutto AS nvarchar(15))AS 'cac:LegalMonetaryTotal/cbc:PayableAmount',
(
SELECT Rechnungpos.id AS [cbc:ID]
, CAST(Rechnungpos.Anzahl AS INT) AS [cbc:InvoicedQuantity]
FROM Rechnungpos WHERE RechnungPos.id_Rechnung = #id_Rechnung
FOR XML PATH('r'), TYPE, ROOT('root')
)
FROM Rechnungen
WHERE Rechnungen.id = #ID_Rechnung
FOR XML PATH(''), TYPE, ROOT('Invoice')
) AS xmldataCol
) AS xmldat;
SELECT #XMLData
.query('<Invoice xmlns:ds="http://www.w3.org/2000/09/xmldsig#"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xades="http://uri.etsi.org/01903/v1.3.2#"
xmlns:cac="urn:oasis:names:specification:ubl:schema:xsd:CommonAggregateComponents-2"
xmlns:cbc="urn:oasis:names:specification:ubl:schema:xsd:CommonBasicComponents-2"
xmlns:ext="urn:oasis:names:specification:ubl:schema:xsd:CommonExtensionComponents-2">
{
for $x in /Invoice/*[local-name()!="root"]
return $x,
for $x in /Invoice/root/r
return <cac:InvoiceLine>{$x/*}</cac:InvoiceLine>
}
</Invoice>');

Snowflake Pandas connection issue

I'm trying to insert a dataframe into a snowflake table using the pandas connector and am getting permission issues, but using the "normal" snowflake connector works fine.
import snowflake.connector as snow
from snowflake.connector.pandas_tools import *
cur = self.conn.cursor()
my_schema = "my_schema"
my_table = "my_table"
cur.execute(f"""INSERT INTO {my_schema}.{my_table}(load_date, some_id)
values (current_timestamp, 'xxx')""")
write_pandas(self.conn, daily_epc_df, table_name=my_table, schema=my_schema)
But I'm getting
File "/Users/abohr/virtualenv/peak38/lib/python3.8/site-packages/snowflake/connector/errors.py", line 85, in default_errorhandler
raise error_class(
snowflake.connector.errors.ProgrammingError: 001757 (42601): SQL compilation error:
Table '"my_schema"."my_table"' does not exist
the same connection can insert and then doesn't work on the same table.
I also tried
df.to_sql(..., method=pd_writer)
and get
pandas.io.sql.DatabaseError: Execution failed on sql 'SELECT name FROM sqlite_master WHERE type='table' AND name=?;': not all arguments converted during string formatting
Why is it talking about sqlite_master if I'm trying to connect to Snowflake? Do the pandas functions require different connections?
my libs:
name = "snowflake-connector-python"
version = "2.3.3"
description = "Snowflake Connector for Python"
category = "main"
optional = false
python-versions = ">=3.5"
[[package]]
name = "pandas"
version = "1.0.5"
description = "Powerful data structures for data analysis, time series, and statistics"
category = "main"
optional = false
python-versions = ">=3.6.1"```
on Python 3.8
I found my issue - I added quote_identifiers=False to
write_pandas(self.conn, daily_epc_df, table_name=my_table, schema=my_schema, quote_identifiers=False)
and now it works.
But seems very wrong that the default behavior would break, I'm still suspicious.
Hmm I've done something superior I think to pandas ...
Tell me if you agree ...
CREATE OR REPLACE PROCEDURE DEMO_DB.PUBLIC.SNOWBALL(
db_name STRING,
schema_name STRING,
snowball_table STRING,
max_age_days FLOAT,
limit FLOAT
)
RETURNS VARIANT
LANGUAGE JAVASCRIPT
COMMENT = 'Collects table and column stats.'
EXECUTE AS OWNER
AS
$$
var validLimit = Math.max(LIMIT, 0); // prevent SQL syntax error caused by negative numbers
var sqlGenerateInserts = `
WITH snowball_tables AS (
SELECT CONCAT_WS('.', table_catalog, table_schema, table_name) AS full_table_name, *
FROM IDENTIFIER(?) -- <<DB_NAME>>.INFORMATION_SCHEMA.TABLES
),
snowball_columns AS (
SELECT CONCAT_WS('.', table_catalog, table_schema, table_name) AS full_table_name, *
FROM IDENTIFIER(?) -- <<DB_NAME>>.INFORMATION_SCHEMA.COLUMNS
),
snowball AS (
SELECT table_name, MAX(stats_run_date_time) AS stats_run_date_time
FROM IDENTIFIER(?) -- <<SNOWBALL_TABLE>> table
GROUP BY table_name
)
SELECT full_table_name, aprox_row_count,
CONCAT (
'INSERT INTO IDENTIFIER(''', ?, ''') ', -- SNOWBALL table
'(table_name,total_rows,table_last_altered,table_created,table_bytes,col_name,',
'col_data_type,col_hll,col_avg_length,col_null_cnt,col_min,col_max,col_top,col_mode,col_avg,stats_run_date_time)',
'SELECT ''', full_table_name, ''' AS table_name, ',
table_stats_sql,
', ARRAY_CONSTRUCT( ', col_name, ') AS col_name',
', ARRAY_CONSTRUCT( ', col_data_type, ') AS col_data_type',
', ARRAY_CONSTRUCT( ', col_hll, ') AS col_hll',
', ARRAY_CONSTRUCT( ', col_avg_length, ') AS col_avg_length',
', ARRAY_CONSTRUCT( ', col_null_cnt, ') AS col_null_cnt',
', ARRAY_CONSTRUCT( ', col_min, ') AS col_min',
', ARRAY_CONSTRUCT( ', col_max, ') AS col_max',
', ARRAY_CONSTRUCT( ', col_top, ') AS col_top',
', ARRAY_CONSTRUCT( ', col_MODE, ') AS col_MODE',
', ARRAY_CONSTRUCT( ', col_AVG, ') AS col_AVG',
', CURRENT_TIMESTAMP() AS stats_run_date_time ',
' FROM ', quoted_table_name
) AS insert_sql
FROM (
SELECT
tbl.full_table_name,
tbl.row_count AS aprox_row_count,
CONCAT ( '"', col.table_catalog, '"."', col.table_schema, '"."', col.table_name, '"' ) AS quoted_table_name,
CONCAT (
'COUNT(1) AS total_rows,''',
IFNULL( tbl.last_altered::VARCHAR, 'NULL'), ''' AS table_last_altered,''',
IFNULL( tbl.created::VARCHAR, 'NULL'), ''' AS table_created,',
IFNULL( tbl.bytes::VARCHAR, 'NULL'), ' AS table_bytes' ) AS table_stats_sql,
LISTAGG (
CONCAT ('''', col.full_table_name, '.', col.column_name, '''' ), ', '
) AS col_name,
LISTAGG ( CONCAT('''', col.data_type, '''' ), ', ' ) AS col_data_type,
LISTAGG ( CONCAT( ' HLL(', '"', col.column_name, '"',') ' ), ', ' ) AS col_hll,
LISTAGG ( CONCAT( ' AVG(ZEROIFNULL(LENGTH(', '"', col.column_name, '"','))) ' ), ', ' ) AS col_avg_length,
LISTAGG ( CONCAT( ' SUM( IFF( ', '"', col.column_name, '"',' IS NULL, 1, 0) ) ' ), ', ') AS col_null_cnt,
LISTAGG ( IFF ( col.data_type = 'NUMBER', CONCAT ( ' MODE(', '"', col.column_name, '"', ') ' ), 'NULL' ), ', ' ) AS col_MODE,
LISTAGG ( IFF ( col.data_type = 'NUMBER', CONCAT ( ' MIN(', '"', col.column_name, '"', ') ' ), 'NULL' ), ', ' ) AS col_min,
LISTAGG ( IFF ( col.data_type = 'NUMBER', CONCAT ( ' MAX(', '"', col.column_name, '"', ') ' ), 'NULL' ), ', ' ) AS col_max,
LISTAGG ( IFF ( col.data_type = 'NUMBER', CONCAT ( ' AVG(', '"', col.column_name,'"',') ' ), 'NULL' ), ', ' ) AS col_AVG,
LISTAGG ( CONCAT ( ' APPROX_TOP_K(', '"', col.column_name, '"', ', 100, 10000)' ), ', ' ) AS col_top
FROM snowball_tables tbl JOIN snowball_columns col ON col.full_table_name = tbl.full_table_name
LEFT OUTER JOIN snowball sb ON sb.table_name = tbl.full_table_name
WHERE (tbl.table_catalog, tbl.table_schema) = (?, ?)
AND ( sb.table_name IS NULL OR sb.stats_run_date_time < TIMESTAMPADD(DAY, - FLOOR(?), CURRENT_TIMESTAMP()) )
--AND tbl.row_count > 0 -- NB: also excludes views (table_type = 'VIEW')
GROUP BY tbl.full_table_name, aprox_row_count, quoted_table_name, table_stats_sql, stats_run_date_time
ORDER BY stats_run_date_time NULLS FIRST )
LIMIT ` + validLimit;
var tablesAnalysed = [];
var currentSql;
try {
currentSql = sqlGenerateInserts;
var generateInserts = snowflake.createStatement( {
sqlText: currentSql,
binds: [
`"${DB_NAME}".information_schema.tables`,
`"${DB_NAME}".information_schema.columns`,
SNOWBALL_TABLE, SNOWBALL_TABLE,
DB_NAME, SCHEMA_NAME, MAX_AGE_DAYS, LIMIT
]
} );
var insertStatements = generateInserts.execute();
// loop over generated INSERT statements and execute them
while (insertStatements.next()) {
var tableName = insertStatements.getColumnValue('FULL_TABLE_NAME');
currentSql = insertStatements.getColumnValue('INSERT_SQL');
var insertStatement = snowflake.createStatement( {
sqlText: currentSql,
binds: [ SNOWBALL_TABLE ]
} );
var insertResult = insertStatement.execute();
tablesAnalysed.push(tableName);
}
return { result: "SUCCESS", analysedTables: tablesAnalysed };
}
catch (err) {
return {
error: err,
analysedTables: tablesAnalysed,
sql: currentSql
};
}
$$;

T-SQL function to convert numeric to words?

I am struggling to build a function that can convert a number 0-9 in a string into the spelling word of it. Here is what I have so far, and I realize 'word' is not a built in conversion, it's just what my thoughts on how to construct this would be:
CREATE FUNCTION [dbo].[fn_Numbers2Words]
(#strText VARCHAR(1000))
RETURNS varchar(1000)
AS
BEGIN
WHILE PATINDEX('%[0-9]%', #strText) > 0
BEGIN
SET #strText = STUFF(#strText, PATINDEX('%[0-9]%', #strText), 1, CONVERT(word, PATINDEX('%[0-9]%', #strText)))
END
RETURN #strText
END
The idea would be to input something like this:
SELECT [dbo].[fn_Numbers2Word]('1900testnumber')
and return this:
'oneninezerozerotestnumber'
I have tried functions that do entire numbers, but since my strings will have alphas they do not work. I also tried incorporating those functions into this function above with no luck. I'm sure it's just something I'm doing syntax wise.
Can anyone help me alter my above function so that it produces my desired result?
Since you are just wanting a simple replacement then using nested replace would super crazy fast and simple. I would avoid using a scalar function here.
declare #strText varchar(1000) = '1900testnumber'
select replace(replace(replace(replace(replace(replace(replace(replace(replace(replace(#strText, '1', ' one'), '2', 'two'), '3', 'three'), '4', 'four'), '5', 'five'), '6', 'six'), '7', 'seven'), '8', 'eight'), '9', 'nine'), '0', 'zero')
Just for fun, here's another option that you could put into a function
Example
Declare #S varchar(max) = '1900testnumber'
Select #S = replace(#S,sFrom,sTo)
From (values ('0','zero')
,('1','one')
,('2','two')
,('3','three')
,('4','four')
,('5','five')
,('6','six')
,('7','seven')
,('8','eight')
,('9','nine')
) A(sFrom,sTo)
Select #S
Returns
oneninezerozerotestnumber
Brute force with CHOOSE:
Returns the item at the specified index from a list of values in SQL Server.
DECLARE #num INT = 20;
SELECT CHOOSE(#num,
'one ',
'two ',
'three ',
'four ',
'five ',
'six ',
'seven ',
'eight ',
'nine ',
'ten',
'eleven ',
'twelve ',
'thirteen ',
'fourteen ',
'fifteen ',
'sixteen ',
'seventeen ',
'eighteen ',
'nineteen ',
'twenty',
'twenty-one ',
'twenty-two ',
'twenty-three ',
'twenty-four ',
'twenty-five ',
'twenty-six ',
'twenty-seven ',
'twenty-eight ',
'twenty-nine ',
'thirty',
'thirty-one ',
'thirty-two ',
'thirty-three',
'thirty-four ',
'thirty-five ',
'thirty-six ',
'thirty-seven ',
'thirty-eight ',
'thirty-nine ',
'forty',
'forty-one ',
'forty-two ',
'forty-three ',
'forty-four ',
'forty-five ',
'forty-six ',
'forty-seven ',
'forty-eight ',
'forty-nine ',
'fifty',
'fifty-one ',
'fifty-two ',
'fifty-three ',
'fifty-four ',
'fifty-five ',
'fifty-six ',
'fifty-seven ',
'fifty-eight ',
'fifty-nine ',
'sixty',
'sixty-one ',
'sixty-two ',
'sixty-three ',
'sixty-four ',
'sixty-five ',
'sixty-six ',
'sixty-seven ',
'sixty-eight ',
'sixty-nine ',
'seventy',
'seventy-one ',
'seventy-two ',
'seventy-three ',
'seventy-four ',
'seventy-five ',
'seventy-six ',
'seventy-seven ',
'seventy-eight ',
'seventy-nine ',
'eighty',
'eighty-one ',
'eighty-two ',
'eighty-three ',
'eighty-four ',
'eighty-five ',
'eighty-six ',
'eighty-seven ',
'eighty-eight ',
'eighty-nine ',
'ninety',
'ninety-one ',
'ninety-two ',
'ninety-three ',
'ninety-four ',
'ninety-five ',
'ninety-six ',
'ninety-seven ',
'ninety-eight ',
'ninety-nine'
)
DBFiddle Demo

sql server query concat string with '-'

In my SQL query, I'm trying to concatenate two strings in my select clause. Here's the expected results:
col A col B Result
null null &
null '' &
null XYZ XYC
'' null &
'' '' &
'' XYZ XYC
ABC null ABC
ABC '' ABC
ABC XYZ ABC-XYC
My challenge is this - how do I get the 'dash' to show up for the last scenario and not the others?
Here's my attempt:
DECLARE #ColA as varchar(10)
DECLARE #ColB as varchar(10)
set #ColA = null; set #ColB = null; select '&' as 'Expected', COALESCE(NULLIF(COALESCE(#ColA, '') + COALESCE(#ColB, ''), ''), '&') as 'Actual'
set #ColA = null; set #ColB = ''; select '&' as 'Expected', COALESCE(NULLIF(COALESCE(#ColA, '') + COALESCE(#ColB, ''), ''), '&') as 'Actual'
set #ColA = null; set #ColB = 'XYC'; select 'XYC' as 'Expected', COALESCE(NULLIF(COALESCE(#ColA, '') + COALESCE(#ColB, ''), ''), '&') as 'Actual'
set #ColA = ''; set #ColB = null; select '&' as 'Expected', COALESCE(NULLIF(COALESCE(#ColA, '') + COALESCE(#ColB, ''), ''), '&') as 'Actual'
set #ColA = ''; set #ColB = ''; select '&' as 'Expected', COALESCE(NULLIF(COALESCE(#ColA, '') + COALESCE(#ColB, ''), ''), '&') as 'Actual'
set #ColA = ''; set #ColB = 'XYC'; select 'XYC' as 'Expected', COALESCE(NULLIF(COALESCE(#ColA, '') + COALESCE(#ColB, ''), ''), '&') as 'Actual'
set #ColA = 'ABC';set #ColB = null; select 'ABC' as 'Expected', COALESCE(NULLIF(COALESCE(#ColA, '') + COALESCE(#ColB, ''), ''), '&') as 'Actual'
set #ColA = 'ABC';set #ColB = ''; select 'ABC' as 'Expected', COALESCE(NULLIF(COALESCE(#ColA, '') + COALESCE(#ColB, ''), ''), '&') as 'Actual'
set #ColA = 'ABC';set #ColB = 'XYC'; select 'ABC-XYC' as 'Expected', COALESCE(NULLIF(COALESCE(#ColA, '') + COALESCE(#ColB, ''), ''), '&') as 'Actual'
Do you think I have to do one giant case when? I have many columns like this, and that would make it unbearable to read.
Thanks!
UPDATE: if I use a case when, then my select looks like this, which seems to work, but is going to be a pain.
select
case when (#ColA is not null and #ColA <> '') and
(#ColB is not null and #ColB <> '')
then #ColA + '-' + #ColB
else COALESCE(NULLIF(COALESCE(#ColA, '') + COALESCE(#ColB, ''), ''), '&')
end
really hoping someone has some suggestions for improvement!
Could you use a searched case function like this?
Select Case When isnull(ColA, '') + isnull(ColB, '') == '' Then '&'
When isnull(ColA, '') <> '' and isnull(ColB, '') <> '' Then ColA + '-' + ColB
Else isnull(ColA, '') + isnull(ColB, '')
From Table1
I think this answer isn't a whole lot different than one of the other ones, but you might try this:
SELECT
CASE
WHEN NULLIF(ColA,'') <> '' AND NULLIF(ColB,'') <> ''
THEN ColA + '-' + ColB
WHEN NULLIF(ColA,'') <> '' OR NULLIF(ColB,'') <> ''
THEN COALESCE(ColA,ColB)
ELSE '&'
END

Resources