I am creating a new table into database. My code is below
CREATE TABLE Master_data AS
select a.*, COALESCE(b.content_view,0) as content_view, COALESCE(b.headline_view,0) as headline_view
FROM [Britannia_MMx_2021].[dbo].[Sales_20210323] a
left join [Britannia_MMx_2021].[dbo].[Doximity_20220328] b on a.NPI = b.NPI
and a.Year_Mon = b.Date;
I am getting this error
Msg 102, Level 15, State 1, Line 46
Incorrect syntax near '('.
Your SQL snippet displays characteristics of TSQL. In TSQL (e.g. in MS SQL Server) you do not use "create table as" followed by a select statement, instead you use an INTO clause e.g.
SELECT a.*
, COALESCE(b.content_view, 0) AS content_view
, COALESCE(b.headline_view, 0) AS headline_view
INTO Master_data
FROM [Britannia_MMx_2021].[dbo].[Sales_20210323] a
LEFT JOIN [Britannia_MMx_2021].[dbo].[Doximity_20220328] b ON a.NPI = b.NPI
AND a.Year_Mon = b.DATE;
In MySQL you do use CREATE TABLE [ IF NOT EXISTS ] new_table [ AS ]
Related
I have the following code table A has a check constraint on column Denial.
CREATE TABLE Table a
(
[ID] int IDENTITY(1,1) NOT NULL ,
[EntityID] int ,
Denial nVarchar(20)
CONSTRAINT Chk_Denial CHECK (Denial IN ('Y', 'N')),
)
Merge statement
MERGE INTO Table a WITH (HOLDLOCK) AS tgt
USING (SELECT DISTINCT
JSON_VALUE(DocumentJSON, '$.EntityID') AS EntityID,
JSON_VALUE(DocumentJSON, '$.Denial') AS Denial
FROM Table1 bd
INNER JOIN table2 bf ON bf.FileUID = bd.FileUID
WHERE bf.Type = 'Payment') AS src ON tgt.[ID] = src.[ID]
WHEN MATCHED
)) THEN
UPDATE SET tgt.ID = src.ID,
tgt.EntityID = src.EntityID,
tgt.Denial = src.Denial,
WHEN NOT MATCHED BY TARGET
THEN INSERT (ID, EntityID, Denial)
VALUES (src.ID, src.EntityID, src.Denial)
THEN DELETE
I get this error when running my MERGE statement:
Error Message Msg 547, Level 16, State 0, Procedure storproctest1, Line 40 [Batch Start Line 0]
The MERGE statement conflicted with the CHECK constraint "Chk_Column". The conflict occurred in the database "Test", table "Table1", and column 'Denial'. The statement has been terminated.
This is due to the source files having "Yes" and "No" instead of 'Y' and 'N'. Hence, I'm getting the above error.
How can I use a Case statement in merge statement to handle the above Check constraints error? or Any alternative solutions.
You can turn Yes to Y and No to N before merging your data. That would belong to the using clause of the merge query:
USING (
SELECT Distinct
JSON_VALUE(DocumentJSON, '$.EntityID') AS EntityID,
CASE JSON_VALUE(DocumentJSON, '$.Denial')
WHEN 'Yes' THEN 'Y'
WHEN 'No' THEN 'N'
ELSE JSON_VALUE(DocumentJSON, '$.Denial')
END AS Denial
FROM Table1 bd
INNER JOIN table2 bf ON bf.FileUID = bd.FileUID
WHERE bf.Type = 'Payment'
) AS src
The case expression translates Y and N values, and leaves other values untouched. Since this applies to the source dataset, the whole rest of the query benefits (ie both the update and insert branches).
I am trying to insert some date into another table. At first I´ve tried to use sqlalchemy to create such queries, but as I got some error when executing, I tried to solve it through raw SQL, but the error still the same.
I am not very used to CTE commands, so I don´t know if there are some restrinctions over them.
WITH Conv_Pre_Pagos AS
(SELECT CONVENIO.COD_IDEN, CONVENIO.D_CLIENTE_NOM
FROM db2rpc.CONVENIO
WHERE CONVENIO.COD_ESPC = 52)
INSERT INTO DB2I023A.ANL_TARF_PAGAS_PREPAGO (convenio, convenente) SELECT CBR_TARF_REC.NR_DOC_SIS_OGM, Conv_Pre_Pagos.D_CLIENTE_NOM
FROM DB2TFA.CBR_TARF_REC JOIN Conv_Pre_Pagos ON CBR_TARF_REC.NR_DOC_SIS_OGM = Conv_Pre_Pagos.COD_IDEN
The sentence is bigger, but I removed some data to bring it cleaner. Still, the same error:
ibm_db_dbi::ProgrammingError: SQLNumResultCols failed: [IBM][CLI Driver][DB2] SQL0199N The use of the reserved word "INSERT" following "INSERT" is not valid.
Expected tokens may include: "(SELECT ,". SQLSTATE=42601 SQLCODE=-199
[SQL: WITH Conv_Pre_Pagos AS (SELECT CONVENIO.COD_IDEN, CONVENIO.D_CLIENTE_NOM
FROM db2rpc.CONVENIO WHERE CONVENIO.COD_ESPC = 52)
INSERT INTO DB2I023A.ANL_TARF_PAGAS_PREPAGO (convenio, convenente)
SELECT CBR_TARF_REC.NR_DOC_SIS_OGM, Conv_Pre_Pagos.D_CLIENTE_NOM
FROM DB2TFA.CBR_TARF_REC JOIN Conv_Pre_Pagos ON CBR_TARF_REC.NR_DOC_SIS_OGM = Conv_Pre_Pagos.COD_IDEN]
(Background on this error at: https://sqlalche.me/e/14/f405)"
Where does it see an "insert following insert"?
Try this:
INSERT INTO DB2I023A.ANL_TARF_PAGAS_PREPAGO (convenio, convenente)
WITH Conv_Pre_Pagos AS
(
SELECT CONVENIO.COD_IDEN, CONVENIO.D_CLIENTE_NOM
FROM db2rpc.CONVENIO
WHERE CONVENIO.COD_ESPC = 52
)
SELECT CBR_TARF_REC.NR_DOC_SIS_OGM, Conv_Pre_Pagos.D_CLIENTE_NOM
FROM DB2TFA.CBR_TARF_REC
JOIN Conv_Pre_Pagos ON CBR_TARF_REC.NR_DOC_SIS_OGM = Conv_Pre_Pagos.COD_IDEN
I can create a table on my MySQL server database with no data in as such
CREATE TABLE plu_to_stock (
ITEM_NUMBER VARCHAR(31),
QTY_AVAILABLE DECIMAL(18,4)
)
How come I can't do as this ?
CREATE TABLE plu_to_stock as select ITEM_NUMBER
, QTY_AVAILABLE
from item_details ,
item_amounts
where item_details.item_id = item_amounts.item_id
I get this message
Msg 156, Level 15, State 1, Line 1
Incorrect syntax near the keyword 'as'.
if I ditch the as
Msg 156, Level 15, State 1, Line 2
Incorrect syntax near the keyword 'select'.
This should work:
CREATE TABLE tableNameDifferentFromTheOneYouUsedAbove
ENGINE=MyISAM SELECT itD.item_id AS ITEM_NUMBER,
itA.item_id AS QTY_AVAILABLE
FROM item_details AS itD
JOIN item_amounts AS itA
ON itD.item_id = itA.item_id
SELECT itD.item_id AS ITEM_NUMBER,
itA.item_id AS QTY_AVAILABLE
into new_table
FROM item_details AS itD
JOIN item_amounts AS itA
ON itD.item_id = itA.item_id
Not sure if this is the correct approach but seems to work just as well.
SELECT PRODDTA.F4006.OADOCO, PRODDTA.F4006.OADCTO, PRODDTA.F4006.OAKCOO, PRODDTA.F4006.OAANTY, PRODDTA.F4006.OAMLNM, PRODDTA.F4006.OAADD1, PRODDTA.F4006.OAADD2, PRODDTA.F4006.OAADD3, PRODDTA.F4006.OAADD4, PRODDTA.F4006.OAADDZ, PRODDTA.F4006.OACTY1, PRODDTA.F4006.OACOUN, PRODDTA.F4006.OAADDS, PRODDTA.F4006.OACRTE, PRODDTA.F4006.OABKML, PRODDTA.F4006.OACTR, PRODDTA.F4006.OAUSER, PRODDTA.F4006.OAPID, PRODDTA.F4006.OAUPMJ, PRODDTA.F4006.OAJOBN, PRODDTA.F4006.OAUPMT, PRODDTA.F4006.OALNID
INTO #BAF4006ShipTo
FROM PRODDTA.F4006
WHERE (PRODDTA.F4006.OAANTY='2');
SELECT PRODDTA.F4211.SDKCOO, PRODDTA.F4211.SDDOCO, PRODDTA.F4211.SDDCTO, PRODDTA.F4211.SDAN8, PRODDTA.F4211.SDSHAN, PRODDTA.F4211.SDDOC, PRODDTA.F4211.SDDCT, PRODDTA.F4211.SDNXTR
INTO #BAFSumF4211byInvoice
FROM PRODDTA.F4211
GROUP BY PRODDTA.F4211.SDKCOO, PRODDTA.F4211.SDDOCO, PRODDTA.F4211.SDDCTO, PRODDTA.F4211.SDAN8, PRODDTA.F4211.SDSHAN, PRODDTA.F4211.SDDOC, PRODDTA.F4211.SDDCT, PRODDTA.F4211.SDNXTR
HAVING ((PRODDTA.F4211.SDKCOO='00035') AND (PRODDTA.F4211.SDNXTR='999'));
SELECT PRODDTA.F0018.TDCO, PRODDTA.F0018.TDDCTO, PRODDTA.F0018.TDDOCO, PRODDTA.F0018.TDAN8, PRODDTA.F0101.ABALPH, PRODDTA.F0116.ALADDS, (PRODDTA.F0018.TDAEXP/100) AS TDAEXP, (PRODDTA.F0018.TDATXN/100) AS TDATXN, (PRODDTA.F0018.TDATXA/100) AS TDATXA, (PRODDTA.F0018.TDSTAM/100) AS TDSTAM, PRODDTA.F0018.TDTXA1, PRODDTA.F0018.TDEXR1, PRODDTA.F0018.TDDGL, PRODDTA.F0018.TDDSVJ
INTO #BAFTaxQuery
FROM (PRODDTA.F0018 INNER JOIN PRODDTA.F0101 ON PRODDTA.F0018.TDAN8 = PRODDTA.F0101.ABAN8) INNER JOIN PRODDTA.F0116 ON PRODDTA.F0018.TDAN8 = PRODDTA.F0116.ALAN8
WHERE ((PRODDTA.F0018.TDCO='00035') AND (PRODDTA.F0018.TDDCTO='RI') AND (PRODDTA.F0018.TDDGL>114182 And PRODDTA.F0018.TDDGL<=114273));
SELECT #BAFTaxQuery.TDCO, #BAFTaxQuery.TDDCTO, #BAFTaxQuery.TDDOCO, #BAFSumF4211byInvoice.SDDOCO, #BAFSumF4211byInvoice.SDDCTO, #BAFTaxQuery.TDAN8, #BAFTaxQuery.ABALPH, #BAFTaxQuery.ALADDS, #BAFSumF4211byInvoice.SDSHAN, #BAFTaxQuery.TDAEXP, #BAFTaxQuery.TDATXN, #BAFTaxQuery.TDATXA, #BAFTaxQuery.TDSTAM, #BAFTaxQuery.TDTXA1, #BAFTaxQuery.TDEXR1, #BAFTaxQuery.TDDGL, #BAFTaxQuery.TDDSVJ
INTO #BAFTaxQuery2
FROM #BAFTaxQuery LEFT JOIN #BAFSumF4211byInvoice ON (#BAFTaxQuery.TDDOCO = #BAFSumF4211byInvoice.SDDOC) AND (#BAFTaxQuery.TDDCTO = #BAFSumF4211byInvoice.SDDCT);
SELECT #BAFTaxQuery2.TDCO, #BAFTaxQuery2.TDDCTO, #BAFTaxQuery2.TDDOCO, #BAFTaxQuery2.SDDOCO, #BAFTaxQuery2.SDDCTO, #BAFTaxQuery2.TDAN8, #BAFTaxQuery2.ABALPH as BAFTaxQuery2_ABALPH, #BAFTaxQuery2.ALADDS as BAFTaxQuery2_ALADDS, #BAFTaxQuery2.SDSHAN,PRODDTA.F0101.ABALPH as BAFTaxQuery3_ABALPH, PRODDTA.F0116.ALADDS as BAFTaxQuery3_ALADDS, #BAFTaxQuery2.TDAEXP, #BAFTaxQuery2.TDATXN, #BAFTaxQuery2.TDATXA, #BAFTaxQuery2.TDSTAM, #BAFTaxQuery2.TDTXA1, #BAFTaxQuery2.TDEXR1, #BAFTaxQuery2.TDDGL, #BAFTaxQuery2.TDDSVJ
INTO #BAFTaxQuery3
FROM #BAFTaxQuery2 INNER JOIN PRODDTA.F0101 ON #BAFTaxQuery2.SDSHAN = PRODDTA.F0101.ABAN8 INNER JOIN PRODDTA.F0116 ON #BAFTaxQuery2.SDSHAN = PRODDTA.F0116.ALAN8;
SELECT #BAFTaxQuery3.TDCO, #BAFTaxQuery3.TDDCTO, #BAFTaxQuery3.TDDOCO, #BAFTaxQuery3.SDDOCO, #BAFTaxQuery3.SDDCTO, #BAFTaxQuery3.TDAN8, #BAFTaxQuery3.BAFTaxQuery2_ABALPH, #BAFTaxQuery3.BAFTaxQuery2_ALADDS, #BAFTaxQuery3.SDSHAN, #BAFTaxQuery3.BAFTaxQuery3_ABALPH, #BAFTaxQuery3.BAFTaxQuery3_ALADDS,#BAFTaxQuery3.TDAEXP, #BAFTaxQuery3.TDATXN, #BAFTaxQuery3.TDATXA, #BAFTaxQuery3.TDSTAM, #BAFTaxQuery3.TDTXA1, #BAFTaxQuery3.TDEXR1, #BAFTaxQuery3.TDDGL, #BAFTaxQuery3.TDDSVJ, #BAF4006ShipTo.OAMLNM, #BAF4006ShipTo.OACTY1, #BAF4006ShipTo.OAADDS
INTO #BAFTaxReportQuery
FROM #BAFTaxQuery3 LEFT JOIN #BAF4006ShipTo ON (#BAFTaxQuery3.SDDCTO = #BAF4006ShipTo.OADCTO) AND (#BAFTaxQuery3.SDDOCO = #BAF4006ShipTo.OADOCO) AND (#BAFTaxQuery3.TDCO = #BAF4006ShipTo.OAKCOO);
DROP TABLE #BAF4006ShipTo
DROP TABLE #BAFTaxQuery
DROP TABLE #BAFTaxQuery2
DROP TABLE #BAFSumF4211byInvoice
DROP TABLE #BAFTaxQuery3
DROP TABLE #BAFTaxReportQuery
The Query runs fine in Sql Server.
I create shared dataset as 'Text' and I'm able to create successfully.
But in the report when I Add Dataset , there are no fields showing.
I tried adding it manually too but get this error. "THe shared Dataset definition is not valid. The report definition element 'Dataset' is empty at line 43,position 5. It is missing a mandatory chid elemnt of type 'Fields.'"
I need to re-write the following Oracle 10g query to work in SQL Server 2008
It's an update query, where some field are retrieved from a SELECT and some are given (from code).
UPDATE "BMAN_SQL"."CELLS_GLIST"
SET ("GLIST_ID", "GLIST_VALUE_ID") = (
SELECT "GLIST_ID", "GLIST_VAL_ID"
FROM "BMAN_SQL"."GLISTS_VAL_UOR"
WHERE ("UOR_ID"=3)
AND ("GLIST_CODE"='X')
),
"SESSION_ID" = 1553245736,
"USER_ID" = 13
WHERE EXISTS ( SELECT * FROM ... )
Note that I need to use the UPDATE SET ... WHERE EXIST ... structure for compatibility with Oracle (query are automatically built by a QueryBuilder class for each specific DBMS).
I also cannot write:
UPDATE "BMAN_SQL"."CELLS_GLIST"
SET ("GLIST_ID", "GLIST_VALUE_ID", "SESSION_ID", "USER_ID") = (
SELECT "GLIST_ID", "GLIST_VAL_ID", 1553245736, 13
FROM "BMAN_SQL"."GLISTS_VAL_UOR"
WHERE ("UOR_ID"=3)
AND ("GLIST_CODE"='X')
)
WHERE EXISTS ( SELECT * FROM ... )
because (as per this old thread Oracle "Cannot update to NULL") it returns an error if the SELECT does not fetch any record.
Thanks in advance!
You need to join update query in this case. Please see syntax below. <Your condition here> Replace this section with your join condition here.
UPDATE m
SET
GLIST_ID = r.GLIST_ID
,GLIST_VALUE_ID = r.GLIST_VAL_ID
, SESSION_ID= 1553245736
, USER_ID = 13
from BMAN_SQL.CELLS_GLIST m
inner join BMAN_SQL.GLISTS_VAL_UOR r on <Your condition here>
WHERE
r.UOR_ID=3 AND (r.GLIST_CODE='X') AND
EXISTS ( SELECT * FROM ... )
EDIT
UPDATE BMAN_SQL.CELLS_GLIST
SET
GLIST_ID = (SELECT TOP 1 GLIST_ID FROM BMAN_SQL.GLISTS_VAL_UOR WHERE (UOR_ID=3) AND (GLIST_CODE='X'))
,GLIST_VALUE_ID = (SELECT TOP 1 GLIST_VAL_ID FROM BMAN_SQL.GLISTS_VAL_UOR WHERE (UOR_ID=3) AND (GLIST_CODE='X'))
,SESSION_ID = 1553245736
,USER_ID = 13
WHERE EXISTS ( SELECT * FROM ... )