I get different results in PostgreSQL/Babelfish on AWS Aurora than I do with SQL Server for the following:
DECLARE @template NVARCHAR(MAX) = ' {@a} and {@b} and {@c}';
DECLARE @params AS TABLE
(
k VARCHAR(255),
v VARCHAR(MAX)
)
INSERT INTO @params VALUES ('b','second')
INSERT INTO @params VALUES ('a','first')
INSERT INTO @params VALUES ('c','more')
SELECT
@template = REPLACE(@template, '{@' + k + '}', ISNULL(v,'NULL'))
FROM @params;
SELECT @template;
SQL Server: "first and second and more"
Babelfish: "{@a} and {@b} and more"
Is this a PostgreSQL difference, a Babelfish difference, my error?
I get different results in PostgreSQL/Babelfish on AWS Aurora than I do with SQL Server for the following:
DECLARE @template NVARCHAR(MAX) = ' {@a} and {@b} and {@c}';
DECLARE @params AS TABLE
(
k VARCHAR(255),
v VARCHAR(MAX)
)
INSERT INTO @params VALUES ('b','second')
INSERT INTO @params VALUES ('a','first')
INSERT INTO @params VALUES ('c','more')
SELECT
@template = REPLACE(@template, '{@' + k + '}', ISNULL(v,'NULL'))
FROM @params;
SELECT @template;
SQL Server: "first and second and more"
Babelfish: "{@a} and {@b} and more"
Is this a PostgreSQL difference, a Babelfish difference, my error?
Share Improve this question edited Mar 4 at 20:24 marc_s 757k184 gold badges1.4k silver badges1.5k bronze badges asked Mar 4 at 20:15 SalahSalah 4173 silver badges13 bronze badges 7 | Show 2 more comments1 Answer
Reset to default 2Per Martin Smith I learned that a recursive select can't be relied on. I rewrote it as a recursive CTE and it works on both platforms:
DECLARE @template NVARCHAR(MAX) = ' {@a} and {@b} and {@c}';
DECLARE @params AS TABLE (
k VARCHAR(255),
v VARCHAR(MAX)
);
INSERT INTO @params VALUES ('b', 'second'), ('a', 'first'), ('c', 'more');
;
WITH params AS (
SELECT
ROW_NUMBER() OVER (ORDER BY (SELECT NULL)) AS rn,
k,
v
FROM @params
),
replacer (repl, paramrow) AS (
SELECT @template, CAST(0 AS BIGINT)
UNION ALL
SELECT
REPLACE(r.repl, '{@' + p.k + '}', p.v),
p.rn
FROM replacer r
INNER JOIN params p ON r.paramrow + 1 = p.rn
)
SELECT TOP 1 repl
FROM replacer
ORDER BY paramrow DESC;
SELECT
. I can't test it right now, but an even simpler example would probably beSELECT @foo = @foo + SomeTable.X FROM SomeTable
– IMSoP Commented Mar 4 at 20:45