请教,存储过程修改
CREATE PROCEDURE news_count
@id int
AS
Update news
Set hits = hits + 1
Where id = @id
GO
-----------------------------------
我想把这个存储过程写成一个通用的,怎么写?
通用字段如下:
news,hits,id
谢谢
------解决方案-------------------- CREATE PROCEDURE news_count
@id int,@Cid varchar(20),@hits varchar(20),@news varchar(20)
AS
declare @sql varchar(100)
set @sql= 'Update '+@news + ' Set '+@hits+ '= '+@hits+ '+1 Where '+@cid+ ' = '+cast(@id as varchar)
exec(@sql)
GO
------解决方案--------------------CREATE PROCEDURE news_count
@Table Varchar(100),
@Col Varchar(100),
@id int
AS
Begin
Declare @S Varchar(8000)
Select @S = ' Update ' + @Table + ' Set ' + @Col + ' = ' + @Col + ' + 1 Where id = ' + Cast(@id As Varchar)
EXEC(@S)
End
GO
------解决方案--------------------CREATE PROCEDURE news_count
@tableName varchar(20),
@id int
AS
declare @sql nvarchar(100)
set @sql=N 'Update '+@tableName+ ' Set hits = case when hits is null then 1 else hits + 1 end Where id = '+ltrim(@id)
exec sp_executesql @sql
------解决方案--------------------CREATE PROCEDURE news_count
@tb varchar(255),
@fld varchar(255),
@pk varchar(255),
@id int
AS
declare @s varchar(8000)
set @s = 'Update ' + @tb +
' Set ' + @fld + ' = ' + @fld + ' + 1 ' +
' Where ' + @pk + ' = ' + rtrim(@id)
exec(@s)
GO