使用 SQL Server 添加删除修改查询储存过程
2014-11-03来源:易贤网

--添加

create procedure usp_add

(

@table nvarchar(255),

@values nvarchar(max)=null

)

as

declare @sql nvarchar(max)

set @sql='insert into '+@table

if @values is not null

set @sql='insert into '+@table+' values('+@values+')'

exec sp_executesql @sql

select @@IDENTITY

go

exec usp_Add '金山股份' ,'''abc'',20,300'

go

--删除

create procedure usp_delete

(

@table nvarchar(255),

@where nvarchar(max)=null

)

as

declare @sql nvarchar(max)

set @sql='delete '+@table

if @where is not null

set @sql+=' where '+@where

exec sp_executesql @sql

go

exec usp_delete '金山股分','id=1'

go

--修改

create procedure usp_update

(

@table nvarchar(255),

@set nvarchar(max),

@where nvarchar(max)=null

)

as

declare @sql nvarchar(max)

set @sql='update '+@table+' set '+@set

if @where is not null

set @sql+=' where '+@where

exec sp_executesql @sql

go

exec usp_update '金山股份','StockName=''腾讯股分''','id=2'

go

--查找

create procedure usp_select

(

@table nvarchar(255),

@where nvarchar(max)=null

)

as

declare @sql nvarchar(max)

set @sql='select * from '+@table

if @where is not null

set @sql=@sql+' where '+@where

exec sp_executesql @sql

go

exec usp_select 'Stock','id=1'

go

更多信息请查看IT技术专栏

推荐信息