首页 > 解决方案 > 从 SQL 中的列打印数据

问题描述

我在 SQL Server 中有一些数据的值如下

ID  Currency    Denomination
1   NGN         5.00;10.00;20.00;50.00;100.00;200.00;500.00;1000.00
2   USD         1.00;2.00;5.00;10.00;20.00;50.00;100.00
3   GBP         5.00;10.00;20.00;50.00
4   EUR         5.00;10.00;20.00;50.00;100.00;200.00;500.00;

现在我想使用 SQL 存储过程打印各个列中的值。我有这个SQL语句

CREATE PROCEDURE SwitchMyCurrency

@Currency varchar (50) = null

AS BEGIN
SET NOCOUNT ON

SELECT * FROM [SmartBoxData].[Denomination_SMO] WHERE Currency = @Currency
    IF(@Currency = 'USD')
        BEGIN
           PRINT Denomination
        END
    ELSE
    IF(@Currency = 'GBP')
        BEGIN
           PRINT Denomination
        END
    IF(@Currency = 'EUR')
        BEGIN
           PRINT Denomination
        END
    ELSE
    IF(@Currency = 'NGN')
        BEGIN
           PRINT Denomination
        END
END
GO

我收到此错误:

Msg 128, Level 15, State 1, Procedure SwitchMyCurrency, Line 12 [Batch Start Line 0]
The name "Denomination" is not permitted in this context. Valid expressions are constants, constant expressions, and (in some contexts) variables. Column names are not permitted.
Msg 128, Level 15, State 1, Procedure SwitchMyCurrency, Line 17 [Batch Start Line 0]
The name "Denomination" is not permitted in this context. Valid expressions are constants, constant expressions, and (in some contexts) variables. Column names are not permitted.
Msg 128, Level 15, State 1, Procedure SwitchMyCurrency, Line 21 [Batch Start Line 0]
The name "Denomination" is not permitted in this context. Valid expressions are constants, constant expressions, and (in some contexts) variables. Column names are not permitted.
Msg 128, Level 15, State 1, Procedure SwitchMyCurrency, Line 26 [Batch Start Line 0]
The name "Denomination" is not permitted in this context. Valid expressions are constants, constant expressions, and (in some contexts) variables. Column names are not permitted.

标签: sqlsql-server

解决方案


你为什么用print?为什么不直接使用select

SELECT Denomination
FROM [SmartBoxData].[Denomination_SMO]
WHERE Currency = @Currency AND
      @Currency IN ('USD', 'GBP', 'EUR', 'NGN');

推荐阅读