【问题标题】:Add variable to query string SQL Server将变量添加到查询字符串 SQL Server
【发布时间】:2018-04-22 17:04:34
【问题描述】:

请支持我在查询字符串中添加变量。

我试过这段代码

create trigger UPLtoTOTAL on UP_PLAN
for insert
as
begin
    set nocount on

    declare @code nvarchar(10)
    declare @qty decimal(18,9)
    declare @dd int

    select @code = inserted.CODE, 
           @qty = inserted.QTY, 
           @dd = inserted.dd 
    from inserted

    declare @sqlstr nvarchar(1000)
    set @sqlstr = 'insert into TOTAL(CODE, COL2) values(' + @code + ',' + @qty + ')'
    exec(@sqlstr)
end

但我从 SQL Server 收到错误:

将 nvarchar 转换为数字数据类型时出现算术溢出错误。

请帮我修改该代码。

谢谢!

【问题讨论】:

  • 这段代码太错误了。 1) SQL Server 触发每条语句而不是每行 2) 不必要地使用动态 SQL 3) 错误的数据类型和隐式转换 4) 总表看起来像一些预聚合(也许物化视图会是更好的解决方案)

标签: sql-server-2008


【解决方案1】:

如果你成功了

create trigger UPLtoTOTAL on UP_PLAN 
for insert as 
begin 
set nocount on
insert into TOTAL(CODE, COL2) values(inserted.CODE, inserted.QTY)
end

这假设您插入的表和您触发的表具有相同的数据类型。

【讨论】:

    猜你喜欢
    • 2021-12-22
    • 1970-01-01
    • 2022-10-25
    • 1970-01-01
    • 2023-03-25
    • 1970-01-01
    • 1970-01-01
    • 2014-12-31
    • 1970-01-01
    相关资源
    最近更新 更多