Rumah  >  Artikel  >  pangkalan data  >  SQLServer通用的CRUD存储过程

SQLServer通用的CRUD存储过程

WBOY
WBOYasal
2016-06-07 15:51:231388semak imbas

欢迎进入Windows社区论坛,与300万技术人员互动交流 >>进入 --通用的增加存储过程 if exists (select * from sysobjects where name = 'usp_insert') drop proc usp_insert go create proc usp_insert ( @table nvarchar(255), @values nvarchar(max

欢迎进入Windows社区论坛,与300万技术人员互动交流 >>进入

    --通用的增加存储过程

    if exists (select * from sysobjects where name = 'usp_insert')

    drop proc usp_insert

    go

    create proc usp_insert

    (

    @table nvarchar(255),

    @values nvarchar(max)

    )

    as

    declare @sql nvarchar(max)

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

    exec sp_executesql @sql

    go

    exec usp_insert 'customer','''Tom'',''132342434243'''

    go

    --通用的删除存储过程

    if exists (select * from sysobjects where name = 'usp_delete')

    drop proc usp_delete

    go

    create proc usp_delete

    (

    @table nvarchar(255),

    @where nvarchar(max)

    )

    as

    declare @sql  nvarchar(max)

    set @sql = 'delete from ['+@table +']'

    if(@where is not null and len(@where)>0)

    set @sql += ' where '+@where

    exec sp_executesql @sql

    go

    exec usp_delete 'customer','id = 1'

    go

    -- 通用的修改存储过程

    if exists (select * from sysobjects where name = 'usp_update')

    drop proc usp_update

    go

    create proc usp_update

    (

    @table nvarchar(255),

    @set nvarchar(max),

    @where nvarchar(max)

    )

    as

    declare @sql  nvarchar(max)

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

    if(@where is not null and len(@where)>0)

    set @sql += ' where '+@where

    exec sp_executesql @sql

    go

    exec usp_update 'customer','name = ''Smile''','id = 1'

    go

    --通用的查询存储过程

    if exists (select * from sysobjects where name = 'usp_select')

    drop proc usp_select

    go

    create proc usp_select

    (

    @table nvarchar(255),

    @where nvarchar(max)

    )

    as

    declare @sql  nvarchar(max)

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

    if(@where is not null and len(@where)>0)

    set @sql += ' where '+@where

    exec sp_executesql @sql

    go

    exec usp_select 'customer','id = 2'

    go

SQLServer通用的CRUD存储过程

Kenyataan:
Kandungan artikel ini disumbangkan secara sukarela oleh netizen, dan hak cipta adalah milik pengarang asal. Laman web ini tidak memikul tanggungjawab undang-undang yang sepadan. Jika anda menemui sebarang kandungan yang disyaki plagiarisme atau pelanggaran, sila hubungi admin@php.cn