Link from Oracle to SQL Server can't create view - sql-server

using oracle 10 i have a link to sql server. I have a query (below) that runs fine as just as a select statment but when i use it to create a view (the view is created succsesfully but when i try to view the data in the view in sql developer) i get a
database 'linkname' does not support ROWIDs in this context
Is the issue the subselects where I am combining multiple rows in one table into multiple columns?
SELECT
upper(cus."cus_last_name") AS lname,
upper(cus."cus_first_name") AS fname,
upper(cus."cus_initial") AS minitial,
upper(cus."maiden_name") AS olname,
pt."cus_dob" AS dob,
pt."cus_id" AS id,
(
SELECT
cus_inst_key."cus_key_value"
FROM
"cus_inst_key"#linkname cus_inst_key
WHERE
cus_inst_key."cus_id" = cus."cus_id"
AND cus_inst_key."cus_key_cd" = 1
) AS CUSALTID,
(
SELECT
pcus_inst_key."cus_key_value"
FROM
"cus_inst_key"#linkname cus_inst_key
WHERE
cus_inst_key."cus_id" = cus."cus_id"
AND cus_inst_key."cus_key_cd" = 2
) AS CUSALTID2,
(
SELECT
cus_inst_key."cus_key_value"
FROM
"cus_inst_key"#linkname cus_inst_key
WHERE
cus_inst_key."cus_id" = cus."cus_id"
AND cus_inst_key."cus_key_cd" = 3
) AS CUSALTID3,
(
SELECT
cus_inst_key."cus_key_value"
FROM
"cus_inst_key"#linkname cus_inst_key
WHERE
cus_inst_key."cus_id" = cus."cus_id"
AND cus_inst_key."cus_key_cd" = 4
) AS CUSALTID4
FROM
"cus"#linkname cus;
thanks for any help.
the view actually is created ok i just can't use the data tab in sql developer for some reason.

Related

How to generate an excel file (.xlsx) from SQL Server

I have this query:
WITH InfoNeg AS
(
SELECT DISTINCT
n.idcliente,
CASE
WHEN DATEDIFF(MONTH, MAX(n.fechanegociacion), GETDATE()) <= 2
THEN 'Negociado 6 meses'
ELSE NULL
END AS TipoNeg
FROM
SAB2NewExports.dbo.negociaciones AS n
WHERE
Aprobacion = 'Si'
AND cerrado = 'Si'
GROUP BY
n.idcliente
), Multi AS
(
SELECT DISTINCT
idcliente, COUNT(distinct idportafolio) AS NumPorts
FROM
orangerefi.wfm.wf_master_HIST
WHERE
YEAR(Fecha_BKP) = 2021
AND MONTH(Fecha_BKP) = 08
GROUP BY
idcliente
)
SELECT DISTINCT
m.IdCliente, c.Nombre1
FROM
orangerefi.wfm.wf_master_HIST as m
LEFT JOIN
InfoNeg ON m.idcliente = InfoNeg.idcliente
LEFT JOIN
Multi ON m.IdCliente = Multi.idcliente
LEFT JOIN
SAB2NewExports.dbo.Clientes AS c ON m.IdCliente = c.IdCliente
WHERE
CanalTrabajo = 'Callcenter - Outbound' -- Cambiar aca
AND YEAR (Fecha_BKP) = 2021
AND MONTH(Fecha_BKP) = 08
AND GrupoTrabajo IN ('Alto') -- Cambiar aca
AND Bucket IN (1, 2) -- Cambiar aca
AND Multi.NumPorts > 1
AND Infoneg.TipoNeg IS NULL
When I run it, I get 30 thousand rows and the columns that I get when performing the query are: ClientID, name. I would like it to be saved in an Excel file when I run it, I don't know if it's possible.
Another question, is it possible to create a variable that stores text?
I used CONCAT(), but the text being so long is a bit cumbersome, I don't know if there is any alternative.
If you can help me, I appreciate it.
To declare a variable
DECLARE #string VARCHAR(MAX)
SET #string = concat()
then insert whatever you are concatenating
Here is an answer given by carusyte
Export SQL query data to Excel
I don't know if this is what you're looking for, but you can export the results to Excel like this:
In the results pane, click the top-left cell to highlight all the records, and then right-click the top-left cell and click "Save Results As". One of the export options is CSV.
You might give this a shot too:
INSERT INTO OPENROWSET
('Microsoft.Jet.OLEDB.4.0',
'Excel 8.0;Database=c:\Test.xls;','SELECT productid, price FROM dbo.product')
Lastly, you can look into using SSIS (replaced DTS) for data exports. Here is a link to a tutorial:
http://www.accelebrate.com/sql_training/ssis_2008_tutorial.htm
== Update #1 ==
To save the result as CSV file with column headers, one can follow the steps shown below:
Go to Tools->Options
Query Results->SQL Server->Results to Grid
Check “Include column headers when copying or saving results”
Click OK.
Note that the new settings won’t affect any existing Query tabs — you’ll need to open new ones and/or restart SSMS.

