declare @tmpid int =-1 print @tmpid select @tmpid=id from dbo.ActivityPlayers where id=@id print @tmpid if @tmpid =-1 begin insert into dbo.ActivityPlayers (id) values(@id) end print @gamename set @gamename='game2' print @gamename update dbo.ActivityPlayers set @gamename=@content where id=@id
如以上代码,申明部分未贴
想以@gamename 代替字段,但是不知道用什么格式sql可以识别?有网友说 用&&包起来或者“”包起来都试过了,米用。
找到方法了 ,只要再定义一个varchar类型的变量,然后将要操作的语句赋值给它,再去调用这个变量。
附上代码参考,注意本来是varchar的变量前后要加上转义的单引号,还有int类型的要转成varchar
1 ALTER PROCEDURE [dbo].[updateActPlayers] 2 -- Add the parameters for the stored procedure here 3 @id int, 4 @content varchar(500), 5 @gamename varchar(200) 6 AS 7 BEGIN 8 -- SET NOCOUNT ON added to prevent extra result sets from 9 -- interfering with SELECT statements. 10 SET NOCOUNT ON; 11 12 -- Insert statements for procedure here 13 declare @tmpid int =-1 14 declare @sql varchar(500) 15 print @tmpid 16 select @tmpid=id from dbo.ActivityPlayers where id=@id 17 print @tmpid 18 if @tmpid =-1 19 begin 20 insert into dbo.ActivityPlayers (id) values(@id) 21 end 22 set @gamename='game2' 23 set @sql='update dbo.ActivityPlayers set '+@gamename+'='''+@content+''' where id='+convert(varchar(20),@id) 24 print @sql 25 exec(@sql) 26 END
[{$xxxxxxxxxxx$}]