日期:2014-05-18  浏览次数:20564 次

sql server 存储过程里的字符单引号怎么写?
SQL code

alter procedure [dbo].[plp_elpoot001_ok]( @tlpsofc01 nvarchar(20))
as
declare @sql nvarchar(1000)
declare @tsyitmp nvarchar(50) 
begin

 set @tsyitmp=(select tlpsofc11 from tlpsofc where tlpsofc01=@tlpsofc01)

 set @sql='update a set a.'
 set @sql=@sql+@tsyitmp
 set @sql=@sql+' =b.tlpsofd06 from tsyi a,tlpsofd b where b.tlpsofd02=a.tsyi01 and a.tsyi01 in(select tlpsofd02 from tlpsofd where tlpsofd01= '
 set @sql=@sql+ @tlpsofc01 +')'

exec( @sql)

end

调试后得到的sql是:
update a set a.tsyi40 =b.tlpsofd06 from tsyi a,tlpsofd b where b.tlpsofd02=a.tsyi01 and a.tsyi01 in(select tlpsofd02 from tlpsofd where tlpsofd01=CCG20120408001 )

我想得到:
update a set a.tsyi40 =b.tlpsofd06 from tsyi a,tlpsofd b where b.tlpsofd02=a.tsyi01 and a.tsyi01 in(select tlpsofd02 from tlpsofd where tlpsofd01='CCG20120408001' )
也就是最后那个where参数要加上引号.



------解决方案--------------------
SQL code

declare @tlpsofc01 varchar(20)
set @tlpsofc01='CCG20120408001'

select 'select * from tablename where tlpsofc01='+@tlpsofc01
/*
select * from tablename where tlpsofc01=CCG20120408001
*/

select 'select * from tablename where tlpsofc01='+''''+@tlpsofc01+''''
/*
select * from tablename where tlpsofc01='CCG20120408001'
*/

------解决方案--------------------
SQL code

set @sql='update a set a.'
 set @sql=@sql+@tsyitmp
 set @sql=@sql+' =b.tlpsofd06 from tsyi a,tlpsofd b where b.tlpsofd02=a.tsyi01 and a.tsyi01 in(select tlpsofd02 from tlpsofd where tlpsofd01= '''
 set @sql=@sql+ @tlpsofc01 +''')'