Merge..USING..WHEN SQL Server equivalent in PostgreSQL

Right now, we are trying to migrate a stored procedure from SQL Server to PostgreSQL. In this, We saw a querying method MERGE..USING..WHEN. So I couldn't find the equivalent. So is there any option to replicate this functionality? The actual query is given below.
WITH phase_list AS (
SELECT #id_plant AS id_plant
, id_phase
, date_started
FROM OPENJSON(#phase_list)
WITH (
id_phase INT '$.id_phase',
date_started DATE '$.date_started'
)
WHERE date_started IS NOT NULL
)
MERGE grow.plant_phase AS t
USING phase_list AS s
ON s.id_plant = t.id_plant AND s.id_phase = t.id_phase
WHEN MATCHED AND (t.date_started <> s.date_started) THEN
UPDATE
SET t.date_started=s.date_started,
t.date_updated=getutcdate(),
t.updated_by=#id_user
WHEN NOT MATCHED BY TARGET THEN
INSERT (id_plant, id_phase, date_started, created_by, updated_by)
VALUES (s.id_plant, s.id_phase, s.date_started, #id_user, #id_user)
WHEN NOT MATCHED BY SOURCE AND t.id_plant = #id_plant THEN
DELETE;
Can we replicate the same using any join operation with some if/else conditions? or any other approach?

SSRS: How to find which reports have subreports?

I have a lot of SQL Server Reporting Services (SSRS) reports (*.rdl). I want to know which of these reports are using subreports. How can I do that? Looking at an easier way instead of opening each report and figuring out if a subreport is being used.
Thanks
I think this should provide you with what you need (With thanks to Bret Stateham ):
--The first CTE gets the content as a varbinary(max)
--as well as the other important columns for all reports,
--data sources and shared datasets.
WITH ItemContentBinaries AS
(
SELECT
ItemID,Name,[Type]
,CASE Type
WHEN 2 THEN 'Report'
WHEN 5 THEN 'Data Source'
WHEN 7 THEN 'Report Part'
WHEN 8 THEN 'Shared Dataset'
ELSE 'Other'
END AS TypeDescription
,CONVERT(varbinary(max),Content) AS Content
FROM ReportServer.dbo.Catalog
WHERE Type IN (2,5,7,8)
),
--The second CTE strips off the BOM if it exists...
ItemContentNoBOM AS
(
SELECT
ItemID,Name,[Type],TypeDescription
,CASE
WHEN LEFT(Content,3) = 0xEFBBBF
THEN CONVERT(varbinary(max),SUBSTRING(Content,4,LEN(Content)))
ELSE
Content
END AS Content
FROM ItemContentBinaries
)
--The outer query gets the content in its varbinary, varchar and xml representations...
,VarcharContent as
(
SELECT
ItemID,Name,[Type],TypeDescription
,Content --varbinary
,CONVERT(varchar(max),Content) AS ContentVarchar --varchar
,CONVERT(xml,Content) AS ContentXML --xml
FROM ItemContentNoBOM
)
SELECT * FROM VarcharContent where ContentVarchar like '%<subreport%'
The following query below will return a list of deployed reports that have subreports. Here's the Microsoft reference and a link for referencing older versions of SSRS. It looks like the only difference is changing the version of SSRS in XMLNAMESPACES part of the CTE.
Query to return all subreports
WITH
XMLNAMESPACES
(
'http://schemas.microsoft.com/sqlserver/reporting/2016/01/reportdefinition' AS rdl
)
,
report_list
AS
(
SELECT
[ReportID] = cat.[ItemID]
, [ReportName] = cat.[Name]
, [ReportPath] = cat.[Path]
, [xmlColumn] = CAST(CAST(cat.[Content] AS VARBINARY(MAX)) AS XML)
FROM
[ReportServer].[dbo].[Catalog] AS cat
WHERE
1=1
AND cat.[Content] IS NOT NULL
AND cat.[Type] = 2
)
SELECT
rpt.[ReportID]
, rpt.[ReportName]
, rpt.[ReportPath]
, [SubReportName] = srpt.x.value('(//rdl:ReportName)[1]', 'NVARCHAR(256)')
FROM
report_list AS rpt
CROSS APPLY xmlColumn.nodes('//rdl:Subreport') AS srpt(x);

Insert into SQL table using Loop

I have two tables
Chapters_tbl (chID, name)
Status_tbl (chID, Ch_status)
For every chID in Chapters_tbl, I want to insert that chID into Status_tbl and Ch_status
I can use both a SQL Server stored procedure or Entity Framework in C#.
How can I do this?
Using T-SQL...
INSERT dbo.Status_tbl (chID, Ch_status)
SELECT
c.chID,
Ch_status = 1
FROM
dbo.Chapters c
WHERE
NOT EXISTS (
SELECT 1
FROM dbo.Staus_tbl s
WHERE c.chID = s.chID
);

View from DB2 to SQL Server 2005

I'm attempting to move a view between DB2 and SQL Server.
CREATE VIEW msu.bad_bus_cnty_st_mstr
AS
SELECT id,
bus_cnty_cntry_cd,
bus_st,
bus_zip
FROM summit.mstr
WHERE ( bus_cnty_cntry_cd, bus_st ) IN (SELECT cnty_cntry_cd,
st
FROM uhelp.cnty_cntry_cd
WHERE
cnty_cntry_descr LIKE '%invalid%');
The view works in DB2, but doesn't work with SQL Server because of the WHERE clause. Can I have a recommendation on how to rewrite this view to work with SQL Server?
It usually helps to define what "doesn't work" means (e.g. what error did you get) and also to specify the version of SQL Server you are using.
Unfortunately SQL Server doesn't support IN() with more than one clause. However you can re-write your view this way:
ALTER VIEW msu.bad_bus_cnty_st_mstr
AS
SELECT id,
bus_cnty_cntry_cd,
bus_st,
bus_zip
FROM summit.mstr AS mstr
WHERE EXISTS
(
SELECT 1
FROM uhelp.cnty_cntry_cd
WHERE cnty_cntry_descr LIKE '%invalid%'
AND cnty_cntry_cd = mstr.bus_cnty_cntry_cd
AND st = mstr.bus_st
);
one way
CREATE VIEW msu.bad_bus_cnty_st_mstr
AS
SELECT id,
bus_cnty_cntry_cd,
bus_st,
bus_zip
FROM summit.mstr m
WHERE EXISTS( SELECT 1 FROM uhelp.cnty_cntry_cd c
WHERE c.cnty_cntry_descr LIKE '%invalid%'
AND c.bus_cnty_cntry_cd = m.bus_cnty_cntry_cd
AND c.st = m.bus_st)

Resources