动态sql - 试图保存价值
嘿,基本上是尝试将 [Field ID] 保存到 @fieldID 中,以便我可以在 SP 中的其他位置使用它。尝试了各种组合,但我大多收到“将 varchar 值转换为数据类型 int 时转换失败”的错误。 [字段 ID] 是一个 int 和主键。我什至尝试强制转换 [field id] 只是为了确保它仍然失败。
Declare @fieldID as int
Declare @sql1 as varchar(1000)
set @sql1 = 'select '+@fieldID+' = [Field ID] from ['+@DataBaseName+'].dbo.[Custom Field Definition] where [Name] = ''Roster Y/N'' and [Table Type] = 0'
--print @sql1
exec(@sql1)
如果我删除“'+@fieldID+'”我会得到正确的值。 我知道这不起作用,但是有类似设置 @fieldID = exec(@sql1) 的东西吗?
感谢您的帮助!
戴夫·K.
hey there, basically trying to get the [Field ID] saved into @fieldID so i can use it at another point in the SP. tried various combinations, but i mostly get errors of 'Conversion failed when converting the varchar value ... to data type int. The [field ID] is an int and primary key. i've even tried casting the [field id] just to make sure, it still fails.
Declare @fieldID as int
Declare @sql1 as varchar(1000)
set @sql1 = 'select '+@fieldID+' = [Field ID] from ['+@DataBaseName+'].dbo.[Custom Field Definition] where [Name] = ''Roster Y/N'' and [Table Type] = 0'
--print @sql1
exec(@sql1)
if i remove the " '+@fieldID+' " i get the proper value back.
i know this doesn't work, but is there something like, set @fieldID = exec(@sql1) ?
thanks for the help!
dave k.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您需要将 SP_EXECUTESQL 与 OUTPUT 一起使用,
这是一个示例
所以,试试这个
you need to use SP_EXECUTESQL with OUTPUT
here is an example
So, try this
简单的答案是将 fieldId 转换为 varchar
但正如 SQLMenace 建议的那样,使用 SP_ExecuteSQL
Easy answer is to convert you fieldId to a varchar
But as SQLMenace suggests, use SP_ExecuteSQL