1

我有可用的 SQL Server 代码,但无论我多么努力,我都无法将其转换为 Firebird。在这件事上我需要你的帮助。SQL Server 和 Firebird 所有的表都是一样的,数据类型也是一样的。

SQL Server 数据库

SQL Server“STOK_FIYAT”表设计

SQL 服务器

SQL Server“STOK_FIYAT”表数据

SQL 服务器

SQL Server“STOK”表设计

SQL 服务器

SQL Server“STOK”表数据

SQL 服务器

我的查询字符串:

DECLARE @cols AS NVARCHAR(MAX),
  @query  AS NVARCHAR(MAX),
  @ColumnNameCollection AS NVARCHAR(MAX),
  @HeaderNameCollection AS NVARCHAR(MAX);

SET @cols = STUFF((SELECT ',' + QUOTENAME(SF.FIYAT_NO)
            FROM STOK_FIYAT SF
      GROUP BY SF.FIYAT_NO
      ORDER BY SF.FIYAT_NO ASC
            FOR XML PATH(''), TYPE
            ).value('.', 'NVARCHAR(MAX)')
        ,1,1,'')

-- Get header list. A DISTINCT list of CompMesoIDs
SELECT @HeaderNameCollection= ISNULL(@HeaderNameCollection + ',','') 
       + QUOTENAME(FIYAT_NO) + ' as FIYAT_' + CAST(FIYAT_NO AS VARCHAR(16))
FROM (SELECT FIYAT_NO FROM STOK_FIYAT) AS STFIYAT 
GROUP BY STFIYAT.FIYAT_NO
ORDER BY STFIYAT.FIYAT_NO

set @query = 'SELECT BLKODU,STOKKODU,STOK_ADI,'+@HeaderNameCollection+'  FROM (SELECT STOK.[BLKODU], [STOKKODU], [STOK_ADI], [FIYAT_NO], [FIYATI] FROM STOK LEFT JOIN STOK_FIYAT ON STOK.BLKODU = STOK_FIYAT.BLSTKODU) AS SourceTable PIVOT(MAX([FIYATI]) FOR [FIYAT_NO] IN('+@cols+')) AS PivotTable ORDER BY BLKODU ASC;'
execute(@query)

SQL 输出:

SQL Server 输出

我必须为 Firebird 做所有的操作。

火鸟数据库:

火鸟“STOK”表设计

火鸟

火鸟“STOK_FIYAT”表设计

火鸟

我对火鸟没有经验。我自己尝试的代码:

SET TERM ^ ;

EXECUTE BLOCK
AS
DECLARE cols VARCHAR(255);
DECLARE query VARCHAR(255);
DECLARE ColumnNameCollection VARCHAR(255);
DECLARE HeaderNameCollection VARCHAR(255);

BEGIN

cols = STUFF((SELECT ',' + QUOTENAME(SF.FIYAT_NO)
            FROM STOK_FIYAT SF
      GROUP BY SF.FIYAT_NO
      ORDER BY SF.FIYAT_NO ASC
            FOR XML PATH(''), TYPE
            ).value('.', 'VARCHAR(255)')
        ,1,1,'')

SELECT :HeaderNameCollection= ISNULL(:HeaderNameCollection + ',','') 
       + QUOTENAME(FIYAT_NO) + ' as FIYAT_' + CAST(FIYAT_NO AS VARCHAR(16))
FROM (SELECT FIYAT_NO FROM STOK_FIYAT) AS STFIYAT 
GROUP BY STFIYAT.FIYAT_NO
ORDER BY STFIYAT.FIYAT_NO

set @query = 'SELECT BLKODU,STOKKODU,STOK_ADI,'+:HeaderNameCollection+'  FROM (SELECT STOK.[BLKODU], [STOKKODU], [STOK_ADI], [FIYAT_NO], [FIYATI] FROM STOK LEFT JOIN STOK_FIYAT ON STOK.BLKODU = STOK_FIYAT.BLSTKODU) AS SourceTable PIVOT(MAX([FIYATI]) FOR [FIYAT_NO] IN('+:cols+')) AS PivotTable ORDER BY BLKODU ASC;'
execute(@query)

END
^

set term ; ^

我得到的错误:

Engine Error (code = 335544569):
Dynamic SQL Error.
SQL error code = -104.
Token unknown - line 14, column 13.
FOR.

SQL Error (code = -104):
Invalid token.
4

0 回答 0