我正在尝试将列Mgrstat中的信息更新为3,并希望大量输入信息。因为它是我必须使用" ="并单独输入每个AppID,但我宁愿一次输入几个。下面的查询显示了我在"中使用"的尝试,它也没有用。我在''""""附近得到"语法不正确。
有什么想法吗?谢谢大家!
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
答案 0 :(得分:3)
这是您需要的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')
答案 1 :(得分: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,
....
)
答案 2 :(得分:-1)
update [Compensation].[dbo].[dev_RPT_Approval]
set Mgr_Stat = @mgrstat
FROM [Compensation].[dbo].[dev_RPT_Approval]
where App_Id IN(@appid)