sql - 从 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.
解决方案
你为什么用print
?为什么不直接使用select
?
SELECT Denomination
FROM [SmartBoxData].[Denomination_SMO]
WHERE Currency = @Currency AND
@Currency IN ('USD', 'GBP', 'EUR', 'NGN');
推荐阅读
- c - 无法在c中递归地读取目录/文件
- yaml - LogStash 配置问题
- binary-tree - 找到二叉树中两个数字的路径重叠的数字
- javascript - 创建元素以添加到 DOM 树导致未定义?
- vue.js - Bootstrap Vue b-form-select 显示 json 对象
- python - Python多处理库行为不端?
- html - 下拉导航菜单出现在右侧并水平堆叠
- opencv - 我正在尝试编写一个 python 代码来使用 OpenCV 从输入图像中获取透视图。如何摆脱这个错误?
- r - 在 R 中使用手动图例需要帮助
- python - 将数据写入从网站抓取的 CSV 时出现 Python 错误