动态sql - 试图保存价值

发布于 2024-10-25 02:50:50 字数 538 浏览 0 评论 0原文

嘿,基本上是尝试将 [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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

茶底世界 2024-11-01 02:50:50

设置@fieldID = exec(@sql1)

您需要将 SP_EXECUTESQL 与 OUTPUT 一起使用,

这是一个示例

DECLARE @chvTableName VARCHAR(100),
@intTableCount INT,
@chvSQL NVARCHAR(100)

SELECT @chvTableName = 'Authors'
SELECT @chvSQL = N'SELECT @intTableCount = COUNT(*) FROM ' + @chvTableName

EXEC sp_executesql @chvSQL, N'@intTableCount INT OUTPUT', @intTableCount OUTPUT

SELECT @intTableCount
GO

所以,试试这个

DECLARE @DataBaseName VARCHAR(100),
@fieldID INT,
@chvSQL NVARCHAR(300)

SELECT @chvSQL = N'SELECT @fieldID =[Field ID]  from ['+@DataBaseName+'].dbo.[Custom Field Definition] where [Name] = ''Roster Y/N'' and [Table Type] = 0'

EXEC sp_executesql @chvSQL, N'@fieldID INT OUTPUT', @fieldID OUTPUT

SELECT @fieldID
GO

set @fieldID = exec(@sql1)

you need to use SP_EXECUTESQL with OUTPUT

here is an example

DECLARE @chvTableName VARCHAR(100),
@intTableCount INT,
@chvSQL NVARCHAR(100)

SELECT @chvTableName = 'Authors'
SELECT @chvSQL = N'SELECT @intTableCount = COUNT(*) FROM ' + @chvTableName

EXEC sp_executesql @chvSQL, N'@intTableCount INT OUTPUT', @intTableCount OUTPUT

SELECT @intTableCount
GO

So, try this

DECLARE @DataBaseName VARCHAR(100),
@fieldID INT,
@chvSQL NVARCHAR(300)

SELECT @chvSQL = N'SELECT @fieldID =[Field ID]  from ['+@DataBaseName+'].dbo.[Custom Field Definition] where [Name] = ''Roster Y/N'' and [Table Type] = 0'

EXEC sp_executesql @chvSQL, N'@fieldID INT OUTPUT', @fieldID OUTPUT

SELECT @fieldID
GO
ぃ双果 2024-11-01 02:50:50

简单的答案是将 fieldId 转换为 varchar

 Declare @fieldID as int
 Declare @sql1 as varchar(10)
 set @sql1 = 'select '+ Convert(varchar(100),@fieldID)
 exec(@sql1)

但正如 SQLMenace 建议的那样,使用 SP_ExecuteSQL

Easy answer is to convert you fieldId to a varchar

 Declare @fieldID as int
 Declare @sql1 as varchar(10)
 set @sql1 = 'select '+ Convert(varchar(100),@fieldID)
 exec(@sql1)

But as SQLMenace suggests, use SP_ExecuteSQL

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文