存储过程从基于 SET 的查询中获取数据

Stored procedure get data from query SET-based

你好,我正在制作一个存储过程,但我在使用打印输出代码时遇到问题,因为稍后我需要将变量写入临时数据库。 这是代码:

SELECT 
    fmsTotalAmount + fmsAirTotalAmount + fmsProjectsTotalAmount TotalAmount, 
    fmsRelationAmount + fmsAirRelationAmount + fmsProjectsRelationAmount TotalRelationAmount
    FROM (

        SELECT 
            SUM(
                CASE WHEN fms1.currency != 'EUR' 
                    THEN fms1.Amount * fms1.Rate
                    ELSE ISNULL(fms1.Amount, 0) END) fmsTotalAmount,
            SUM(
                CASE WHEN fms1.relationcode = 'SHIP02' 
                    THEN 
                        CASE WHEN fms1.currency != 'EUR' 
                            THEN fms1.Amount * fms1.Rate
                            ELSE ISNULL(fms1.Amount, 0) END
                    ELSE 0 END) fmsRelationAmount,
            SUM(
                CASE WHEN fmsAir1.currency != 'EUR' 
                    THEN fmsAir1.Amount * fmsAir1.Rate
                    ELSE ISNULL(fmsAir1.Amount, 0) END) fmsAirTotalAmount,
            SUM(
                CASE WHEN fmsProjects1.relationcode = 'SHIP02' 
                    THEN 
                        CASE WHEN fmsProjects1.currency != 'EUR' 
                            THEN fmsProjects1.Amount * fmsAir1.Rate
                            ELSE ISNULL(fmsProjects1.Amount, 0) END
                    ELSE 0 END) fmsAirRelationAmount,
            SUM(
                CASE WHEN fmsProjects1.currency != 'EUR' 
                    THEN fmsProjects1.Amount * fmsAir1.Rate
                    ELSE ISNULL(fmsProjects1.Amount, 0) END) fmsProjectsTotalAmount,
            SUM(
                CASE WHEN fmsProjects1.relationcode = 'SHIP02' 
                    THEN 
                        CASE WHEN fmsProjects1.currency != 'EUR' 
                            THEN fmsProjects1.Amount * fmsProjects1.Rate
                            ELSE ISNULL(fmsProjects1.Amount, 0) END
                    ELSE 0 END) fmsProjectsRelationAmount
        FROM   [fms].[dbo].[file] f
        LEFT JOIN [fms].[dbo].[outgoinginvoiceline] fms1 ON 
            fms1.filenumber = CONVERT(NVARCHAR, f.filenumber)
        LEFT JOIN [fmsAir].[dbo].[outgoinginvoiceline] fmsAir1 ON 
            fmsAir1.filenumber = CONVERT(NVARCHAR, f.filenumber)
        LEFT JOIN [fmsProjects].[dbo].[outgoinginvoiceline] fmsProjects1 ON 
            fmsProjects1.filenumber = CONVERT(NVARCHAR, f.filenumber)


) a

现在我想打印此 SELECT 语句的输出。但是当我这样做时:

print 'Total money invoiced: ' + CONVERT(nvarchar, fmsTotalAmount) + '€ for this Relation' print 'Total money invoiced: ' + CONVERT(nvarchar, fmsTotalRelationAmount) + '€ in total'

但是我得到以下错误:

The name "fmsTotalAmount" is not permitted in this context. Valid expressions are constants, constant expressions, and (in some contexts) variables. Column names are not permitted.

The name "fmsTotalRelationAmount" is not permitted in this context. Valid expressions are constants, constant expressions, and (in some contexts) variables. Column names are not permitted.

在照片上。如何从中获取值到变量中?而不是将它作为一行返回。我需要变量。

首先尝试声明一个打印变量并打印该值。

declare @ptext varchar(max)
select @ptext = 'Total money invoiced: ' + CONVERT(nvarchar, fmsTotalAmount) + '€ for this Relation' 
print @ptext
select @ptext = 'Total money invoiced: ' + CONVERT(nvarchar, fmsTotalRelationAmount) + '€ in total'
print @ptext

如果我理解正确,你想将 SELECT 的结果存储到 table。

为了实现这一点,您首先尝试将值写入变量,然后 - 在第二步中 - 您尝试使用 INSERT 语句将这些变量插入 table 中。

最好试试这个语法:

SELECT col1, col2, ... INTO #SomeTable FROM SomeWhere

INTO #SomeTable 将自动创建一个具有拟合结构的临时 table,并将完整结果插入到这个新创建的临时 table 中。

一个简单的 SELECT * FROM #SomeTable 会返回结果。