0

I have Linked server from PostgreSQL 12 to my SSMS and trying to join two PostgreSQL tables and insert the result to my table with the stored procedure. The problem I have is when adding a WHERE clause it starts displaying NULLs in the second table and I know there are values in there. Please see the examples below:

First query:

SELECT
    a.hardware_id AS [HardwareID], 
    a.created_at AS CreatedAt,
    ISNULL(b.serial, NULL) AS [DiskSerial],
    ISNULL(b.model, NULL) AS [DiskModel],
    ISNULL(b.vendor, NULL) AS [DiskMake],
    ISNULL(b.type_display, NULL) AS [DiskType],
    ISNULL(b.transport, NULL) AS [DiskTransport],
    ISNULL(b.status_display, NULL) AS [DiskStatus],
    ISNULL(b.capacity_display, NULL) AS [DiskCapacity],
    ISNULL(CONVERT(DATETIME, b.erase_finish_time AT TIME ZONE 'UTC'), NULL) AS [DiskEraseFinishTime],
    ISNULL(b.device_id, NULL) AS [DiskEraseID],
    ISNULL(b.erase_algorithm_display, NULL) AS [DiskEraseMethod],
    ISNULL(b.erase_level, NULL) AS [DiskEraseLevel],
    ISNULL(CONVERT(DATETIME, b.erase_start_time AT TIME ZONE 'UTC'), NULL) AS [DiskEraseStartTime],
    ISNULL(b.erase_result_display, NULL) AS [DiskEraseResult]
FROM [LinkedServerName].[DatabaseName].[public].[hardware] AS a
LEFT JOIN [LinkedServerName].[DatabaseName].[public].[disk_device] AS b 
    ON a.hardware_id = b.hardware_id

Running this query returns 3393 rows: Result 3393 rows

Now when I add a WHERE clause to the query the second table is displayed as NULLs apart from the first record

Second query added WHERE:

SELECT
    a.hardware_id AS [HardwareID], 
    a.created_at AS CreatedAt,
    ISNULL(b.serial, NULL) AS [DiskSerial],
    ISNULL(b.model, NULL) AS [DiskModel],
    ISNULL(b.vendor, NULL) AS [DiskMake],
    ISNULL(b.type_display, NULL) AS [DiskType],
    ISNULL(b.transport, NULL) AS [DiskTransport],
    ISNULL(b.status_display, NULL) AS [DiskStatus],
    ISNULL(b.capacity_display, NULL) AS [DiskCapacity],
    ISNULL(CONVERT(DATETIME, b.erase_finish_time AT TIME ZONE 'UTC'), NULL) AS [DiskEraseFinishTime],
    ISNULL(b.device_id, NULL) AS [DiskEraseID],
    ISNULL(b.erase_algorithm_display, NULL) AS [DiskEraseMethod],
    ISNULL(b.erase_level, NULL) AS [DiskEraseLevel],
    ISNULL(CONVERT(DATETIME, b.erase_start_time AT TIME ZONE 'UTC'), NULL) AS [DiskEraseStartTime],
    ISNULL(b.erase_result_display, NULL) AS [DiskEraseResult]
FROM [LinkedServerName].[DatabaseName].[public].[hardware] AS a
LEFT JOIN [LinkedServerName].[DatabaseName].[public].[disk_device] AS b 
    ON a.hardware_id = b.hardware_id WHERE a.created_at >= '2023-10-10'

Running this query returns filtered data of 1551 rows but without values from the second table, I want to get this number of rows (filtered by a.created_at) with the data populated for the rest of the columns: enter image description here

I tried changing WHERE clause to AND and this does not filter results by a.created_at

Third query:

SELECT
    a.hardware_id AS [HardwareID], 
    a.created_at AS CreatedAt,
    ISNULL(b.serial, NULL) AS [DiskSerial],
    ISNULL(b.model, NULL) AS [DiskModel],
    ISNULL(b.vendor, NULL) AS [DiskMake],
    ISNULL(b.type_display, NULL) AS [DiskType],
    ISNULL(b.transport, NULL) AS [DiskTransport],
    ISNULL(b.status_display, NULL) AS [DiskStatus],
    ISNULL(b.capacity_display, NULL) AS [DiskCapacity],
    ISNULL(CONVERT(DATETIME, b.erase_finish_time AT TIME ZONE 'UTC'), NULL) AS [DiskEraseFinishTime],
    ISNULL(b.device_id, NULL) AS [DiskEraseID],
    ISNULL(b.erase_algorithm_display, NULL) AS [DiskEraseMethod],
    ISNULL(b.erase_level, NULL) AS [DiskEraseLevel],
    ISNULL(CONVERT(DATETIME, b.erase_start_time AT TIME ZONE 'UTC'), NULL) AS [DiskEraseStartTime],
    ISNULL(b.erase_result_display, NULL) AS [DiskEraseResult]
FROM [LinkedServerName].[DatabaseName].[public].[hardware] AS a
LEFT JOIN [LinkedServerName].[DatabaseName].[public].[disk_device] AS b 
    ON a.hardware_id = b.hardware_id AND a.created_at >= '2023-10-10'

Running this query returns 3270 rows for some reason and filtering does not work as well: enter image description here

13
  • SSMS is just an IDE-like applicaton; you don't have a linked server in SSMS you have a linked server in your database engine. Presumably that's SQL Server? That you are using SSMS to connect to your instance is very likely completely irrelevant. Commented Nov 20, 2023 at 12:40
  • Your AND a.created_at >= '2023-10-10' is in the ON for the LEFT JOIN to disk_device; you should expect rows that have a value less than 2023-10-10 as those rows will have no related rows in the table "b" is for "disk_device" (what b..?). Commented Nov 20, 2023 at 12:42
  • Bad Habits to Kick : Using table aliases like (a, b, c) or (t1, t2, t3) Commented Nov 20, 2023 at 12:43
  • 1
    ISNULL(b.serial, NULL)? Why? Why return NULL if the value of b.serial is NULL? It's already NULL. It's like having CASE MyColumn WHEN 1 THEN 1 ELSE MyColumn END; it might as well be MyColumn. Commented Nov 20, 2023 at 12:45
  • That's Correct @ThomA RE_1: Its SQL Server, Commented Nov 20, 2023 at 12:56

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.