使用 CAST 函数的参数化更新查询
Parametrized update query with CAST function
我正在使用如下更新查询:
UPDATE PeopleTable
SET firstname = CAST(REPLACE(CAST(firstname as nvarchar(max)), 'John', 'ReplacedFirstName') as ntext)
我想对几个名字做同样的事情,比如 "Dave, Tom, Harry, Rick, Nick"
如何正确执行此操作?
我试过这样的事情:
create procedure proc2
@sp varchar(25)
as
select UPDATE PeopleTable SET first name = CAST(REPLACE(CAST(BODY as
nvarchar(max)), searchString = @sp,'ReplacedFirstName') as ntext)
go
declare @sp varchar(25)
//I want to loop here and execute the stored proc for a list of names
set @sp = 'name'
exec proc2 @sp
您可以将 cte
与值构造一起使用
with t as (
select *
from ( values ('John', 'ReplacedFirstName'), ('Dave', 'ReplacedFirstName'),
. . .
) tt (firstname, ReplacedFirstName)
)
update p
set p.firstname = t.ReplacedFirstName
from PeopleTable p inner join
t
on t.firstname = p.firstname;
PreparedStatement pstmt = null;
String query = "UPDATE PeopleTable SET firstname = CAST(REPLACE(CAST(firstname as nvarchar(max)), ? ,'ReplacedFirstName') as ntext)";
pstmt = conn.prepareStatement(query);
for(String s: myList) {
pstmt.setString(1, s);
pstmt.addBatch();
}
pstmt.executeUpdate();
我正在使用如下更新查询:
UPDATE PeopleTable
SET firstname = CAST(REPLACE(CAST(firstname as nvarchar(max)), 'John', 'ReplacedFirstName') as ntext)
我想对几个名字做同样的事情,比如 "Dave, Tom, Harry, Rick, Nick"
如何正确执行此操作?
我试过这样的事情:
create procedure proc2
@sp varchar(25)
as
select UPDATE PeopleTable SET first name = CAST(REPLACE(CAST(BODY as
nvarchar(max)), searchString = @sp,'ReplacedFirstName') as ntext)
go
declare @sp varchar(25)
//I want to loop here and execute the stored proc for a list of names
set @sp = 'name'
exec proc2 @sp
您可以将 cte
与值构造一起使用
with t as (
select *
from ( values ('John', 'ReplacedFirstName'), ('Dave', 'ReplacedFirstName'),
. . .
) tt (firstname, ReplacedFirstName)
)
update p
set p.firstname = t.ReplacedFirstName
from PeopleTable p inner join
t
on t.firstname = p.firstname;
PreparedStatement pstmt = null;
String query = "UPDATE PeopleTable SET firstname = CAST(REPLACE(CAST(firstname as nvarchar(max)), ? ,'ReplacedFirstName') as ntext)";
pstmt = conn.prepareStatement(query);
for(String s: myList) {
pstmt.setString(1, s);
pstmt.addBatch();
}
pstmt.executeUpdate();