【问题标题】:Update column information更新列信息
【发布时间】:2015-05-14 15:14:45
【问题描述】:

我正在尝试将 Mgrstat 列中的信息更新为 3,并希望批量输入信息。因为我必须使用“=”并单独输入每个 AppID,但我宁愿一次输入多个。下面的查询显示了我使用“in”的尝试,它也不起作用。我得到“关键字'in'附近的语法不正确”。

有什么想法吗?谢谢大家!

declare @appid as int
declare @mgrstat as int

set @appid in ('10995',
'11201',
'9523',
'9558',
'9666',
'10069',
'10547',
'10548',
'9702',
'10698',
'9754',
'10161',
'10162',
'11240',
'11241',
'9553',
'10848',
'10667',
'9383',
'10709',
'9696',
'10053',
'10702')
set @mgrstat = 3


update [Compensation].[dbo].[dev_RPT_Approval]
set Mgr_Stat = @mgrstat
 FROM [Compensation].[dbo].[dev_RPT_Approval]
  where @appid = App_Id

  select *
  from [Compensation].[dbo].[dev_RPT_Approval]
  where @appid = App_Id

【问题讨论】:

  • 我没有看到任何查询分隔符,例如;
  • 您对问题的措辞让我有点困惑,所以为了澄清一下,您是否试图将所有 mgr_stats 设置为 3,其中 app_id 在 @ 中的 appIds 列表中987654324@?
  • 这是 sql server 吗?它看起来是 sql servery
  • 这是 SQL。我要做的是构建查询,这样我就不必单独输入每个 App_ID,因为我有很多。我所包含的并非包罗万象。
  • "SQL" 不是 RDBMS,很多 RDBMS 都使用 sql。是sql server、mysql、postgres、oracle吗?它看起来像 sql server,但您的问题没有被标记为这样(并且应该是)

标签: sql sql-update


【解决方案1】:

这是您需要的 SQL:

update dev_RPT_Approval set Mgr_Stat=3 
where designation
in ('10995',
'11201',
'9523',
'9558',
'9666',
'10069',
'10547',
'10548',
'9702',
'10698',
'9754',
'10161',
'10162',
'11240',
'11241',
'9553',
'10848',
'10667',
'9383',
'10709',
'9696',
'10053',
'10702')

【讨论】:

  • 看起来是这样做的!谢谢!
  • 嗨@MitchellHeath,如果这个或任何答案解决了您的问题,请点击复选标记考虑accepting it。这向更广泛的社区表明您已经找到了解决方案,并为回答者和您自己提供了一些声誉。没有义务这样做。
【解决方案2】:

如果我理解正确,并且您希望所有 mgr_stats 都为 3,而 app_id 在您的问题中提供的列表中,那么您可以通过以下几种方式做到这一点:

update [Compensation].[dbo].[dev_RPT_Approval]
set Mgr_Stat = 3
where app_id in (
'10995',
'11201',
'9523',
'9558',
'9666',
'10069',
'10547',
'10548',
'9702',
'10698',
'9754',
'10161',
'10162',
'11240',
'11241',
'9553',
'10848',
'10667',
'9383',
'10709',
'9696',
'10053',
'10702'
)

或(使用表变量的sql server)

declare @ids table (id varchar(50))
insert into @ids (id)
select     '10995'
union all select    '11201'
union all select    '9523'
union all select    '9558'
union all select    '9666'
union all select    '10069'
union all select    '10547'
union all select    '10548'
union all select    '9702'
union all select    '10698'
union all select    '9754'
union all select    '10161'
union all select    '10162'
union all select    '11240'
union all select    '11241'
union all select    '9553'
union all select    '10848'
union all select    '10667'
union all select    '9383'
union all select    '10709'
union all select    '9696'
union all select    '10053'
union all select    '10702'

update [Compensation].[dbo].[dev_RPT_Approval]
set Mgr_Stat = 3
from [Compensation].[dbo].[dev_RPT_Approval] t
inner join @ids i on t.app_id = i.id

关于您发布的代码的一些注意事项:

declare @appid as int
set @appId in ...

与此有关的一些事情 - @appId 被声明为整数,这意味着它是一个标量值(不能是集合) - 对于值集,您可以使用表变量,就像我在第二个示例中所做的那样完成您的问题。

此外,由于您作为 int 变量,我假设您的 ID 是 int 类型,不需要引号。

代替:

where app_id in (
'10995',
....
)

你可以这样做:

where app_id in (
10995,
....
)

【讨论】:

    【解决方案3】:

    你可以试试这个吗? 可能对你有用,伙计。在这里,您使用“=”而不是“IN

    传递多个值
      update [Compensation].[dbo].[dev_RPT_Approval]
        set Mgr_Stat = @mgrstat
         FROM [Compensation].[dbo].[dev_RPT_Approval]
          where App_Id IN(@appid)
    

    【讨论】:

    • 这不起作用,注意 OPs 变量 @appid 是一个标量,不能包含多个值。
    猜你喜欢
    • 1970-01-01
    • 2010-12-20
    • 2020-05-06
    • 1970-01-01
    • 1970-01-01
    • 2021-10-19
    • 2021-08-31
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多