SQL Server common CRUD stored procedures

Source: Internet
Author: User
Welcome to the Windows community forum and interact with 3 million technical staff. -- add the Stored Procedure ifexists (select * distinct) dropprocusp_insertgocreateprocusp_insert (@ tablenvarchar (255), @ valuesnvarchar (max

Welcome to the Windows community forum and interact with 3 million technical staff> go to -- General to add the stored procedure if exists (select * from sysobjects where name = 'usp _ insert ') drop proc usp_insert go create proc usp_insert (@ table nvarchar (255), @ values nvarchar (max

Welcome to the Windows community forum and interact with 3 million technicians>

-- General addition of Stored Procedures

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 '', ''100 '''

Go

-- General delete stored procedure

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

-- General Stored Procedure Modification

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

-- General query stored procedures

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.