sql - where 子句中的 SQL 参数
问题描述
如果这是一个简单的问题,我深表歉意,但我觉得有些基本的东西我不理解。
我有以下脚本:
select @sql =
'select
fld1
,' + @param1 +'
,fld2
from
table
where
column = ''y'''
exec (@sql)
它完全返回了我期望的结果,但是当我将 where 子句中的列更改为如下参数时:
select @sql =
'select
fld1
,' + @param1 +'
,fld2
from
table
where
@param2 = ''y'''
exec (@sql)
根本不返回任何结果。我不能像这样在 where 子句中使用参数是否有根本原因?
谢谢
解决方案
学习使用sp_executesql
并将参数传递到查询执行中。
这看起来像:
select @sql = '
select fld1, @param1, fld2
from table
where @param2 = ''y''
';
exec sp_executesql @sql, N'@param1 ?, @param2 varchar(255)', @param1=@param1, @param2=@param2;
的?
类型@param1
。
推荐阅读
- python - 创建类以在python中获取天气数据时出现属性错误
- azure - 主机密钥在 Azure Function App 中的工作原理是什么?
- node.js - npm install serialport 需要VS吗?
- git - 从 Azure DevOps Pipeline 提交到 Git Repo 时文件夹重复
- java - Angular 和 ADFS
- jmeter-plugins - MAVEN JMETER:无法生成仪表板结果
- matlab - 如何访问包含结构的已加载文件中的特定行和列?
- uwp - Webview 控件的高度有限制吗?
- node.js - Express:cors 配置是否依赖于服务器 TTL?
- java - OAuth2 WebSecurityConfigurerAdapter 规则有什么意义,因为它没有优先于 ResourceServerConfigurerAdapter