从 Sybase 中的参数构造动态列
我正在尝试在 Sybase 中编写存储过程(SP)。
SP 采用 5 个 varchar 参数。
根据传递的参数,我想构造要从特定表中选择的列名。
以下作品:
DECLARE @TEST VARCHAR(50) 选择@TEST =“国家” --print @TEST
执行("选择不同的 id_country AS id_level, Country AS nm_level FROM tempdb..tbl_books 内连接 (tbl_ch2_bespoke_report 内连接 tbl_ch2_bespoke_rpt_mapping ON tbl_ch2_bespoke_report.id_report = tbl_ch2_bespoke_rpt_mapping.id_report) ON id_" + @TEST + "= tbl_ch2_bespoke_rpt_mapping.id_pnl_level WHERE tbl_ch2_bespoke_report.id_report = 14")
但给了我多个结果:
1 1 行受影响。1
id_level nm_level
4376 XYZ
2 4340 ABC
但是我想只获得第二个结果。
我是否需要使用动态 SQL 来实现此目的?
非常感谢您的帮助。
——查帕克斯
I'm trying to write a stored proc (SP) in Sybase.
The SP takes 5 varchar parameters.
Based on the parameters passed, I want to construct the column names to be selected from a particular table.
The below works:
DECLARE @TEST VARCHAR(50)
SELECT @TEST = "country"
--print @TEST
execute("SELECT DISTINCT id_country AS id_level, Country AS nm_level
FROM tempdb..tbl_books INNER JOIN
(tbl_ch2_bespoke_report INNER JOIN tbl_ch2_bespoke_rpt_mapping
ON tbl_ch2_bespoke_report.id_report = tbl_ch2_bespoke_rpt_mapping.id_report)
ON id_" + @TEST + "= tbl_ch2_bespoke_rpt_mapping.id_pnl_level
WHERE tbl_ch2_bespoke_report.id_report = 14")
but gives me multiple results:
1 1 row(s) affected.
id_level nm_level
1 4376 XYZ
2 4340 ABC
I would like to however only obtain the 2nd result.
Do I need to necessarily use dynamic SQL to achieve this?
Many thanks for your help.
--Chapax
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果我理解正确,您希望消除“受影响的 1 行”。线。如果是这样,“set nocount on/off”选项应该可以解决问题:
或者
If I'm understanding you correctly, you'd like to eliminate the "1 row(s) affected." line. If so, the "set nocount on/off" option should do the trick:
or
SET NOCOUNT {ON|OFF}
以关闭行计数消息。SET
是的,您需要动态 SQL 来更改结果集的结构或内容(列列表或
WHERE
子句)。SET NOCOUNT {ON|OFF}
to turn off row count messages.Yes, you need to you dynamic SQL to change the structure or content of the result set (either the column list or the
WHERE
clause).