如何在存储过程中传递列作为参数?
如何传递并使用列名来检索实际列中的 bigint 变量?
DECLARE @personID BIGINT,
DECLARE @queryString varchar(500)
Set @queryString = 'Select @personID = ' + @PersonColumnID + ' from dbo.Loss_Witness where WitnessID = @witnessID'
exec(@queryString)
错误消息指出“必须声明变量‘@personID’。”我也尝试过
Set @queryString = 'Select ' + @personID + ' = ' + @witnessPersonID + ' from dbo.Loss_Witness where WitnessID = @witnessID'
并收到错误消息“将数据类型 varchar 转换为 bigint 时出错”。
有什么想法吗?
How do I pass and use the column name to retrieve a bigint variable in the actual column?
DECLARE @personID BIGINT,
DECLARE @queryString varchar(500)
Set @queryString = 'Select @personID = ' + @PersonColumnID + ' from dbo.Loss_Witness where WitnessID = @witnessID'
exec(@queryString)
Error message states "Must declare variable '@personID'." I also tried
Set @queryString = 'Select ' + @personID + ' = ' + @witnessPersonID + ' from dbo.Loss_Witness where WitnessID = @witnessID'
And got the error message "Error converting data type varchar to bigint."
Any thoughts?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您需要使用
OUTPUT
关键字指定 @personID 是一个输出参数:有
You need to specify that @personID is an out parameter with the
OUTPUT
keyword:There's another example here.
另请注意,您的代码中可能存在 SQL 注入安全漏洞。如果您不清理@PersonColumnID,那么您可能会遇到大麻烦。
Also note that there is potentially an SQL Injection security hole in your code. If you're not sanitizing @PersonColumnID then you could be in big trouble.